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& getInstance () noexcept;
35 
38 
39  List getCounts (int minimumThreshold) const;
40 
41 public:
47  {
48  public:
49  CounterBase () noexcept;
50 
51  virtual ~CounterBase () noexcept;
52 
53  int increment () noexcept
54  {
55  return ++m_count;
56  }
57 
58  int decrement () noexcept
59  {
60  return --m_count;
61  }
62 
63  int getCount () const noexcept
64  {
65  return m_count.load ();
66  }
67 
68  CounterBase* getNext () const noexcept
69  {
70  return m_next;
71  }
72 
73  virtual char const* getName () const = 0;
74 
75  private:
76  virtual void checkPureVirtual () const = 0;
77 
78  protected:
81  };
82 
83 private:
84  CountedObjects () noexcept;
85  ~CountedObjects () noexcept = default;
86 
87 private:
88  std::atomic <int> m_count;
89  std::atomic <CounterBase*> m_head;
90 };
91 
92 //------------------------------------------------------------------------------
93 
101 template <class Object>
103 {
104 public:
105  CountedObject () noexcept
106  {
107  getCounter ().increment ();
108  }
109 
110  CountedObject (CountedObject const&) noexcept
111  {
112  getCounter ().increment ();
113  }
114 
115  CountedObject& operator=(CountedObject const&) noexcept = default;
116 
117  ~CountedObject () noexcept
118  {
119  getCounter ().decrement ();
120  }
121 
122 private:
124  {
125  public:
126  Counter () noexcept { }
127 
128  char const* getName () const override
129  {
130  return Object::getCountedObjectName ();
131  }
132 
133  void checkPureVirtual () const override { }
134  };
135 
136 private:
137  static Counter& getCounter() noexcept
138  {
139  static_assert(std::is_nothrow_constructible<Counter>{}, "");
140  static Counter c;
141  return c;
142  }
143 };
144 
145 } // ripple
146 
147 #endif
ripple::CountedObjects::m_count
std::atomic< int > m_count
Definition: CountedObject.h:88
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:102
utility
ripple::CountedObjects::CounterBase::m_count
std::atomic< int > m_count
Definition: CountedObject.h:79
std::pair
vector
ripple::CountedObject::~CountedObject
~CountedObject() noexcept
Definition: CountedObject.h:117
ripple::CountedObjects::CounterBase::getNext
CounterBase * getNext() const noexcept
Definition: CountedObject.h:68
ripple::CountedObjects::CountedObjects
CountedObjects() noexcept
Definition: CountedObject.cpp:32
ripple::CountedObjects::CounterBase::m_next
CounterBase * m_next
Definition: CountedObject.h:80
ripple::CountedObject::Counter::Counter
Counter() noexcept
Definition: CountedObject.h:126
ripple::CountedObject::Counter
Definition: CountedObject.h:123
ripple::CountedObject::Counter::getName
char const * getName() const override
Definition: CountedObject.h:128
ripple::CountedObjects::getInstance
static CountedObjects & getInstance() noexcept
Definition: CountedObject.cpp:25
ripple::CountedObjects::CounterBase::getCount
int getCount() const noexcept
Definition: CountedObject.h:63
std::atomic::load
T load(T... args)
ripple::CountedObjects::CounterBase::~CounterBase
virtual ~CounterBase() noexcept
Definition: CountedObject.cpp:88
atomic
ripple::CountedObject::CountedObject
CountedObject(CountedObject const &) noexcept
Definition: CountedObject.h:110
ripple::CountedObjects
Manages all counted object types.
Definition: CountedObject.h:31
ripple::CountedObjects::CounterBase
Implementation for CountedObject.
Definition: CountedObject.h:46
ripple::CountedObjects::CounterBase::getName
virtual char const * getName() const =0
ripple::CountedObjects::m_head
std::atomic< CounterBase * > m_head
Definition: CountedObject.h:89
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:105
ripple::CountedObjects::getCounts
List getCounts(int minimumThreshold) const
Definition: CountedObject.cpp:38
ripple::CountedObject::getCounter
static Counter & getCounter() noexcept
Definition: CountedObject.h:137
ripple::CountedObject::Counter::checkPureVirtual
void checkPureVirtual() const override
Definition: CountedObject.h:133
ripple::CountedObjects::CounterBase::increment
int increment() noexcept
Definition: CountedObject.h:53
std::is_nothrow_constructible
ripple::CountedObjects::CounterBase::CounterBase
CounterBase() noexcept
Definition: CountedObject.cpp:70
ripple::CountedObjects::CounterBase::decrement
int decrement() noexcept
Definition: CountedObject.h:58
string