OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
pointer.h
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// Borland Class Library
3// Copyright (c) 1992, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Various types of smart pointer templatized classes
7//----------------------------------------------------------------------------
8
9#if !defined(OWL_POINTER_H)
10#define OWL_POINTER_H
11
12#include <owl/private/defs.h>
13#if defined(BI_HAS_PRAGMA_ONCE)
14# pragma once
15#endif
16
17#include <new>
18
19namespace owl {
20
21#include <owl/preclass.h>
22
23//
24// A pair of smart pointer template classes. Provides basic conversion
25// operator to T*, as well as dereferencing (*), and 0-checking (!).
26// These classes assume that they alone are responsible for deleting the
27// object or array unless Relinquish() is called.
28//
29
30/// \addtogroup utility
31/// @{
32
33//
34/// \class TPointerBase
35// ~~~~~ ~~~~~~~~~~~~
36/// Smart pointer base class. Provides basic conversion operator to T*, as well as
37/// dereferencing (*), and 0-checking (!). This and derived classes assume that they alone are
38/// responsible for deleting the object or array unless Relinquish() is called.
39//
40template<class T> class TPointerBase {
41 public:
42 T& operator *() {return *P;}
43 operator T*() {return P;}
44 int operator !() const {return P==nullptr;}
45 T* Relinquish() {T* p = P; P = nullptr; return p;}
46
47 protected:
48 TPointerBase(T* pointer) : P(pointer) {}
50
51 T* P;
52
53 private:
54 void* operator new(size_t) noexcept {return nullptr;} // prohibit use of new
55#if !defined(BI_COMP_GNUC)
56 void operator delete(void* p) {(static_cast<TPointerBase<T>*>(p))->P=0;}
57#endif
58
59
60#if !defined(OWL5_COMPAT)
61 // Prohibit copying.
62 //
64 TPointerBase& operator=(const TPointerBase&);
65#endif
66};
67
68//
69/// \class TPointer
70// ~~~~~ ~~~~~~~~
71/// Smart pointer to a single object. Provides member access operator ->
72//
73template<class T> class TPointer : public TPointerBase<T> {
74 public:
76
77#if !defined(OWL5_COMPAT)
78 explicit
79#endif
80 TPointer(T* pointer) : TPointerBase<T>(pointer) {}
81
82 ~TPointer() {delete this->P;}
83
85 {
86 if (src == this->P) return *this; // same-pointer assignment
87
88 delete this->P;
89 this->P = src;
90 return *this;
91 }
92
93 T* operator->() {return this->P;} // Could throw exception if P==0
94
95};
96
97//
98/// \class TAPointer
99// ~~~~~ ~~~~~~~~~
100/// Pointer to an array of type T. Provides an array subscript operator and uses
101/// array delete[]
102//
103template<class T> class TAPointer : public TPointerBase<T> {
104 public:
106
107#if !defined(OWL5_COMPAT)
108 explicit
109#endif
110 TAPointer(T array[]) : TPointerBase<T>(array) {}
111
112 ~TAPointer() {delete[] this->P;}
113
115 {
116 if (src == this->P) return *this; // same-pointer assignment
117
118 delete[] this->P;
119 this->P = src;
120 return *this;
121 }
122
123 T& operator[](int i) {return this->P[i];} // Could throw exception if P==0
124 T& operator[](size_t i) {return this->P[i];} // Could throw exception if P==0
125
126};
127
128//----------------------------------------------------------------------------
129
130//
131/// \class TEnvelope
132// ~~~~~ ~~~~~~~~~
133/// Envelope-letter type of smart pointers. In this implementation the envelope
134/// acts as a smart pointer to a reference-counted internal letter. This allows
135/// a lot of flexibility & safety in working with a single object (associated
136/// with the letter) being assigned to numerous envelopes in many scopes. An 'A'
137/// version is provided for use with arrays.
138///
139/// Use like:
140/// \code
141/// TEnvelope<T> e1 = new T(x,y,x); // e1 now owns a T in a letter
142/// e1->Func(); // invokes Func() on the new T
143/// Func(*e1); // passing a T& or a T this way
144/// TEnvelope<T> e2 = e1; // e2 safely shares the letter with e1
145///
146/// TAEnvelope<T> e1 = new T[99]; // e1 now owns T[] in a letter
147/// e1[i].Func(); // invokes Func() on a T element
148/// Func(e1[i]); // passing a T& or a T this way
149/// TAEnvelope<T> e2 = e1; // e2 safely shares the letter with e1
150/// \endcode
151
152//
153template<class T> class TEnvelope {
154 public:
155 TEnvelope(T* object) : Letter(new TLetter(object)) {}
156 TEnvelope(const TEnvelope& src) : Letter(src.Letter) {Letter->AddRef();}
157 ~TEnvelope() {Letter->Release();}
158
160 TEnvelope& operator =(T* object);
161
162 T* operator->() { return Letter->Object; }
163 T& operator *() { return *Letter->Object; }
164
165 private:
166 struct TLetter {
167 TLetter(T* object) : Object(object), RefCount(1) {}
168 ~TLetter() { delete Object; }
169
170 void AddRef() { RefCount++; }
171 void Release() { if (--RefCount == 0) delete this; }
172
173 T* Object;
174 int RefCount;
175 };
176 TLetter* Letter;
177};
178
179//
180//
181//
183 public:
184 TVoidPointer( const void *p = nullptr ) : Ptr(p) {}
185 TVoidPointer( const TVoidPointer& rp ) : Ptr(rp.Ptr) {}
186
188 { Ptr = vp.Ptr; return *this; }
189 const TVoidPointer& operator = ( const void *p )
190 { Ptr = p; return *this; }
191 operator void *() const { return CONST_CAST(void *,Ptr); }
192
193 private:
194 const void *Ptr;
195};
196
197
198//
199/// \class TComRefBase
200// ~~~~~ ~~~~~~~~~~~
201/// Encapsulation of OLE interface pointers
202//
203template<class T> class TComRefBase {
204 public:
205 operator T*() {return I;}
206 operator T**() {Clear(); return &I;}
207 int operator !() const {return I==nullptr;}
208 void operator delete(void* p) {(static_cast<TComRefBase<T>*>(p))->I=0;}
209 protected:
210 TComRefBase(const TComRefBase<T>& i) : I(i.I) {if (I) I->AddRef();}
211 TComRefBase(T* i) : I(i) {}
214
215 void Clear() {if(I){I->Release(); I = nullptr;}}
216 T* I;
217 private:
218
219 // Prohibit use of operator new
220 //
221 void* operator new(size_t) noexcept {return nullptr;}
222};
223
224template<class T> class TComRef : public TComRefBase<T> {
225 public:
229
231 {
232 if (iface == this->I) return *this; // same-interface assignment
233
234 this->Clear();
235 this->I = iface;
236 return *this;
237 }
238
240 {
241 if (&i == this) return *this; // self-assignment
242
243 this->Clear();
244 if (i.I)
245 {
246 this->I = i.I;
247 this->I->AddRef();
248 }
249 return *this;
250 }
251
252 T* operator->() {return this->I;} // Could throw exception if I==0
253 T* operator->() const {return this->I;} // Could throw exception if I==0
254 //void** operator&() {this->Clear(); return (void**)&I;}
255};
256
257/// @}
258
259#include <owl/posclass.h>
260
261template<class T>
263{
264 if (&src == this) return *this; // self-assignment
265
266 Letter->Release();
267 Letter = src.Letter;
268 Letter->AddRef();
269 return *this;
270}
271
272template<class T>
274{
275 if (object == Letter->Object) return *this; // same-pointer assignment
276
277 Letter->Release();
278 Letter = new TLetter(object); // Assumes non-null! Use with new
279 return *this;
280}
281
282//
283/// \class TAEnvelope
284// ~~~~~ ~~~~~~~~~~
285/// Envelope-letter type of smart pointer array. In this implementation the envelope
286/// acts as a smart pointer to a reference-counted internal letter. This allows a
287/// lot of flexibility & safety in working with a single object (associated with the
288/// letter) being assigned to numerous envelopes in many scopes.
289///
290/// Use:
291/// \code
292/// TAEnvelope<T> e1 = new T[99]; // e1 now owns T[] in a letter
293/// e1[i].Func(); // invokes Func() on a T element
294/// Func(e1[i]); // passing a T& or a T this way
295/// TAEnvelope<T> e2 = e1; // e2 safely shares the letter with e1
296/// \endcode
297template<class T> class TAEnvelope {
298 public:
299 TAEnvelope(T array[]) : Letter(new TLetter(array)) {}
300 TAEnvelope(const TAEnvelope& src) : Letter(src.Letter) {Letter->AddRef();}
301 ~TAEnvelope() { Letter->Release(); }
302
304 TAEnvelope& operator =(T array[]);
305
306 T& operator[](int i) { return Letter->Array[i]; }
307 T* operator *() { return Letter->Array; }
308
309 private:
310 struct TLetter {
311 TLetter(T array[]) : Array(array), RefCount(1) {}
312 ~TLetter() { delete[] Array; }
313
314 void AddRef() { RefCount++; }
315 void Release() { if (--RefCount == 0) delete this; }
316
317 T* Array;
318 int RefCount;
319 };
320 TLetter* Letter;
321};
322
323template<class T>
325{
326 if (&src == this) return *this; // self-assignment
327
328 Letter->Release();
329 Letter = src.Letter;
330 Letter->AddRef();
331 return *this;
332}
333
334template<class T>
336{
337 if (array == Letter->Array) return *this; // same-pointer assignment
338
339 Letter->Release();
340 Letter = new TLetter(array); // Assumes non-null! Use with new
341 return *this;
342}
343
344} // OWL namespace
345
346#endif // OWL_POINTER_H
Envelope-letter type of smart pointer array.
Definition pointer.h:297
TAEnvelope(T array[])
Definition pointer.h:299
T & operator[](int i)
Definition pointer.h:306
TAEnvelope & operator=(const TAEnvelope &src)
Definition pointer.h:324
TAEnvelope(const TAEnvelope &src)
Definition pointer.h:300
T * operator*()
Definition pointer.h:307
Pointer to an array of type T.
Definition pointer.h:103
T & operator[](size_t i)
Definition pointer.h:124
TAPointer(T array[])
Definition pointer.h:110
TAPointer< T > & operator=(T *src)
Definition pointer.h:114
T & operator[](int i)
Definition pointer.h:123
Encapsulation of OLE interface pointers.
Definition pointer.h:203
int operator!() const
Definition pointer.h:207
TComRefBase(T *i)
Definition pointer.h:211
TComRefBase(const TComRefBase< T > &i)
Definition pointer.h:210
TComRef(T *iface)
Definition pointer.h:227
T * operator->() const
Definition pointer.h:253
TComRef< T > & operator=(T *iface)
Definition pointer.h:230
T * operator->()
Definition pointer.h:252
TComRef(const TComRef< T > &i)
Definition pointer.h:228
Envelope-letter type of smart pointers.
Definition pointer.h:153
TEnvelope & operator=(const TEnvelope &src)
Definition pointer.h:262
T & operator*()
Definition pointer.h:163
TEnvelope(const TEnvelope &src)
Definition pointer.h:156
T * operator->()
Definition pointer.h:162
TEnvelope(T *object)
Definition pointer.h:155
Smart pointer base class.
Definition pointer.h:40
TPointerBase(T *pointer)
Definition pointer.h:48
int operator!() const
Definition pointer.h:44
Smart pointer to a single object. Provides member access operator ->
Definition pointer.h:73
T * operator->()
Definition pointer.h:93
TPointer(T *pointer)
Definition pointer.h:80
TPointer< T > & operator=(T *src)
Definition pointer.h:84
const TVoidPointer & operator=(const TVoidPointer &vp)
Definition pointer.h:187
TVoidPointer(const TVoidPointer &rp)
Definition pointer.h:185
TVoidPointer(const void *p=nullptr)
Definition pointer.h:184
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
#define CONST_CAST(targetType, object)
Definition defs.h:273