CFx SDK Documentation  2022 SP0
SmartClsIdPtr.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 
25 
26 
27 #ifndef _ODSMARTCLSIDPTR_INCLUDED_
28 #define _ODSMARTCLSIDPTR_INCLUDED_
29 
30 #include "TD_PackPush.h"
31 #include "SmartPtr.h"
32 
33 class OdRxObject;
34 
35 #define ODRX_DEFINE_CLSID_RETRIEVER(Class) \
36 class OdRxClsIdHolderFor_##Class\
37 {\
38 public:\
39  static inline const OdRxClass* classId()\
40  {\
41  OdRxClass* pClass = static_cast<OdRxClass*>(::odrxClassDictionary()->getAt(OD_T(#Class)).get());\
42  if(!pClass)\
43  throw OdError(eNotInitializedYet);\
44  return pClass;\
45  }\
46 }
47 
48 #define ODRX_DEFINE_CLSID_SMARTPTR(Class) \
49 ODRX_DEFINE_CLSID_RETRIEVER(Class);\
50 typedef OdClsIdSmartPtr<OdRxClsIdHolderFor_##Class, Class> Class##ClsIdPtr
51 
52 
59 template <class ClsIdRetriever, class T> class OdClsIdSmartPtr : public OdBaseObjectPtr
60 {
64  inline void internalAddRef() { if(m_pObject) { static_cast<T*>(m_pObject)->addRef(); } }
65 
77  inline void assign(const T* pObject)
78  {
79  release();
80  m_pObject = (OdRxObject*)pObject;
81  internalAddRef();
82  }
83 
92  inline void internalQueryX(const OdRxObject* pObject)
93  {
94  if(pObject)
95  {
96  OdRxObject* pX = pObject->queryX(ClsIdRetriever::classId());
97  if(pX)
98  m_pObject = pX;
99  else
101  }
102  }
103 
104  inline void assign(const OdRxObject* pObject)
105  {
106  release();
107  internalQueryX(pObject);
108  }
109 
110  // Note: Using of SmartPtr<T> as bool expression produce ambiguous call with some compilers.
111  // Use isNull() method instead.
112 
116  bool operator !() const { ODA_FAIL(); return false; }
117 
121  operator bool() const { ODA_FAIL(); return false; }
122 
126  operator bool() { ODA_FAIL(); return false; }
127 
128 public:
139  inline OdClsIdSmartPtr() { }
140 
141  inline OdClsIdSmartPtr(const T* pObject, OdRxObjMod) {m_pObject = (OdRxObject*)pObject; }
142 
143  inline OdClsIdSmartPtr(const T* pObject) {m_pObject = (OdRxObject*)pObject; internalAddRef(); }
144 
145  inline OdClsIdSmartPtr(const OdRxObject* pObject) { internalQueryX(pObject); }
146 
150  static inline const OdRxClass* classId()
151  {
152  return ClsIdRetriever::classId();
153  }
154 
156  {
157  internalQueryX(pObject);
158  if(pObject)
159  pObject->release();
160  }
161 
162  inline OdClsIdSmartPtr(const OdClsIdSmartPtr& pObject)
163  {
164  m_pObject = const_cast<T*>(static_cast<const T*>(pObject.get()));
165  internalAddRef();
166  }
167 
168  inline OdClsIdSmartPtr(const OdRxObjectPtr& pObject) { internalQueryX(pObject.get()); }
169 
170  inline OdClsIdSmartPtr(const OdBaseObjectPtr& pObject) { internalQueryX(pObject.get()); }
171 
172  static inline OdClsIdSmartPtr cast(const OdRxObject* pObject)
173  {
174  OdClsIdSmartPtr pRes;
175  if (pObject)
176  pRes.attach(static_cast<T*>(pObject->queryX(ClsIdRetriever::classId())));
177  return pRes;
178  }
179 
191  inline void attach(const T* pObject) { release(); m_pObject = const_cast<T*>(pObject); }
192 
204  inline void attach(OdRxObject* pObject)
205  {
206  release();
207  internalQueryX(pObject);
208  if(pObject)
209  pObject->release();
210  }
211 
218  inline ~OdClsIdSmartPtr() { release(); }
219 
228  inline void release()
229  {
230  if (m_pObject)
231  {
232  static_cast<T*>(m_pObject)->release();
233  m_pObject = 0;
234  }
235  }
236 
245  inline T* detach()
246  {
247  T* res = static_cast<T*>(m_pObject);
248  m_pObject = 0;
249  return res;
250  }
251 
253  { assign(pObject); return *this; }
254 
256  { assign(pObject.get()); return *this; }
257 
258  inline OdClsIdSmartPtr& operator = (const T* pObject)
259  { assign(pObject); return *this; }
260 
269  inline const T* get() const { return const_cast<T*>(static_cast<const T*>(m_pObject)); }
270 
279  inline T* get() { return static_cast<T*>(m_pObject); }
280 
287  inline T* operator ->() { return static_cast<T*>(m_pObject); }
288 
289  inline const T* operator ->() const { return const_cast<T*>(static_cast<const T*>(m_pObject)); }
290 
291 #ifdef ODA_GCC_2_95
300  inline operator T*() const { return static_cast<T*>(m_pObject); }
301 
302 #else
311  inline operator T*() { return static_cast<T*>(m_pObject); }
312 
319  inline operator const T*() const { return static_cast<const T*>(m_pObject); }
320 #endif
321 
322  inline bool operator==(const void* p) const { return (m_pObject==p); }
323 
324  inline bool operator==(const OdClsIdSmartPtr& ptr) const { return operator==((void*)ptr.get()); }
325 
326  inline bool operator!=(const void* p) const { return (m_pObject!=p); }
327 
328  inline bool operator!=(const OdClsIdSmartPtr& ptr) const { return operator!=((void*)ptr.get()); }
329 };
330 
331 #include "TD_PackPop.h"
332 
333 #endif //_ODSMARTCLSIDPTR_INCLUDED_
#define ODA_FAIL()
Definition: DebugStuff.h:65
@ eNotThatKindOfClass
OdRxObjMod
Definition: RxObject.h:56
OdRxObject * m_pObject
Definition: BaseObjectPtr.h:53
OdRxObject * get() const
Definition: BaseObjectPtr.h:65
OdClsIdSmartPtr(OdRxObject *pObject, OdRxObjMod)
OdClsIdSmartPtr(const T *pObject, OdRxObjMod)
static OdClsIdSmartPtr cast(const OdRxObject *pObject)
bool operator==(const void *p) const
OdClsIdSmartPtr(const OdRxObjectPtr &pObject)
OdClsIdSmartPtr(const OdClsIdSmartPtr &pObject)
OdClsIdSmartPtr(const OdRxObject *pObject)
void attach(OdRxObject *pObject)
OdClsIdSmartPtr(const T *pObject)
bool operator!=(const OdClsIdSmartPtr &ptr) const
void attach(const T *pObject)
OdClsIdSmartPtr & operator=(const OdClsIdSmartPtr &pObject)
bool operator!=(const void *p) const
bool operator==(const OdClsIdSmartPtr &ptr) const
static const OdRxClass * classId()
const T * get() const
OdClsIdSmartPtr(const OdBaseObjectPtr &pObject)
virtual void release()=0
virtual OdRxObject * queryX(const OdRxClass *pClass) const
OdRxObject * get()
Definition: RxObject.h:503