rippled
Loading...
Searching...
No Matches
CountedObject.h
1#ifndef XRPL_BASICS_COUNTEDOBJECT_H_INCLUDED
2#define XRPL_BASICS_COUNTEDOBJECT_H_INCLUDED
3
4#include <xrpl/beast/type_name.h>
5
6#include <atomic>
7#include <string>
8#include <utility>
9#include <vector>
10
11namespace ripple {
12
15{
16public:
17 static CountedObjects&
18 getInstance() noexcept;
19
22
23 List
24 getCounts(int minimumThreshold) const;
25
26public:
31 class Counter
32 {
33 public:
34 Counter(std::string name) noexcept : name_(std::move(name)), count_(0)
35 {
36 // Insert ourselves at the front of the lock-free linked list
38 Counter* head;
39
40 do
41 {
42 head = instance.m_head.load();
43 next_ = head;
44 } while (instance.m_head.exchange(this) != head);
45
46 ++instance.m_count;
47 }
48
49 ~Counter() noexcept = default;
50
51 int
52 increment() noexcept
53 {
54 return ++count_;
55 }
56
57 int
58 decrement() noexcept
59 {
60 return --count_;
61 }
62
63 int
64 getCount() const noexcept
65 {
66 return count_.load();
67 }
68
69 Counter*
70 getNext() const noexcept
71 {
72 return next_;
73 }
74
75 std::string const&
76 getName() const noexcept
77 {
78 return name_;
79 }
80
81 private:
85 };
86
87private:
88 CountedObjects() noexcept;
89 ~CountedObjects() noexcept = default;
90
91private:
92 std::atomic<int> m_count;
93 std::atomic<Counter*> m_head;
94};
95
96//------------------------------------------------------------------------------
97
105template <class Object>
107{
108private:
109 static auto&
110 getCounter() noexcept
111 {
112 static CountedObjects::Counter c{beast::type_name<Object>()};
113 return c;
114 }
115
116public:
117 CountedObject() noexcept
118 {
119 getCounter().increment();
120 }
121
123 {
124 getCounter().increment();
125 }
126
128 operator=(CountedObject const&) noexcept = default;
129
130 ~CountedObject() noexcept
131 {
132 getCounter().decrement();
133 }
134};
135
136} // namespace ripple
137
138#endif
Tracks the number of instances of an object.
CountedObject & operator=(CountedObject const &) noexcept=default
CountedObject(CountedObject const &) noexcept
static auto & getCounter() noexcept
Implementation for CountedObject.
Counter(std::string name) noexcept
Counter * getNext() const noexcept
int getCount() const noexcept
std::string const & getName() const noexcept
Manages all counted object types.
std::atomic< Counter * > m_head
std::atomic< int > m_count
static CountedObjects & getInstance() noexcept
List getCounts(int minimumThreshold) const
T load(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
STL namespace.