Artec 3D Scanning SDK  2.0
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 
40 /**
41 * Basic reference implementation of IRef interface
42 */
44 {
45 public:
46  RefCounter() : refCount_(1) {}
47 
48  int addRef() const
49  {
50 #ifdef _WIN32
51  return _InterlockedIncrement((volatile long*)&refCount_);
52 #else
53  // We assume that Linux or OS X here
54  return __sync_add_and_fetch(&refCount_, 1);
55 #endif
56  }
57 
58  int release() const
59  {
60 #ifdef _WIN32
61  return _InterlockedDecrement((volatile long*)&refCount_);
62 #else
63  // We assume that Linux or OS X is here
64  return __sync_sub_and_fetch(&refCount_, 1);
65 #endif
66  }
67 
68 private:
69  mutable volatile int refCount_;
70 };
71 
72 } // namespace details
73 
74 /// Implementation of IRef interface. To create your own class, inherit it from RefBase.
75 template <class Interface> class RefBase : public Interface
76 {
77 public:
79  virtual ~RefBase() {}
80 };
81 
82 } } } // namespace artec::sdk::base
83 
84 #endif // _REF_H_
Basic reference implementation of IRef interface.
Definition: RefBase.h:43
Implementation of IRef interface. To create your own class, inherit it from RefBase.
Definition: RefBase.h:75