pion-net  4.0.9
common/include/pion/PionCounter.hpp
00001 // -----------------------------------------------------------------------
00002 // pion-common: a collection of common libraries used by the Pion Platform
00003 // -----------------------------------------------------------------------
00004 // Copyright (C) 2007-2008 Atomic Labs, Inc.  (http://www.atomiclabs.com)
00005 //
00006 // Distributed under the Boost Software License, Version 1.0.
00007 // See http://www.boost.org/LICENSE_1_0.txt
00008 //
00009 
00010 #ifndef __PION_PIONCOUNTER_HEADER__
00011 #define __PION_PIONCOUNTER_HEADER__
00012 
00013 #include <pion/PionConfig.hpp>
00014 #include <boost/cstdint.hpp>
00015 #include <boost/thread/mutex.hpp>
00016 
00017 
00018 namespace pion {    // begin namespace pion
00019 
00020 
00024 class PionCounter {
00025 protected:
00026     
00028     inline void increment(void) {
00029         boost::mutex::scoped_lock counter_lock(m_mutex);
00030         ++m_value;
00031     }
00032     
00034     inline void decrement(void) {
00035         boost::mutex::scoped_lock counter_lock(m_mutex);
00036         --m_value;
00037     }
00038     
00040     template <typename IntegerType>
00041     inline void add(const IntegerType& n) {
00042         boost::mutex::scoped_lock counter_lock(m_mutex);
00043         m_value += n;
00044     }
00045     
00047     template <typename IntegerType>
00048     inline void subtract(const IntegerType& n) {
00049         boost::mutex::scoped_lock counter_lock(m_mutex);
00050         m_value -= n;
00051     }
00052 
00054     template <typename IntegerType>
00055     inline void assign(const IntegerType& n) {
00056         boost::mutex::scoped_lock counter_lock(m_mutex);
00057         m_value = n;
00058     }
00059     
00060 
00061 public:
00062 
00064     explicit PionCounter(unsigned long n = 0) {
00065         assign(n);
00066     }
00067 
00069     virtual ~PionCounter() {}
00070 
00072     PionCounter(const PionCounter& c) : m_value(c.getValue()) {}
00073     
00075     inline const PionCounter& operator=(const PionCounter& c) { assign(c.getValue()); return *this; }
00076     
00078     inline const PionCounter& operator++(void) { increment(); return *this; }
00079     
00081     inline const PionCounter& operator--(void) { decrement(); return *this; }
00082     
00084     template <typename IntegerType>
00085     inline const PionCounter& operator+=(const IntegerType& n) { add(n); return *this; }
00086     
00088     template <typename IntegerType>
00089     inline const PionCounter& operator-=(const IntegerType& n) { subtract(n); return *this; }
00090     
00092     template <typename IntegerType>
00093     inline const PionCounter& operator=(const IntegerType& n) { assign(n); return *this; }
00094 
00096     template <typename IntegerType>
00097     inline bool operator==(const IntegerType& n) const { return getValue() == n; }
00098     
00100     template <typename IntegerType>
00101     inline bool operator>(const IntegerType& n) const { return getValue() > n; }
00102     
00104     template <typename IntegerType>
00105     inline bool operator<(const IntegerType& n) const { return getValue() < n; }
00106     
00108     template <typename IntegerType>
00109     inline bool operator>=(const IntegerType& n) const { return getValue() >= n; }
00110     
00112     template <typename IntegerType>
00113     inline bool operator<=(const IntegerType& n) const { return getValue() <= n; }
00114     
00116     inline void reset(void) { assign(0); }
00117 
00119     inline boost::uint64_t getValue(void) const {
00120         return m_value;
00121     }
00122     
00123 
00124 private:
00125 
00127     boost::mutex                m_mutex;
00128     
00130     boost::uint64_t             m_value;
00131 };
00132 
00133 
00134 }   // end namespace pion
00135 
00136 #endif
00137