-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstances_counter.hpp
More file actions
64 lines (58 loc) · 2.13 KB
/
Copy pathinstances_counter.hpp
File metadata and controls
64 lines (58 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* File: /lib/debug/instances_counter.hpp
*
* This file is a part of cpplib project which is distributed under MIT License.
* See file LICENSE for full license details.
*
* Copyright (c) 2020-present Nikita Zuev (V.Slavski!) <[email protected]>
*/
#ifndef CPPLIB__lib__debug__instances_counter__hpp
#define CPPLIB__lib__debug__instances_counter__hpp
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstdio>
namespace lib::debug {
// DECLARATION lib::debug::InstancesCounter
template< class T, class CounterType = ::std::size_t >
struct InstancesCounter {
struct DbgCounter {
void dump() const noexcept {
::std::fprintf( stderr,
"%s"
"\n\tinstances alive: %lu"
"\tctor(): %lu"
"\tctor(&): %lu"
"\tctor(&&): %lu"
"\top =(&): %lu"
"\top =(&&): %lu"
"\n"
, __PRETTY_FUNCTION__
, (::std::uint64_t) instances
, (::std::uint64_t) max_instances
, (::std::uint64_t) ctor_copied
, (::std::uint64_t) ctor_moved
, (::std::uint64_t) copied
, (::std::uint64_t) moved
);
}
CounterType instances = 0;
CounterType max_instances = 0;
CounterType ctor_copied = 0;
CounterType ctor_moved = 0;
CounterType copied = 0;
CounterType moved = 0;
private:
friend struct InstancesCounter<T, CounterType>;
~DbgCounter() noexcept { dump(); assert( instances == 0 ); }
static DbgCounter & get() noexcept { static DbgCounter counter; return counter; }
};
static const DbgCounter & dbg_counter() noexcept { DbgCounter::get(); }
InstancesCounter() noexcept { ++DbgCounter::get().instances; ++DbgCounter::get().max_instances; }
InstancesCounter( const InstancesCounter & ) noexcept : InstancesCounter() { ++DbgCounter::get().ctor_copied; }
InstancesCounter( InstancesCounter && ) noexcept : InstancesCounter() { ++DbgCounter::get().ctor_moved; }
~InstancesCounter() noexcept { --DbgCounter::get().instances; }
InstancesCounter & operator = ( const InstancesCounter & ) noexcept { ++DbgCounter::get().copied; }
InstancesCounter & operator = ( InstancesCounter && ) noexcept { ++DbgCounter::get().moved; }
};
} // namespace lib::debug
#endif // CPPLIB__lib__debug__instances_counter__hpp