rippled
CountedObject.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_BASICS_COUNTEDOBJECT_H_INCLUDED
21 #define RIPPLE_BASICS_COUNTEDOBJECT_H_INCLUDED
22 
23 #include <atomic>
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 namespace ripple {
29 
32 {
33 public:
34  static CountedObjects&
35  getInstance() noexcept;
36 
39 
40  List
41  getCounts(int minimumThreshold) const;
42 
43 public:
49  {
50  public:
51  CounterBase() noexcept;
52 
53  virtual ~CounterBase() noexcept;
54 
55  int
56  increment() noexcept
57  {
58  return ++m_count;
59  }
60 
61  int
62  decrement() noexcept
63  {
64  return --m_count;
65  }
66 
67  int
68  getCount() const noexcept
69  {
70  return m_count.load();
71  }
72 
74  getNext() const noexcept
75  {
76  return m_next;
77  }
78 
79  virtual char const*
80  getName() const = 0;
81 
82  private:
83  virtual void
84  checkPureVirtual() const = 0;
85 
86  protected:
89  };
90 
91 private:
92  CountedObjects() noexcept;
93  ~CountedObjects() noexcept = default;
94 
95 private:
96  std::atomic<int> m_count;
97  std::atomic<CounterBase*> m_head;
98 };
99 
100 //------------------------------------------------------------------------------
101 
109 template <class Object>
111 {
112 public:
113  CountedObject() noexcept
114  {
115  getCounter().increment();
116  }
117 
118  CountedObject(CountedObject const&) noexcept
119  {
120  getCounter().increment();
121  }
122 
124  operator=(CountedObject const&) noexcept = default;
125 
126  ~CountedObject() noexcept
127  {
128  getCounter().decrement();
129  }
130 
131 private:
133  {
134  public:
135  Counter() noexcept
136  {
137  }
138 
139  char const*
140  getName() const override
141  {
142  return Object::getCountedObjectName();
143  }
144 
145  void
146  checkPureVirtual() const override
147  {
148  }
149  };
150 
151 private:
152  static Counter&
153  getCounter() noexcept
154  {
155  static_assert(std::is_nothrow_constructible<Counter>{}, "");
156  static Counter c;
157  return c;
158  }
159 };
160 
161 } // namespace ripple
162 
163 #endif
ripple::CountedObjects::m_count
std::atomic< int > m_count
Definition: CountedObject.h:96
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:110
utility
ripple::CountedObjects::CounterBase::m_count
std::atomic< int > m_count
Definition: CountedObject.h:87
std::pair
vector
ripple::CountedObject::~CountedObject
~CountedObject() noexcept
Definition: CountedObject.h:126
ripple::CountedObjects::CounterBase::getNext
CounterBase * getNext() const noexcept
Definition: CountedObject.h:74
ripple::CountedObjects::CountedObjects
CountedObjects() noexcept
Definition: CountedObject.cpp:33
ripple::CountedObjects::CounterBase::m_next
CounterBase * m_next
Definition: CountedObject.h:88
ripple::CountedObject::Counter::Counter
Counter() noexcept
Definition: CountedObject.h:135
ripple::CountedObject::Counter
Definition: CountedObject.h:132
ripple::CountedObject::Counter::getName
char const * getName() const override
Definition: CountedObject.h:140
ripple::CountedObjects::getInstance
static CountedObjects & getInstance() noexcept
Definition: CountedObject.cpp:26
ripple::CountedObjects::CounterBase::getCount
int getCount() const noexcept
Definition: CountedObject.h:68
std::atomic::load
T load(T... args)
ripple::CountedObjects::CounterBase::~CounterBase
virtual ~CounterBase() noexcept
Definition: CountedObject.cpp:86
atomic
ripple::CountedObject::CountedObject
CountedObject(CountedObject const &) noexcept
Definition: CountedObject.h:118
ripple::CountedObjects
Manages all counted object types.
Definition: CountedObject.h:31
ripple::CountedObjects::CounterBase
Implementation for CountedObject.
Definition: CountedObject.h:48
ripple::CountedObjects::CounterBase::getName
virtual char const * getName() const =0
ripple::CountedObjects::m_head
std::atomic< CounterBase * > m_head
Definition: CountedObject.h:97
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::CountedObjects::CounterBase::checkPureVirtual
virtual void checkPureVirtual() const =0
std
STL namespace.
ripple::CountedObject::CountedObject
CountedObject() noexcept
Definition: CountedObject.h:113
ripple::CountedObjects::getCounts
List getCounts(int minimumThreshold) const
Definition: CountedObject.cpp:38
ripple::CountedObject::getCounter
static Counter & getCounter() noexcept
Definition: CountedObject.h:153
ripple::CountedObject::Counter::checkPureVirtual
void checkPureVirtual() const override
Definition: CountedObject.h:146
ripple::CountedObjects::CounterBase::increment
int increment() noexcept
Definition: CountedObject.h:56
std::is_nothrow_constructible
ripple::CountedObjects::CounterBase::CounterBase
CounterBase() noexcept
Definition: CountedObject.cpp:70
ripple::CountedObjects::CounterBase::decrement
int decrement() noexcept
Definition: CountedObject.h:62
string