ReferenceCount.hh
Go to the documentation of this file.
00001 
00041 #ifndef CRL_MULTISENSE_REFERENCECOUNT_HH
00042 #define CRL_MULTISENSE_REFERENCECOUNT_HH
00043 
00044 #include <stdint.h>
00045 
00046 namespace crl {
00047 namespace multisense {
00048 namespace details {
00049 namespace utility {
00050 
00051 class ReferenceCount
00052 {
00053 public:
00054     
00055     bool isShared() const {
00056         if (m_countP && (*m_countP) > 1)
00057             return true;
00058         return false;
00059     }
00060 
00061     void reset() {
00062         release();
00063         m_countP = new int32_t(1);
00064     }
00065 
00066     ReferenceCount() 
00067         : m_countP(new int32_t(1)) {};
00068 
00069     ReferenceCount(const ReferenceCount& source) 
00070         : m_countP(source.m_countP) {
00071         share();
00072     }
00073 
00074     ~ReferenceCount() {
00075         release();
00076     }
00077 
00078     ReferenceCount& operator=(const ReferenceCount& source) {
00079         if (this != &source) {
00080             release();
00081             m_countP = source.m_countP;
00082             share();
00083         }
00084         return *this;
00085     }                  
00086 
00087 private:
00088 
00089     volatile int32_t *m_countP;
00090         
00091     void share() {
00092         if (m_countP)
00093 #if WIN32
00094             InterlockedIncrement((LONG*)m_countP);
00095 #else
00096             __sync_fetch_and_add(m_countP, 1);
00097 #endif
00098     }
00099 
00100     void release() {
00101         if (m_countP) {
00102 #if WIN32
00103             int32_t count = InterlockedDecrement((LONG*)m_countP);
00104 #else
00105             int32_t count = __sync_sub_and_fetch(m_countP, 1);
00106 #endif
00107             if (count <= 0)
00108                 delete m_countP;
00109             m_countP = NULL;
00110         }
00111     }
00112 };
00113 
00114 }}}} // namespaces
00115 
00116 #endif /* #ifndef CRL_MULTISENSE_REFERENCECOUNT_HH */


multisense_lib
Author(s):
autogenerated on Thu Aug 27 2015 14:01:11