CFx SDK Documentation 2024 SP0
Loading...
Searching...
No Matches
SmartPtr.h
Go to the documentation of this file.
1
2// Copyright (C) 2002-2022, 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 Open Design Alliance software pursuant to a license
16// agreement with Open Design Alliance.
17// Open Design Alliance Copyright (C) 2002-2022 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
57template <class T> class OdSmartPtr : public OdBaseObjectPtr
58{
59protected:
64 {
65 if(m_pObject)
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
140public:
155 {
156 }
157
158 OdSmartPtr(const T* pObject, OdRxObjMod)
159 : OdBaseObjectPtr(pObject)
160 {
161 }
162
163 OdSmartPtr(const T* pObject)
164 : OdBaseObjectPtr(pObject)
165 {
167 }
168
169 OdSmartPtr(const OdRxObject* pObject)
171 {
172 internalQueryX(pObject);
173 }
174
177 {
178 internalQueryX(pObject);
179 if(pObject)
180 pObject->release();
181 }
182
183 OdSmartPtr(const OdSmartPtr& pObject)
184 : OdBaseObjectPtr(pObject.get())
185 {
187 }
188
189 OdSmartPtr(const OdRxObjectPtr& pObject)
191 {
192 internalQueryX(pObject.get());
193 }
194
197 {
198 internalQueryX(pObject.get());
199 }
200
201//FELIX_CHANGE_BEGIN
202 OdSmartPtr( OdSmartPtr&& pObject ) noexcept : OdBaseObjectPtr( std::move( pObject ) )
203 {}
204//FELIX_CHANGE_END
205
220 void attach(const T* pObject)
221 {
222 release();
223 m_pObject = const_cast<T*>(pObject);
224 }
225
240 void attach(OdRxObject* pObject)
241 {
242 release();
243 internalQueryX(pObject);
244 if(pObject)
245 pObject->release();
246 }
247
255 {
256 release();
257 }
258
269 void release()
270 {
271 if (m_pObject)
272 {
274 m_pObject = 0;
275 }
276 }
277
289 {
290 T* pRes = static_cast<T*>(m_pObject);
291 m_pObject = 0;
292 return pRes;
293 }
294
304 {
305 assign(pObject);
306 return *this;
307 }
308
309//FELIX_CHANGE_BEGIN
310 OdSmartPtr& operator = ( OdSmartPtr&& pObject ) noexcept
311 {
312 OdBaseObjectPtr::operator=( std::move( pObject ) );
313 return *this;
314 }
315//FELIX_CHANGE_END
316
318 {
319 assign(pObject.get());
320 return *this;
321 }
322
323 OdSmartPtr& operator = (const T* pObject)
324 {
325 assign(pObject);
326 return *this;
327 }
328
339 const T* get() const
340 {
341 return static_cast<const T*>(m_pObject);
342 }
343
354 T* get()
355 {
356 return static_cast<T*>(m_pObject);
357 }
358
366 {
367 return static_cast<T*>(m_pObject);
368 }
375 const T* operator ->() const
376 {
377 return static_cast<const T*>(m_pObject);
378 }
379
380#ifdef ODA_GCC_2_95
388 operator T*() const
389 {
390 return const_cast<T*>(static_cast<const T*>(m_pObject));
391 }
392
393#else
402 operator T*()
403 {
404 return static_cast<T*>(m_pObject);
405 }
406
407 operator const T*() const
408 {
409 return static_cast<const T*>(m_pObject);
410 }
411
412#endif
413
414 bool operator==(const void* pObject) const
415 {
416 return (m_pObject==pObject);
417 }
418
419 bool operator==(const OdSmartPtr& pObject) const
420 {
421 return operator==((void*)pObject.get());
422 }
423
424 bool operator!=(const void* pObject) const
425 {
426 return (m_pObject!=pObject);
427 }
428
429 bool operator!=(const OdSmartPtr& pObject) const
430 {
431 return operator!=((void*)pObject.get());
432 }
433};
434
435#include "TD_PackPop.h"
436
437#endif // _ODASMARTPOINTER_INCLUDED_
#define ODA_FAIL()
Definition: DebugStuff.h:88
OdRxObjMod
Definition: RxObject.h:56
OdRxObject * m_pObject
Definition: BaseObjectPtr.h:54
OdRxObject * get() const
Definition: BaseObjectPtr.h:81
OdBaseObjectPtr & operator=(OdBaseObjectPtr &&pObject) noexcept
Definition: BaseObjectPtr.h:66
virtual OdRxObject * queryX(const OdRxClass *pClass) const
virtual void release()=0
virtual OdRxClass * isA() const
virtual void addRef()=0
OdRxObject * get()
Definition: RxObject.h:503
T * operator->()
Definition: SmartPtr.h:365
OdSmartPtr(const T *pObject, OdRxObjMod)
Definition: SmartPtr.h:158
void release()
Definition: SmartPtr.h:269
const T * get() const
Definition: SmartPtr.h:339
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
OdSmartPtr(const OdBaseObjectPtr &pObject)
Definition: SmartPtr.h:195
OdSmartPtr()
Definition: SmartPtr.h:153
void attach(const T *pObject)
Definition: SmartPtr.h:220
bool operator!() const
Definition: SmartPtr.h:124
bool operator==(const OdSmartPtr &pObject) const
Definition: SmartPtr.h:419
void assign(const OdRxObject *pObject)
Definition: SmartPtr.h:110
OdSmartPtr(OdSmartPtr &&pObject) noexcept
Definition: SmartPtr.h:202
OdSmartPtr(const OdRxObject *pObject)
Definition: SmartPtr.h:169
void attach(OdRxObject *pObject)
Definition: SmartPtr.h:240
void internalAddRef()
Definition: SmartPtr.h:63
bool operator!=(const void *pObject) const
Definition: SmartPtr.h:424
OdSmartPtr(const OdRxObjectPtr &pObject)
Definition: SmartPtr.h:189
bool operator==(const void *pObject) const
Definition: SmartPtr.h:414
T * detach()
Definition: SmartPtr.h:288
bool operator!=(const OdSmartPtr &pObject) const
Definition: SmartPtr.h:429
void internalQueryX(const OdRxObject *pObject)
Definition: SmartPtr.h:98
OdSmartPtr(OdRxObject *pObject, OdRxObjMod)
Definition: SmartPtr.h:175
T * get()
Definition: SmartPtr.h:354
OdSmartPtr & operator=(const OdSmartPtr &pObject)
Definition: SmartPtr.h:303
~OdSmartPtr()
Definition: SmartPtr.h:254