TTransPtr.h
Go to the documentation of this file.
1 #ifndef istd_TRetSmartPtr_included
2 #define istd_TRetSmartPtr_included
3 
4 
5 // Qt includes
6 #include <QtCore/QtGlobal>
7 
8 // ACF includes
9 #include <istd/TPointerBase.h>
10 
11 
12 namespace istd
13 {
14 
15 
23 template <class Type>
24 class TTransPtr
25 {
26 public:
31  TTransPtr(const TTransPtr& pointer);
32  ~TTransPtr();
33 
37  bool IsValid() const;
38 
42  const Type* GetPtr() const;
43 
47  Type* GetPtr();
48 
52  void Reset();
53 
57  void Swap(TTransPtr& pointer);
58 
59  template <class CastedType>
60  CastedType Cast() const
61  {
62  return dynamic_cast<CastedType>(GetPtr());
63  }
64 
65  // operators
70  TTransPtr& operator=(const TTransPtr& pointer);
71  Type& operator*() const;
72  Type* operator->() const;
73 
74 protected:
75  class RefCountBase: public TPointerBase<Type>
76  {
77  public:
79 
80  explicit RefCountBase(Type* pointer)
81  : BaseClass(pointer)
82  {
83  Q_ASSERT(pointer != NULL);
84  }
85  virtual ~RefCountBase(){}
86 
87  // abstract methods
91  virtual void OnAttached() = 0;
95  virtual void OnDetached() = 0;
96  };
97 
98  TTransPtr();
99 
103  void Detach();
107  RefCountBase* GetInternalCounter(const TTransPtr& pointer) const;
108 
109  RefCountBase* m_counterPtr;
110 };
111 
112 
113 // public methods
114 
115 template <class Type>
117 : m_counterPtr(pointer.m_counterPtr)
118 {
119  const_cast<TTransPtr&>(pointer).m_counterPtr = NULL;
120 }
121 
122 
123 template <class Type>
125 {
126  Detach();
127 }
128 
129 
130 template <class Type>
132 {
133  return (m_counterPtr != NULL) && (m_counterPtr->GetPtr() != NULL);
134 }
135 
136 
137 template <class Type>
138 inline const Type* TTransPtr<Type>::GetPtr() const
139 {
140  if (m_counterPtr != NULL){
141  return m_counterPtr->GetPtr();
142  }
143  else{
144  return NULL;
145  }
146 }
147 
148 
149 template <class Type>
151 {
152  if (m_counterPtr != NULL){
153  return m_counterPtr->GetPtr();
154  }
155  else{
156  return NULL;
157  }
158 }
159 
160 
161 template <class Type>
163 {
164  Detach();
165 
166  m_counterPtr = NULL;
167 }
168 
169 
170 template <class Type>
172 {
173  qSwap(m_counterPtr, pointer.m_counterPtr);
174 }
175 
176 
177 template <class Type>
179 {
180  if (m_counterPtr != pointer.m_counterPtr){
181  Detach();
182 
183  m_counterPtr = pointer.m_counterPtr;
184 
185  const_cast<TTransPtr&>(pointer).m_counterPtr = NULL;
186  }
187 
188  return *this;
189 }
190 
191 
192 template <class Type>
193 inline Type& TTransPtr<Type>::operator*() const
194 {
195  Q_ASSERT(m_counterPtr != NULL);
196  Q_ASSERT(m_counterPtr->GetPtr() != NULL);
197 
198  return *m_counterPtr->GetPtr();
199 }
200 
201 
202 template <class Type>
203 inline Type* TTransPtr<Type>::operator->() const
204 {
205  Q_ASSERT(m_counterPtr != NULL);
206  Q_ASSERT(m_counterPtr->GetPtr() != NULL);
207 
208  return m_counterPtr->GetPtr();
209 }
210 
211 
212 // protected methods
213 
214 template <class Type>
216 : m_counterPtr(NULL)
217 {
218 }
219 
220 
221 template <class Type>
223 {
224  if (m_counterPtr != NULL){
225  m_counterPtr->OnDetached();
226  }
227 }
228 
229 
230 template <class Type>
232 {
233  return pointer.m_counterPtr;
234 }
235 
236 
237 } // namespace istd
238 
239 
240 #endif // !istd_TRetSmartPtr_included
241 
242 
Implementation of pointer wrapper.
Definition: TPointerBase.h:49
virtual void OnAttached()=0
Called if new pointer is attached to this internal handler.
Implementation of data transfer smart pointer.
Definition: TTransPtr.h:24
void Reset()
Set this pointer to NULL.
Definition: TTransPtr.h:162
RefCountBase * m_counterPtr
Definition: TTransPtr.h:109
RefCountBase(Type *pointer)
Definition: TTransPtr.h:80
TTransPtr & operator=(const TTransPtr &pointer)
Copy operator overtaking the pointer.
Definition: TTransPtr.h:178
Type & operator*() const
Definition: TTransPtr.h:193
bool IsValid() const
Check, whether the object is in valid state.
Definition: TTransPtr.h:131
RefCountBase * GetInternalCounter(const TTransPtr &pointer) const
Get internal counter.
Definition: TTransPtr.h:231
CastedType Cast() const
Definition: TTransPtr.h:60
#define NULL
Definition: istd.h:64
Type * operator->() const
Definition: TTransPtr.h:203
void Detach()
Detach counter object without changing of internal counter pointer.
Definition: TTransPtr.h:222
void Swap(TTransPtr &pointer)
Swap two pointers.
Definition: TTransPtr.h:171
TPointerBase< Type > BaseClass
Definition: TTransPtr.h:78
const Type * GetPtr() const
Get access to pointed object.
Definition: TTransPtr.h:138
virtual void OnDetached()=0
Called if pointer is detached from this internal handler.

© 2007-2017 Witold Gantzke and Kirill Lepskiy