RefBase.h
Go to the documentation of this file.
1 /********************************************************************
2  *
3  * Project Artec 3D Scanning SDK
4  *
5  * Purpose: Ref counter declaration
6  *
7  * Copyright: Artec Group
8  *
9  ********************************************************************/
10 
11 #ifndef _REF_H_
12 #define _REF_H_
13 
14 #ifdef WIN32
15 #include <intrin.h>
16 #endif
17 
18 #define IMPLEMENT_IREF \
19  virtual int addRef() const \
20  { \
21  return ref_.addRef(); \
22  } \
23  virtual int release() const \
24  { \
25  long refCount = ref_.release(); \
26  if (refCount == 0) \
27  delete this; \
28  \
29  return refCount; \
30  } \
31 private: \
32  artec::sdk::base::details::RefCounter ref_; \
33 public:
34 
35 namespace artec { namespace sdk { namespace base
36 {
37 namespace details
38 {
39 
41 {
42 public:
43  RefCounter() : refCount_(1) {}
44 
45  int addRef() const
46  {
47 #ifdef WIN32
48  return _InterlockedIncrement((volatile long*)&refCount_);
49 #else
50  // we assume linux or osx here
51  return __sync_add_and_fetch(&refCount_, 1);
52 #endif
53  }
54 
55  int release() const
56  {
57 #ifdef WIN32
58  return _InterlockedDecrement((volatile long*)&refCount_);
59 #else
60  // we assume linux or osx here
61  return __sync_sub_and_fetch(&refCount_, 1);
62 #endif
63  }
64 
65 private:
66  mutable volatile int refCount_;
67 };
68 
69 } // namespace details
70 
71 template <class Interface> class RefBase : public Interface
72 {
73 public:
75  virtual ~RefBase() {}
76 };
77 
78 } } } // namespace artec::sdk::base
79 
80 #endif // _REF_H_