CFx SDK Documentation  2020SP3
SharedPtr.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 #ifndef _SharedPtr_h_Included
26 #define _SharedPtr_h_Included
27 
28 #include "TD_PackPush.h"
29 
30 #include "OdaCommon.h"
31 #include "OdAlloc.h"
32 
48 template <class T> class OdSharedPtr
49 {
50 public:
51 
52  OdSharedPtr() : _reference(0), _refCount(0) {}
53 
54  OdSharedPtr(T* pObject) : _refCount(0)
55  {
56  if ((_reference = pObject) != 0)
57  {
58  _refCount = (int*)::odrxAlloc(sizeof(int*));
59  *_refCount = 1;
60  }
61  }
62 
66  OdSharedPtr(const OdSharedPtr& other)
67  {
68  _reference = other._reference;
69  _refCount = other._refCount;
70  if (_refCount)
71  {
72  ++*_refCount;
73  }
74  }
75 
83  {
84  if (_refCount && !--*_refCount)
85  {
86  delete _reference;
87  odrxFree(_refCount);
88  }
89  }
90 
92  {
93  if (_reference != other._reference)
94  {
95  if (_refCount && !--*_refCount)
96  {
97  odrxFree(_refCount);
98  delete _reference;
99  }
100 
101  _reference = other._reference;
102  _refCount = other._refCount;
103  if (_refCount)
104  {
105  ++*_refCount;
106  }
107  }
108 #ifdef _DEBUG
109  else if (_reference != 0)
110  {
111  // there can't be independent shared pointers on the same object
112  ODA_ASSERT_ONCE(_refCount == other._refCount);
113  // if the counters are the same - do nothing (the assignment is no-op)
114  }
115 #endif // _DEBUG
116  return *this;
117  }
118 
125  T* operator->() { return _reference; }
126 
134  T* get() { return _reference; }
135 
143  const T* get() const { return _reference; }
144 
145  operator T*() { return _reference; }
146  operator const T*() const { return _reference; }
147 
154  const T* operator->() const { return _reference; }
155 
156  T& operator*(){ return *_reference; }
157 
158  const T& operator*() const { return *_reference; }
159 
163  bool isNull() const {return _reference == 0;}
164 
170  T* detach()
171  {
172  if (!_refCount)
173  return 0;
174  if (*_refCount > 1)
175  {
176  ODA_FAIL();
177  }
178  odrxFree(_refCount);
179  _refCount = 0;
180  T* ret = _reference;
181  _reference = 0;
182  return ret;
183  }
187  int refCount() const
188  {
189  return _refCount ? *_refCount : 0;
190  }
191 private:
192  T* _reference;
193  int* _refCount;
194 };
195 
196 #include "TD_PackPop.h"
197 
198 #endif // _SharedPtr_h_Included
OdSharedPtr::detach
T * detach()
Definition: SharedPtr.h:170
OdSharedPtr::isNull
bool isNull() const
Definition: SharedPtr.h:163
OdSharedPtr
Definition: SharedPtr.h:49
ODA_ASSERT_ONCE
#define ODA_ASSERT_ONCE(exp)
Definition: DebugStuff.h:51
TD_PackPop.h
ODA_FAIL
#define ODA_FAIL()
Definition: DebugStuff.h:65
odrxFree
ALLOCDLL_EXPORT void odrxFree(void *pMemBlock)
OdSharedPtr::refCount
int refCount() const
Definition: SharedPtr.h:187
OdSharedPtr::operator*
T & operator*()
Definition: SharedPtr.h:156
OdSharedPtr::operator->
T * operator->()
Definition: SharedPtr.h:125
OdAlloc.h
OdSharedPtr::get
const T * get() const
Definition: SharedPtr.h:143
OdaCommon.h
TD_PackPush.h
OdSharedPtr::~OdSharedPtr
~OdSharedPtr()
Definition: SharedPtr.h:82
OdSharedPtr::operator->
const T * operator->() const
Definition: SharedPtr.h:154
odrxAlloc
ALLOCDLL_EXPORT void * odrxAlloc(size_t nBytes)
OdSharedPtr::OdSharedPtr
OdSharedPtr()
Definition: SharedPtr.h:52
OdSharedPtr::operator=
OdSharedPtr & operator=(const OdSharedPtr &other)
Definition: SharedPtr.h:91
OdSharedPtr::operator*
const T & operator*() const
Definition: SharedPtr.h:158
OdSharedPtr::OdSharedPtr
OdSharedPtr(T *pObject)
Definition: SharedPtr.h:54
OdSharedPtr::OdSharedPtr
OdSharedPtr(const OdSharedPtr &other)
Definition: SharedPtr.h:66
OdSharedPtr::get
T * get()
Definition: SharedPtr.h:134