CFx SDK Documentation  2023 SP0
SmartPtr.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2017, Open Design Alliance (the "Alliance").
3 // All rights reserved.
4 //
5 // This software and its documentation and related materials are owned by
6 // the Alliance. The software may only be incorporated into application
7 // programs owned by members of the Alliance, subject to a signed
8 // Membership Agreement and Supplemental Software License Agreement with the
9 // Alliance. The structure and organization of this software are the valuable
10 // trade secrets of the Alliance and its suppliers. The software is also
11 // protected by copyright law and international treaty provisions. Application
12 // programs incorporating this software must include the following statement
13 // with their copyright notices:
14 //
15 // This application incorporates Teigha(R) software pursuant to a license
16 // agreement with Open Design Alliance.
17 // Teigha(R) Copyright (C) 2002-2017 by Open Design Alliance.
18 // All rights reserved.
19 //
20 // By use of this software, its documentation or related materials, you
21 // acknowledge and accept the above terms.
23 
24 #ifndef _ODASMARTPOINTER_INCLUDED_
25 #define _ODASMARTPOINTER_INCLUDED_
26 
27 #include "TD_PackPush.h"
29 #include "BaseObjectPtr.h"
30 
57 template <class T> class OdSmartPtr : public OdBaseObjectPtr
58 {
59 protected:
64  {
65  if(m_pObject)
66  m_pObject->addRef();
67  }
68 
80  void assign(const T* pObject)
81  {
82  if (m_pObject != pObject) // Prevent deleting object if nRefs=1
83  {
84  release();
85  m_pObject = const_cast<T*>(pObject);
87  }
88  }
89 
98  void internalQueryX(const OdRxObject* pObject)
99  {
100  if(pObject)
101  {
102  OdRxObject* pX = pObject->queryX(T::desc());
103  if(pX)
104  m_pObject = pX;
105  else
106  throw OdError_NotThatKindOfClass(pObject->isA(), T::desc());
107  }
108  }
109 
110  void assign(const OdRxObject* pObject)
111  {
112  release();
113  internalQueryX(pObject);
114  }
115 
116  // Note: Using of SmartPtr<T> as bool expression produce ambiguous call with some compilers.
117  // Use isNull() method instead.
118 
124  bool operator !() const { ODA_FAIL(); return false; }
125 
131  operator bool() const { ODA_FAIL(); return false; }
132 
138  operator bool() { ODA_FAIL(); return false; }
139 
140 public:
154  : OdBaseObjectPtr()
155  {
156  }
157 
158  OdSmartPtr(const T* pObject, OdRxObjMod)
159  : OdBaseObjectPtr(pObject)
160  {
161  }
162 
163  OdSmartPtr(const T* pObject)
164  : OdBaseObjectPtr(pObject)
165  {
166  internalAddRef();
167  }
168 
169  OdSmartPtr(const OdRxObject* pObject)
170  : OdBaseObjectPtr()
171  {
172  internalQueryX(pObject);
173  }
174 
176  : OdBaseObjectPtr()
177  {
178  internalQueryX(pObject);
179  if(pObject)
180  pObject->release();
181  }
182 
183  OdSmartPtr(const OdSmartPtr& pObject)
184  : OdBaseObjectPtr(pObject.get())
185  {
186  internalAddRef();
187  }
188 
189  OdSmartPtr(const OdRxObjectPtr& pObject)
190  : OdBaseObjectPtr()
191  {
192  internalQueryX(pObject.get());
193  }
194 
195  OdSmartPtr(const OdBaseObjectPtr& pObject)
196  : OdBaseObjectPtr()
197  {
198  internalQueryX(pObject.get());
199  }
200 
215  void attach(const T* pObject)
216  {
217  release();
218  m_pObject = const_cast<T*>(pObject);
219  }
220 
235  void attach(OdRxObject* pObject)
236  {
237  release();
238  internalQueryX(pObject);
239  if(pObject)
240  pObject->release();
241  }
242 
250  {
251  release();
252  }
253 
264  void release()
265  {
266  if (m_pObject)
267  {
268  m_pObject->release();
269  m_pObject = 0;
270  }
271  }
272 
283  T* detach()
284  {
285  T* pRes = static_cast<T*>(m_pObject);
286  m_pObject = 0;
287  return pRes;
288  }
289 
299  {
300  assign(pObject);
301  return *this;
302  }
303 
305  {
306  assign(pObject.get());
307  return *this;
308  }
309 
310  OdSmartPtr& operator = (const T* pObject)
311  {
312  assign(pObject);
313  return *this;
314  }
315 
326  const T* get() const
327  {
328  return static_cast<const T*>(m_pObject);
329  }
330 
341  T* get()
342  {
343  return static_cast<T*>(m_pObject);
344  }
345 
353  {
354  return static_cast<T*>(m_pObject);
355  }
362  const T* operator ->() const
363  {
364  return static_cast<const T*>(m_pObject);
365  }
366 
367 #ifdef ODA_GCC_2_95
375  operator T*() const
376  {
377  return const_cast<T*>(static_cast<const T*>(m_pObject));
378  }
379 
380 #else
389  operator T*()
390  {
391  return static_cast<T*>(m_pObject);
392  }
393 
394  operator const T*() const
395  {
396  return static_cast<const T*>(m_pObject);
397  }
398 
399 #endif
400 
401  bool operator==(const void* pObject) const
402  {
403  return (m_pObject==pObject);
404  }
405 
406  bool operator==(const OdSmartPtr& pObject) const
407  {
408  return operator==((void*)pObject.get());
409  }
410 
411  bool operator!=(const void* pObject) const
412  {
413  return (m_pObject!=pObject);
414  }
415 
416  bool operator!=(const OdSmartPtr& pObject) const
417  {
418  return operator!=((void*)pObject.get());
419  }
420 };
421 
422 #include "TD_PackPop.h"
423 
424 #endif // _ODASMARTPOINTER_INCLUDED_
#define ODA_FAIL()
Definition: DebugStuff.h:65
OdRxObjMod
Definition: RxObject.h:63
OdRxObject * m_pObject
Definition: BaseObjectPtr.h:53
OdRxObject * get() const
Definition: BaseObjectPtr.h:65
virtual void release()=0
virtual OdRxClass * isA() const
virtual OdRxObject * queryX(const OdRxClass *pClass) const
virtual void addRef()=0
OdRxObject * get()
Definition: RxObject.h:522
OdSmartPtr & operator=(const OdSmartPtr &pObject)
Definition: SmartPtr.h:298
OdSmartPtr(const T *pObject, OdRxObjMod)
Definition: SmartPtr.h:158
void release()
Definition: SmartPtr.h:264
T * detach()
Definition: SmartPtr.h:283
OdSmartPtr(const T *pObject)
Definition: SmartPtr.h:163
OdSmartPtr(const OdSmartPtr &pObject)
Definition: SmartPtr.h:183
void assign(const T *pObject)
Definition: SmartPtr.h:80
T * operator->()
Definition: SmartPtr.h:352
OdSmartPtr(const OdBaseObjectPtr &pObject)
Definition: SmartPtr.h:195
OdSmartPtr()
Definition: SmartPtr.h:153
void attach(const T *pObject)
Definition: SmartPtr.h:215
bool operator!() const
Definition: SmartPtr.h:124
bool operator==(const OdSmartPtr &pObject) const
Definition: SmartPtr.h:406
void assign(const OdRxObject *pObject)
Definition: SmartPtr.h:110
const T * get() const
Definition: SmartPtr.h:326
OdSmartPtr(const OdRxObject *pObject)
Definition: SmartPtr.h:169
void attach(OdRxObject *pObject)
Definition: SmartPtr.h:235
void internalAddRef()
Definition: SmartPtr.h:63
bool operator!=(const void *pObject) const
Definition: SmartPtr.h:411
OdSmartPtr(const OdRxObjectPtr &pObject)
Definition: SmartPtr.h:189
bool operator==(const void *pObject) const
Definition: SmartPtr.h:401
bool operator!=(const OdSmartPtr &pObject) const
Definition: SmartPtr.h:416
void internalQueryX(const OdRxObject *pObject)
Definition: SmartPtr.h:98
OdSmartPtr(OdRxObject *pObject, OdRxObjMod)
Definition: SmartPtr.h:175
T * get()
Definition: SmartPtr.h:341
~OdSmartPtr()
Definition: SmartPtr.h:249