xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
basics
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
10
namespace
xrpl
{
11
15
class
CountedObjects
16
{
17
public
:
18
static
CountedObjects
&
19
getInstance
()
noexcept
;
20
21
using
Entry
=
std::pair<std::string, int>
;
22
using
List
=
std::vector<Entry>
;
23
24
[[nodiscard]]
List
25
getCounts
(
int
minimumThreshold)
const
;
26
27
public
:
33
class
Counter
34
{
35
public
:
36
Counter
(
std::string
name) noexcept :
name_
(std::move(name)),
count_
(0)
37
{
38
// Insert ourselves at the front of the lock-free linked list
39
CountedObjects
& instance =
CountedObjects::getInstance
();
40
Counter
* head =
nullptr
;
41
42
do
43
{
44
head = instance.
head_
.load();
45
next_
= head;
46
}
while
(instance.
head_
.exchange(
this
) != head);
47
48
++instance.
count_
;
49
}
50
51
~Counter
() noexcept = default;
52
53
int
54
increment
() noexcept
55
{
56
return
++
count_
;
57
}
58
59
int
60
decrement
() noexcept
61
{
62
return
--
count_
;
63
}
64
65
[[nodiscard]]
int
66
getCount
() const noexcept
67
{
68
return
count_
.load();
69
}
70
71
[[nodiscard]]
Counter
*
72
getNext
() const noexcept
73
{
74
return
next_
;
75
}
76
77
[[nodiscard]]
std::string
const
&
78
getName
() const noexcept
79
{
80
return
name_
;
81
}
82
83
private
:
84
std::string
const
name_
;
85
std::atomic<int>
count_
;
86
Counter
*
next_
;
87
};
88
89
private
:
90
CountedObjects
() noexcept;
91
~
CountedObjects
() noexcept = default;
92
93
private:
94
std
::atomic<
int
>
count_
;
95
std
::atomic<
Counter
*>
head_
;
96
};
97
98
//------------------------------------------------------------------------------
99
108
template <class Object>
109
class
CountedObject
110
{
111
private
:
112
static
auto
&
113
getCounter
() noexcept
114
{
115
static
CountedObjects::Counter
kC{
beast::typeName<Object>
()};
116
return
kC;
117
}
118
119
CountedObject
() noexcept
120
{
121
getCounter
().increment();
122
}
123
124
CountedObject
(
CountedObject
const
&)
noexcept
125
{
126
getCounter
().increment();
127
}
128
129
CountedObject
&
130
operator=
(
CountedObject
const
&)
noexcept
=
default
;
131
132
public
:
133
~CountedObject
() noexcept
134
{
135
getCounter
().decrement();
136
}
137
138
friend
Object
;
139
};
140
141
}
// namespace xrpl
atomic
std::string
xrpl::CountedObject
Tracks the number of instances of an object.
Definition
CountedObject.h:110
xrpl::CountedObject::Object
friend Object
Definition
CountedObject.h:138
xrpl::CountedObject::CountedObject
CountedObject(CountedObject const &) noexcept
Definition
CountedObject.h:124
xrpl::CountedObject::CountedObject
CountedObject() noexcept
Definition
CountedObject.h:119
xrpl::CountedObject::operator=
CountedObject & operator=(CountedObject const &) noexcept=default
xrpl::CountedObject::getCounter
static auto & getCounter() noexcept
Definition
CountedObject.h:113
xrpl::CountedObject::~CountedObject
~CountedObject() noexcept
Definition
CountedObject.h:133
xrpl::CountedObjects::Counter
Implementation for CountedObject.
Definition
CountedObject.h:34
xrpl::CountedObjects::Counter::name_
std::string const name_
Definition
CountedObject.h:84
xrpl::CountedObjects::Counter::~Counter
~Counter() noexcept=default
xrpl::CountedObjects::Counter::getCount
int getCount() const noexcept
Definition
CountedObject.h:66
xrpl::CountedObjects::Counter::decrement
int decrement() noexcept
Definition
CountedObject.h:60
xrpl::CountedObjects::Counter::getName
std::string const & getName() const noexcept
Definition
CountedObject.h:78
xrpl::CountedObjects::Counter::count_
std::atomic< int > count_
Definition
CountedObject.h:85
xrpl::CountedObjects::Counter::next_
Counter * next_
Definition
CountedObject.h:86
xrpl::CountedObjects::Counter::getNext
Counter * getNext() const noexcept
Definition
CountedObject.h:72
xrpl::CountedObjects::Counter::Counter
Counter(std::string name) noexcept
Definition
CountedObject.h:36
xrpl::CountedObjects::Counter::increment
int increment() noexcept
Definition
CountedObject.h:54
xrpl::CountedObjects::getCounts
List getCounts(int minimumThreshold) const
Definition
CountedObject.cpp:20
xrpl::CountedObjects::getInstance
static CountedObjects & getInstance() noexcept
Definition
CountedObject.cpp:8
xrpl::CountedObjects::List
std::vector< Entry > List
Definition
CountedObject.h:22
xrpl::CountedObjects::head_
std::atomic< Counter * > head_
Definition
CountedObject.h:95
xrpl::CountedObjects::count_
std::atomic< int > count_
Definition
CountedObject.h:94
xrpl::CountedObjects::Entry
std::pair< std::string, int > Entry
Definition
CountedObject.h:21
xrpl::CountedObjects::CountedObjects
CountedObjects() noexcept
Definition
CountedObject.cpp:15
beast::typeName
std::string typeName()
Definition
type_name.h:16
std
STL namespace.
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
std::pair
string
utility
vector
Generated by
1.17.0