TRef.h
Go to the documentation of this file.
1 /********************************************************************
2  *
3  * Project Artec 3D Scanning SDK
4  *
5  * Purpose: Helper class for safe intefrace storage
6  *
7  * Copyright: Artec Group
8  *
9  ********************************************************************/
10 
11 #ifndef _TREF_H_
12 #define _TREF_H_
13 
14 #include <stddef.h>
15 #include <assert.h>
16 #include <artec/sdk/base/IRef.h>
17 #include <artec/sdk/base/Log.h>
18 
19 namespace artec { namespace sdk { namespace base
20 {
21 
22 template <class T> class TRef
23 {
24 public:
25  typedef T base;
26 
27  TRef(T* p = NULL)
28  {
29  tObject_ = p;
30  if (p)
31  p->addRef();
32  }
33 
34  TRef(const TRef<T>& p)
35  {
36  tObject_ = p.tObject_;
37 
38  if (tObject_)
39  tObject_->addRef();
40  }
41 
43  {
44  tObject_ = p.tObject_;
45  p.tObject_ = NULL;
46  }
47 
49  {
50  if (tObject_)
51  tObject_->release();
52  }
53 
54  operator bool() const { return tObject_ != NULL; }
55 
56  T* operator ->() const { assert( tObject_ != NULL ); return tObject_; }
57 
58  operator T*() const { return tObject_; }
59 
60  const T& operator *() const { assert( tObject_ != NULL ); return *tObject_; }
61 
62  T& operator *() { assert( tObject_ != NULL ); return *tObject_; }
63 
65  {
66  T* tmpObject = tObject_;
67 
68  tObject_ = p;
69 
70  if (tObject_)
71  tObject_->addRef();
72 
73  if (tmpObject)
74  tmpObject->release();
75 
76  return *this;
77  }
78 
80  {
81  T* tmpObject = tObject_;
82 
83  tObject_ = p;
84 
85  if (tObject_)
86  tObject_->addRef();
87 
88  if (tmpObject)
89  tmpObject->release();
90 
91  return *this;
92  }
93 
94  /// move operator
96  {
97  // disable operator&() overloading
98  void* addressP = reinterpret_cast<void*>(&const_cast<char&>(reinterpret_cast<const volatile char&>(p)));
99 
100  if (this != addressP)
101  {
102  if (tObject_)
103  tObject_->release();
104 
105  tObject_ = p.tObject_;
106  p.tObject_ = NULL;
107  }
108  return *this;
109  }
110 
111  T** operator&()
112  {
113  if (tObject_ != NULL)
114  {
115  ABASESDK_ERROR(L"Please release your tref before using operator&");
116  return NULL;
117  }
118 
119  return &tObject_;
120  }
121 
122  bool operator==(T* p) const
123  {
124  return tObject_ == p;
125  }
126 
127  bool operator!=(T* p) const
128  {
129  return !(tObject_ == p);
130  }
131 
132  /// attach to interface pointer without calling addref() for it
133  void attach(T* p)
134  {
135  if (tObject_)
136  tObject_->release();
137 
138  tObject_ = p;
139  }
140 
141  /// detach interface pointer without calling release() for it
142  T* detach()
143  {
144  T* tmpObject = tObject_;
145  tObject_ = NULL;
146  return tmpObject;
147  }
148 
149  void release()
150  {
151  if (tObject_ == NULL)
152  return;
153 
154  tObject_->release();
155  tObject_ = NULL;
156  }
157 
158 protected:
160 
161 };
162 
163 } } } // namespace artec::sdk::base
164 
165 #endif // _TREF_H_
TRef(const TRef< T > &p)
Definition: TRef.h:34
TRef(TRef< T > &&p)
Definition: TRef.h:42
void attach(T *p)
attach to interface pointer without calling addref() for it
Definition: TRef.h:133
TRef< T > & operator=(T *p)
Definition: TRef.h:64
T * operator->() const
Definition: TRef.h:56
TRef(T *p=NULL)
Definition: TRef.h:27
TRef< T > & operator=(TRef< T > &&p)
move operator
Definition: TRef.h:95
bool operator==(T *p) const
Definition: TRef.h:122
const T & operator*() const
Definition: TRef.h:60
T * detach()
detach interface pointer without calling release() for it
Definition: TRef.h:142
bool operator!=(T *p) const
Definition: TRef.h:127
TRef< T > & operator=(const TRef< T > &p)
Definition: TRef.h:79
#define ABASESDK_ERROR(...)
Definition: Log.h:65