CFx SDK Documentation 2026 SP0
Loading...
Searching...
No Matches
SmartPtr.h
Go to the documentation of this file.
1
2// Copyright (C) 2002-2024, 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-2024 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)
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
116public:
131 {
132 }
133
134 OdSmartPtr(const T* pObject, OdRxObjMod)
135 : OdBaseObjectPtr(pObject)
136 {
137 }
138
139 OdSmartPtr(const T* pObject)
140 : OdBaseObjectPtr(pObject)
141 {
143 }
144
145 OdSmartPtr(const OdRxObject* pObject)
147 {
148 internalQueryX(pObject);
149 }
150
153 {
154 internalQueryX(pObject);
155 if(pObject)
156 pObject->release();
157 }
158
159 OdSmartPtr(const OdSmartPtr& pObject)
160 : OdBaseObjectPtr(pObject.get())
161 {
163 }
164
165 OdSmartPtr(const OdRxObjectPtr& pObject)
167 {
168 internalQueryX(pObject.get());
169 }
170
173 {
174 internalQueryX(pObject.get());
175 }
176
177//FELIX_CHANGE_BEGIN
178 OdSmartPtr( OdSmartPtr&& pObject ) noexcept : OdBaseObjectPtr( std::move( pObject ) )
179 {}
180//FELIX_CHANGE_END
181
196 void attach(const T* pObject)
197 {
198 release();
199 m_pObject = const_cast<T*>(pObject);
200 }
201
216 void attach(OdRxObject* pObject)
217 {
218 release();
219 internalQueryX(pObject);
220 if(pObject)
221 pObject->release();
222 }
223
231 {
232 release();
233 }
234
245 void release()
246 {
247 if (m_pObject)
248 {
249 m_pObject->release();
250 m_pObject = 0;
251 }
252 }
253
265 {
266 T* pRes = static_cast<T*>(m_pObject);
267 m_pObject = 0;
268 return pRes;
269 }
270
280 {
281 assign(pObject);
282 return *this;
283 }
284
285//FELIX_CHANGE_BEGIN
286 OdSmartPtr& operator = ( OdSmartPtr&& pObject ) noexcept
287 {
288 OdBaseObjectPtr::operator=( std::move( pObject ) );
289 return *this;
290 }
291//FELIX_CHANGE_END
292
294 {
295 assign(pObject.get());
296 return *this;
297 }
298
299 OdSmartPtr& operator = (const T* pObject)
300 {
301 assign(pObject);
302 return *this;
303 }
304
315 const T* get() const
316 {
317 return static_cast<const T*>(m_pObject);
318 }
319
330 T* get()
331 {
332 return static_cast<T*>(m_pObject);
333 }
334
342 {
343 return static_cast<T*>(m_pObject);
344 }
345
351 const T* operator ->() const
352 {
353 return static_cast<const T*>(m_pObject);
354 }
355
356#ifdef ODA_GCC_2_95
364 operator T*() const
365 {
366 return const_cast<T*>(static_cast<const T*>(m_pObject));
367 }
368
369#else
378 operator T*()
379 {
380 return static_cast<T*>(m_pObject);
381 }
382
383 operator const T*() const
384 {
385 return static_cast<const T*>(m_pObject);
386 }
387
388#endif
389
390 bool operator==(const void* pObject) const
391 {
392 return (m_pObject==pObject);
393 }
394
395 bool operator==(const OdSmartPtr& pObject) const
396 {
397 return operator==((void*)pObject.get());
398 }
399
400 bool operator!=(const void* pObject) const
401 {
402 return (m_pObject!=pObject);
403 }
404
405 bool operator!=(const OdSmartPtr& pObject) const
406 {
407 return operator!=((void*)pObject.get());
408 }
409
413 bool operator !() const { return m_pObject == nullptr; }
414
418 explicit operator bool() const { return m_pObject != nullptr; }
419};
420
421#include "TD_PackPop.h"
422
423#endif // _ODASMARTPOINTER_INCLUDED_
OdRxObjMod
Definition RxObject.h:56
OdRxObject * m_pObject
OdRxObject * get() const
OdBaseObjectPtr & operator=(OdBaseObjectPtr &&pObject) noexcept
virtual OdRxObject * queryX(const OdRxClass *pClass) const
virtual void release()=0
virtual OdRxClass * isA() const
OdRxObject * get()
Definition RxObject.h:503
T * operator->()
Definition SmartPtr.h:341
OdSmartPtr(const T *pObject, OdRxObjMod)
Definition SmartPtr.h:134
void release()
Definition SmartPtr.h:245
const OdDbObjectContextPE * get() const
Definition SmartPtr.h:315
OdSmartPtr(const T *pObject)
Definition SmartPtr.h:139
OdSmartPtr(const OdSmartPtr &pObject)
Definition SmartPtr.h:159
void assign(const T *pObject)
Definition SmartPtr.h:80
OdSmartPtr(const OdBaseObjectPtr &pObject)
Definition SmartPtr.h:171
void attach(const T *pObject)
Definition SmartPtr.h:196
bool operator!() const
Definition SmartPtr.h:413
bool operator==(const OdSmartPtr &pObject) const
Definition SmartPtr.h:395
void assign(const OdRxObject *pObject)
Definition SmartPtr.h:110
OdSmartPtr(OdSmartPtr &&pObject) noexcept
Definition SmartPtr.h:178
OdSmartPtr(const OdRxObject *pObject)
Definition SmartPtr.h:145
void attach(OdRxObject *pObject)
Definition SmartPtr.h:216
void internalAddRef()
Definition SmartPtr.h:63
bool operator!=(const void *pObject) const
Definition SmartPtr.h:400
OdSmartPtr(const OdRxObjectPtr &pObject)
Definition SmartPtr.h:165
bool operator==(const void *pObject) const
Definition SmartPtr.h:390
T * detach()
Definition SmartPtr.h:264
bool operator!=(const OdSmartPtr &pObject) const
Definition SmartPtr.h:405
void internalQueryX(const OdRxObject *pObject)
Definition SmartPtr.h:98
OdSmartPtr(OdRxObject *pObject, OdRxObjMod)
Definition SmartPtr.h:151
T * get()
Definition SmartPtr.h:330
OdSmartPtr & operator=(const OdSmartPtr &pObject)
Definition SmartPtr.h:279