pion-net  4.0.9
PionCounter.hpp
1 // -----------------------------------------------------------------------
2 // pion-common: a collection of common libraries used by the Pion Platform
3 // -----------------------------------------------------------------------
4 // Copyright (C) 2007-2008 Atomic Labs, Inc. (http://www.atomiclabs.com)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #ifndef __PION_PIONCOUNTER_HEADER__
11 #define __PION_PIONCOUNTER_HEADER__
12 
13 #include <pion/PionConfig.hpp>
14 #include <boost/cstdint.hpp>
15 #include <boost/thread/mutex.hpp>
16 
17 
18 namespace pion { // begin namespace pion
19 
20 
24 class PionCounter {
25 protected:
26 
28  inline void increment(void) {
29  boost::mutex::scoped_lock counter_lock(m_mutex);
30  ++m_value;
31  }
32 
34  inline void decrement(void) {
35  boost::mutex::scoped_lock counter_lock(m_mutex);
36  --m_value;
37  }
38 
40  template <typename IntegerType>
41  inline void add(const IntegerType& n) {
42  boost::mutex::scoped_lock counter_lock(m_mutex);
43  m_value += n;
44  }
45 
47  template <typename IntegerType>
48  inline void subtract(const IntegerType& n) {
49  boost::mutex::scoped_lock counter_lock(m_mutex);
50  m_value -= n;
51  }
52 
54  template <typename IntegerType>
55  inline void assign(const IntegerType& n) {
56  boost::mutex::scoped_lock counter_lock(m_mutex);
57  m_value = n;
58  }
59 
60 
61 public:
62 
64  explicit PionCounter(unsigned long n = 0) {
65  assign(n);
66  }
67 
69  virtual ~PionCounter() {}
70 
72  PionCounter(const PionCounter& c) : m_value(c.getValue()) {}
73 
75  inline const PionCounter& operator=(const PionCounter& c) { assign(c.getValue()); return *this; }
76 
78  inline const PionCounter& operator++(void) { increment(); return *this; }
79 
81  inline const PionCounter& operator--(void) { decrement(); return *this; }
82 
84  template <typename IntegerType>
85  inline const PionCounter& operator+=(const IntegerType& n) { add(n); return *this; }
86 
88  template <typename IntegerType>
89  inline const PionCounter& operator-=(const IntegerType& n) { subtract(n); return *this; }
90 
92  template <typename IntegerType>
93  inline const PionCounter& operator=(const IntegerType& n) { assign(n); return *this; }
94 
96  template <typename IntegerType>
97  inline bool operator==(const IntegerType& n) const { return getValue() == n; }
98 
100  template <typename IntegerType>
101  inline bool operator>(const IntegerType& n) const { return getValue() > n; }
102 
104  template <typename IntegerType>
105  inline bool operator<(const IntegerType& n) const { return getValue() < n; }
106 
108  template <typename IntegerType>
109  inline bool operator>=(const IntegerType& n) const { return getValue() >= n; }
110 
112  template <typename IntegerType>
113  inline bool operator<=(const IntegerType& n) const { return getValue() <= n; }
114 
116  inline void reset(void) { assign(0); }
117 
119  inline boost::uint64_t getValue(void) const {
120  return m_value;
121  }
122 
123 
124 private:
125 
127  boost::mutex m_mutex;
128 
130  boost::uint64_t m_value;
131 };
132 
133 
134 } // end namespace pion
135 
136 #endif
137