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