-
Notifications
You must be signed in to change notification settings - Fork 0
/
trackable.hpp
100 lines (74 loc) · 2.34 KB
/
trackable.hpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef SCHLAZICONTROL_TRACKABLE_HPP
#define SCHLAZICONTROL_TRACKABLE_HPP
#include <cstdint>
#include <iosfwd>
namespace sc {
namespace detail {
/**
* class TrackableTracker
*/
class TrackableTracker
{
public:
void announce();
void forget();
void increase( std::size_t allocated );
void decrease( std::size_t allocated );
void statistics( std::ostream& os ) const;
private:
std::size_t count_ {};
std::size_t allocated_ {};
};
/**
* class TrackableBase
*/
class TrackableBase
{
public:
TrackableBase( TrackableTracker& tracker );
TrackableBase( TrackableBase const& other );
TrackableBase( TrackableBase&& other );
~TrackableBase();
TrackableBase& operator=( TrackableBase const& other );
TrackableBase& operator=( TrackableBase&& other );
TrackableBase const& trackable() const { return *this; }
TrackableTracker const& tracker() const { return *tracker_; }
void statistics( std::ostream& os ) const;
protected:
template< typename Type >
void allocate( std::size_t count )
{
increase( count * sizeof( Type ) );
}
template< typename Type >
void deallocate( std::size_t count )
{
decrease( count * sizeof( Type ) );
}
private:
void increase( std::size_t allocated );
void decrease( std::size_t allocated );
TrackableTracker* tracker_;
std::size_t allocated_ {};
};
} // namespace detail
/**
* class Trackable
*/
template< typename Derived >
class Trackable : public detail::TrackableBase
{
public:
using Trackable_ = Trackable;
static detail::TrackableTracker const& tracker() { return tracker_; }
Trackable()
: detail::TrackableBase( tracker_ )
{
}
private:
static detail::TrackableTracker tracker_;
};
template< typename Derived >
detail::TrackableTracker Trackable< Derived >::tracker_;
} // namespace sc
#endif // SCHLAZICONTROL_TRACKABLE_HPP