CFx SDK Documentation 2024 SP0
Loading...
Searching...
No Matches
TPtr.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 __TPTR_H__
25#define __TPTR_H__
26
30template <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
41template <typename TObj>
43{
44 public:
45 static void addRef(TObj* pObj) {}
46 static void release(TObj* pObj) { delete pObj; }
47};
48
52template <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
74template <typename TObj, typename THelper = TObjRelease<TObj> >
75class 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 template<class U, typename UHelper> friend class TPtr;
112 template<class U> TPtr(const TPtr<U>& pOther) : m_obj(pOther.m_obj)
113 {
114 THelper::addRef(m_obj);
115 }
116 template<class U> TPtr& operator =(const TPtr<U> &other)
117 {
118 return replace(other.m_obj);
119 }
120 template<class U> TPtr<U> staticCast() const
121 {
122 return TPtr<U>(static_cast<U*>(m_obj));
123 }
124 template<class U> TPtr<U> dynamicCast() const
125 {
126 return TPtr<U>(dynamic_cast<U*>(m_obj));
127 }
128 const TObj* get() const
129 {
130 return m_obj;
131 }
132 TObj* get()
133 {
134 return m_obj;
135 }
137 {
139 return m_obj;
140 }
141 const TObj* operator ->() const
142 {
144 return m_obj;
145 }
146 operator TObj *()
147 {
148 return m_obj;
149 }
150 operator const TObj *() const
151 {
152 return m_obj;
153 }
154 TObj** getPtr()
155 {
156 THelper::release(m_obj);
157 m_obj = NULL;
158 return &m_obj; // return reference to NULL pointer
159 }
160 bool isNull() const
161 {
162 return !m_obj;
163 }
164 TObj *detach ()
165 {
166 TObj *pObj = m_obj;
167 m_obj = NULL;
168 return pObj;
169 }
170 TObj* attach(TObj* obj)
171 {
172 THelper::release(m_obj);
173 m_obj = obj;
174 return m_obj;
175 }
176};
177
178#endif // __TPTR_H__
#define ODA_ASSERT(exp)
Definition: DebugStuff.h:57
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
const TObj * get() const
Definition: TPtr.h:128
TPtr(const TPtr &ref)
Definition: TPtr.h:90
TPtr< U > staticCast() const
Definition: TPtr.h:120
TPtr & operator=(TObj *pObj)
Definition: TPtr.h:103
TObj * detach()
Definition: TPtr.h:164
TObj * operator->()
Definition: TPtr.h:136
TPtr()
Definition: TPtr.h:89
TObj * attach(TObj *obj)
Definition: TPtr.h:170
TObj * get()
Definition: TPtr.h:132
bool isNull() const
Definition: TPtr.h:160
~TPtr()
Definition: TPtr.h:99
TPtr< U > dynamicCast() const
Definition: TPtr.h:124
TPtr(const TPtr< U > &pOther)
Definition: TPtr.h:112
TPtr & replace(TObj *pObj)
Definition: TPtr.h:80
TObj * m_obj
Definition: TPtr.h:78
TObj ** getPtr()
Definition: TPtr.h:154
TPtr(const TObj *pObj, bool bAttach=false)
Definition: TPtr.h:94
GLenum GLint ref
Definition: gles2_ext.h:262