LibMultiSense
LibMultiSense Documentation
Loading...
Searching...
No Matches
ReferenceCount.hh
Go to the documentation of this file.
1
41#ifndef CRL_MULTISENSE_REFERENCECOUNT_HH
42#define CRL_MULTISENSE_REFERENCECOUNT_HH
43
44#include <stdint.h>
45
46namespace crl {
47namespace multisense {
48namespace details {
49namespace utility {
50
52{
53public:
54
55 bool isShared() const {
56 if (m_countP && (*m_countP) > 1)
57 return true;
58 return false;
59 }
60
61 void reset() {
62 release();
63 m_countP = new int32_t(1);
64 }
65
67 : m_countP(new int32_t(1)) {};
68
70 : m_countP(source.m_countP) {
71 share();
72 }
73
75 release();
76 }
77
79 if (this != &source) {
80 release();
81 m_countP = source.m_countP;
82 share();
83 }
84 return *this;
85 }
86
87private:
88
89 volatile int32_t *m_countP;
90
91 void share() {
92 if (m_countP)
93#ifdef WIN32
94 InterlockedIncrement((LONG*)m_countP);
95#else
96 __sync_fetch_and_add(m_countP, 1);
97#endif
98 }
99
100 void release() {
101 if (m_countP) {
102#ifdef WIN32
103 int32_t count = InterlockedDecrement((LONG*)m_countP);
104#else
105 int32_t count = __sync_sub_and_fetch(m_countP, 1);
106#endif
107 if (count <= 0)
108 delete m_countP;
109 m_countP = NULL;
110 }
111 }
112};
113
114}}}} // namespaces
115
116#endif /* #ifndef CRL_MULTISENSE_REFERENCECOUNT_HH */
ReferenceCount & operator=(const ReferenceCount &source)