CFx SDK Documentation  2023 SP0
TPtr.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 __TPTR_H__
25 #define __TPTR_H__
26 
30 template <typename TObj>
32 {
33  public:
34  static void addRef(TObj* pObj) { if (pObj) pObj->addRef(); }
35  static void release(TObj* pObj) { if (pObj) pObj->release(); }
36 };
37 
41 template <typename TObj>
43 {
44  public:
45  static void addRef(TObj* pObj) {}
46  static void release(TObj* pObj) { delete pObj; }
47 };
48 
52 template <typename TObj>
54 {
55  public:
56  static void addRef(TObj* pObj) {}
57  static void release(TObj* pObj) { delete []pObj; }
58 };
59 
63 //template <typename TObj>
64 //class TObjFree
65 //{
66 // public:
67 // static void addRef(TObj* pObj) {}
68 // static void release(TObj* pObj) { ::odrxFree(pObj); }
69 //};
70 
74 template <typename TObj, typename THelper = TObjRelease<TObj> >
75 class TPtr
76 {
77  protected:
78  TObj *m_obj;
79 
80  TPtr &replace(TObj* pObj)
81  {
82  THelper::addRef(pObj);
83  THelper::release(m_obj);
84  m_obj = pObj;
85  return *this;
86  }
87 
88  public:
89  TPtr() : m_obj (NULL) {}
90  TPtr(const TPtr& ref) : m_obj (ref.m_obj)
91  {
92  THelper::addRef(m_obj);
93  }
94  TPtr(const TObj* pObj, bool bAttach = false) : m_obj(const_cast<TObj*>(pObj))
95  {
96  if (!bAttach)
97  THelper::addRef(m_obj);
98  }
100  {
101  THelper::release(m_obj);
102  }
103  TPtr &operator =(TObj *pObj)
104  {
105  return replace (pObj);
106  }
108  {
109  return replace (ref.m_obj);
110  }
111  const TObj* get() const
112  {
113  return m_obj;
114  }
115  TObj* get()
116  {
117  return m_obj;
118  }
119  TObj* operator ->()
120  {
121  ODA_ASSERT(m_obj);
122  return m_obj;
123  }
124  const TObj* operator ->() const
125  {
126  ODA_ASSERT(m_obj);
127  return m_obj;
128  }
129  operator TObj *()
130  {
131  return m_obj;
132  }
133  operator const TObj *() const
134  {
135  return m_obj;
136  }
137  TObj** getPtr()
138  {
139  THelper::release(m_obj);
140  m_obj = NULL;
141  return &m_obj; // return reference to NULL pointer
142  }
143  bool isNull() const
144  {
145  return !m_obj;
146  }
147  TObj *detach ()
148  {
149  TObj *pObj = m_obj;
150  m_obj = NULL;
151  return pObj;
152  }
153  TObj* attach(TObj* obj)
154  {
155  THelper::release(m_obj);
156  m_obj = obj;
157  return m_obj;
158  }
159 };
160 
161 #endif // __TPTR_H__
#define ODA_ASSERT(exp)
Definition: DebugStuff.h:49
#define NULL
Definition: GsProperties.h:177
static void release(TObj *pObj)
Definition: TPtr.h:57
static void addRef(TObj *pObj)
Definition: TPtr.h:56
static void release(TObj *pObj)
Definition: TPtr.h:46
static void addRef(TObj *pObj)
Definition: TPtr.h:45
static void release(TObj *pObj)
Definition: TPtr.h:35
static void addRef(TObj *pObj)
Definition: TPtr.h:34
Definition: TPtr.h:76
TPtr(const TPtr &ref)
Definition: TPtr.h:90
TObj * operator->()
Definition: TPtr.h:119
TObj * attach(TObj *obj)
Definition: TPtr.h:153
TPtr()
Definition: TPtr.h:89
TObj * get()
Definition: TPtr.h:115
bool isNull() const
Definition: TPtr.h:143
~TPtr()
Definition: TPtr.h:99
const TObj * get() const
Definition: TPtr.h:111
TPtr & operator=(TObj *pObj)
Definition: TPtr.h:103
TObj ** getPtr()
Definition: TPtr.h:137
TObj * m_obj
Definition: TPtr.h:78
TPtr & replace(TObj *pObj)
Definition: TPtr.h:80
TObj * detach()
Definition: TPtr.h:147
TPtr(const TObj *pObj, bool bAttach=false)
Definition: TPtr.h:94
GLenum GLint ref
Definition: gles2_ext.h:262