summaryrefslogtreecommitdiff
path: root/STL/ptr.h
blob: ad0fe736795ba873cf7471632865a552acf89b69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//*****************************************************************************
//
// Class Ptr
//
// Ptr is a proxy for a pointer to an object.  This class provides automatic
// deletion of objects allocated on the heap.  Objects are viewed via the
// ->, *, and (itemClass *) operators.  Since these operators are inline, there
// is no performance penalty for using this proxy instead of a real pointer.
//
// You can assign multiple pointers to one Ptr object.  If you
// do, the old object will be destroyed automatically.
//
// Use suggestions:
//  1. To clean up pointers returned by functions or methods that  
//     return pointers and expect the caller to delete the pointer.
//  2. To make sure an object gets deleted.  Using Ptr saves you
//     the hassle of writing an exception handler for this purpose.
//  3. Part of a composite object (assembly-part).
//     This class is a useful substitute when pointers are needed,
//     because object clean-up is fully encapsulated.
//     The following are cases where pointers would normally be used.
//
//     For declaring class data members that may or
//     may not be instantiated (1 to 0-or-1 relationship). 
//
//     To instantiate a data member only when it is really needed, 
//     reducing average memory consumption during execution.
//
//     MFC and thread-local storage -- some MFC objects can only be
//     accessed by the thread that created them.  MFC sub-component
//     objects cannot be instantiated along with its container object
//     if the container object is created in one thread and the MFC 
//     object is accessed in another.
//
//     When a 'part' class does not have a default constructor.
//     There are two solutions provided by C++ for this situation.
//     The designer could use a pointer to the object instead, 
//     or the designer could alter the assembly class's constructor 
//     to accept parameters that are passed on to the 'part' class's
//     constructor.
//
// See Design Patterns - Proxy pattern, and the Iterator pattern (pg 266).
//
// Template Parameters:
//   itemClass: The class of the underlying object (the 'pointed to' object).
//
//*****************************************************************************

template <class itemClass>
class Ptr
{
  public:

    // Construction
    Ptr( itemClass *pItem = NULL );
    Ptr( const Ptr& );

    // Destruction
    ~Ptr();
    
    // Access to 'real' object
    inline itemClass * operator -> () const;
    inline operator itemClass * () const;
    inline itemClass& operator * () const;

    // Assignment
    Ptr& operator = ( const Ptr& );
    Ptr& operator = ( itemClass *pItem );
    itemClass *& GetPtrRef();
    
    // Other
    void Disown();

  private:

    itemClass *m_pObject;
};

//*****************************************************************************
//
// Ptr default constructor
//
// DESCRIPTION:
//
//      Constructor. 
//
// INPUT PARAMETERS:
//      pItem - Pointer to the object to point to.  pItem is deleted
//              when 'this' is deleted.
//
//*****************************************************************************

template <class itemClass>
Ptr<itemClass>::Ptr<itemClass>( itemClass *pItem )
  : m_pObject( pItem )
{
}

//*****************************************************************************
//
// Ptr::Ptr (Ptr&)
//
// DESCRIPTION:
//
//      Copy constructor. To avoid deleting 'rCopy's object 
//      twice, 'rCopy' will not point to any object after this method completes. 
//      rCopy is not really const, but it is declared 'const' since
//      this allows putting Ptr's in STL containers.
//
// INPUT PARAMETERS:
//      rCopy - A reference to the Ptr to copy.
//
// OUTPUT PARAMETERS:
//      rCopy - No longer points to anything.
//
//*****************************************************************************

template <class itemClass>
Ptr<itemClass>::Ptr<itemClass>( const Ptr<itemClass>& rCopy )
{
  m_pObject = rCopy.m_pObject;
  
  // rCopy no longer owns the object.
  const_cast< Ptr<itemClass>& >(rCopy).m_pObject = NULL;
}

//*****************************************************************************
//
// Ptr::~Ptr
//
// DESCRIPTION:
//
//      Destroys the object that is being pointed to (if any).
//
//*****************************************************************************

template <class itemClass>
Ptr<itemClass>::~Ptr<itemClass>()
{
  delete m_pObject; // delete NULL OK
}

//*****************************************************************************
//
// Ptr::GetPtrRef
//
// DESCRIPTION:
//      
//      Returns a reference to an internal pointer.  This allows these 
//      objects to be sent to functions that accept pointers to pointers
//      or references to pointers as output arguments.  Typically, this
//      is how functions return multiple objects. 
// 
//      This is not const because this method is ultimately intended
//      for changing the object's value.
//
// RETURNS:
//      A reference to the 'pointer' data member of this object.
//
//*****************************************************************************

template <class itemClass>
itemClass *& Ptr<itemClass>::GetPtrRef()
{ 
  return m_pObject; 
}

//*****************************************************************************
//
// Ptr::operator ->
//
// DESCRIPTION:
//      
//      Provides access to the interface of the underlying object.
//
// RETURNS:
//      Nothing callers can really use - only the compiler can use it.
//      This method is part of the pointer to member operator (-> *).
//
//*****************************************************************************

template <class itemClass>
itemClass * Ptr<itemClass>::operator -> () const
{ 
  return m_pObject; 
}

//*****************************************************************************
//
// Ptr::operator itemClass * 
//
// DESCRIPTION:
//      
//      Provides access to the underlying object.
//
// RETURNS:
//      Pointer to the object that is being pointed to.
//
//*****************************************************************************

template <class itemClass>
Ptr<itemClass>::operator itemClass * () const
{ 
  return m_pObject; 
}

//*****************************************************************************
//
// Ptr::operator * 
//
// DESCRIPTION:
//      
//      Provides access to the underlying object.
//
// RETURNS:
//      Reference to the object that is being pointed to.
//
//*****************************************************************************

template <class itemClass>
itemClass& Ptr<itemClass>::operator * () const
{ 
  return *m_pObject; 
}

//*****************************************************************************
//
// Ptr::operator = (Ptr&)
//
// DESCRIPTION:
//
//      For assigning one Ptr to another.  Deletes the object that 'this' 
//      is pointing to and makes 'this' point to the object that 'rCopy'
//      is pointing to.  To avoid deleting 'rCopy's object twice, 'rCopy'
//      will not point to any object after this method completes. 
//
//      Although the parameter rCopy can be arugably not "const", it
//      must be "const" since the compiler will complain in certain
//      cases with warning C4270. 
//
// INPUT PARAMETERS:
//      rCopy - A reference to the Ptr to copy.
//
// OUTPUT PARAMETERS:
//      rCopy - No longer points to anything.
//
// RETURNS:
//      A reference to 'this'.
//
//*****************************************************************************

template <class itemClass>
Ptr<itemClass>&
Ptr<itemClass>::operator = ( const Ptr<itemClass>& rCopy )
{
  // Check for A = A 
  if ( &rCopy == this )
  {
    return *this;
  } 
  
  // Save current pointer so we can delete it after
  // doing everything else
  itemClass *pOldObject = m_pObject;
  
  // Get the pointer out of rCopy
  m_pObject = rCopy.m_pObject;
    
  // rCopy no longer owns the object.
  const_cast< Ptr<itemClass>& >(rCopy).m_pObject = NULL;
  
  // This might generate an exception.  But, we won't
  // introduce a memory leak because 'this' now
  // owns rCopy's pointer.
  delete pOldObject; // delete NULL OK
  
  return *this;
}

//*****************************************************************************
//
// Ptr::operator =
//
// DESCRIPTION:
//
//      Changes the underlying object.  If the proxy currently has an
//      underlying object, then it is deleted.
//
// INPUT PARAMETERS:
//      pItem - Reference to the new object.  Can be NULL.
//
// RETURNS:
//      Reference to 'this'.
//
//*****************************************************************************

template <class itemClass>
Ptr<itemClass>& Ptr<itemClass>::operator = ( itemClass *pItem )
{
  if ( m_pObject == pItem )
  {
    return *this;
  }
  
  // Save current pointer so we can delete it after
  // doing everything else
  itemClass *pOldObject = m_pObject;
  
  m_pObject = pItem;
  
  // This might generate an exception.  But, we won't
  // introduce a memory leak because 'this' now
  // owns pItem. 
  delete pOldObject; // delete NULL OK
  
  return *this;
}

//*****************************************************************************
//
// Ptr::Disown 
//
// DESCRIPTION:
//
//      Ptr objects delete their pointed-to objects when they go out
//      of scope.  Calling Disown() causes the Ptr object to 'forget' 
//      that it is currently pointing to an object.
//
// RETURNS:
//      Doesn't return anything because returning a value such as a pointer
//      to the item encourages misuse leading to memory leaks.
// 
//*****************************************************************************
template <class itemClass>
void Ptr<itemClass>::Disown()
{
  m_pObject = NULL;
}