File: cmtkSmartConstPtr.h

package info (click to toggle)
cmtk 3.3.1p2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,492 kB
  • sloc: cpp: 87,098; ansic: 23,347; sh: 3,896; xml: 1,551; perl: 707; makefile: 332
file content (242 lines) | stat: -rw-r--r-- 6,798 bytes parent folder | download | duplicates (5)
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
/*
//
//  Copyright 1997-2009 Torsten Rohlfing
//
//  Copyright 2004-2012 SRI International
//
//  This file is part of the Computational Morphometry Toolkit.
//
//  http://www.nitrc.org/projects/cmtk/
//
//  The Computational Morphometry Toolkit is free software: you can
//  redistribute it and/or modify it under the terms of the GNU General Public
//  License as published by the Free Software Foundation, either version 3 of
//  the License, or (at your option) any later version.
//
//  The Computational Morphometry Toolkit is distributed in the hope that it
//  will be useful, but WITHOUT ANY WARRANTY; without even the implied
//  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License along
//  with the Computational Morphometry Toolkit.  If not, see
//  <http://www.gnu.org/licenses/>.
//
//  $Revision: 5436 $
//
//  $LastChangedDate: 2018-12-10 19:01:20 -0800 (Mon, 10 Dec 2018) $
//
//  $LastChangedBy: torstenrohlfing $
//
*/

#ifndef __cmtkSmartConstPtr_h_included_
#define __cmtkSmartConstPtr_h_included_

#include <cmtkconfig.h>

#ifndef NULL
#  define NULL 0
#endif

#include <algorithm>
#include <cassert>

#include <System/cmtkSafeCounter.h>

namespace
cmtk
{

/** \addtogroup System */
//@{

/** Smart pointer with reference counting.
 */
template<class T>
class SmartConstPointer
{
public:
  /// This class instance.
  typedef SmartConstPointer<T> Self;

  /// The underlying raw pointer type.
  typedef const T* PointerType;

  /// Reference to static null object.
  static Self& Null()
  {
    static Self null;
    return null;
  }
  
  /// Get current reference counter value: use with caution!
  unsigned int GetReferenceCount() const 
  { 
    return this->m_ReferenceCount->Get(); 
  }

  /** Default constructor.
   * This needs to be separate because the constructor that takes a dumb pointer
   * must be explicit, but the default constructor should not be.
   */
  SmartConstPointer() 
    : m_ReferenceCount( new SafeCounter( 1 ) )
  { 
    this->m_Object.ptrConst = NULL;
  }
  
  /** Construct from dumb pointer.
   * Note that you MUST NEVER use this constructor more than once for each
   * dumb pointer, other than NULL!
   */
  explicit SmartConstPointer( T *const object ) 
    : m_ReferenceCount( new SafeCounter( 1 ) )
  { 
    this->m_Object.ptrConst = object;
  }

  /** Copy constructor template.
   * Increment reference counter in the process.
   */
  template<class T2>
  SmartConstPointer( const SmartConstPointer<T2>& ptr ) 
    : m_ReferenceCount( ptr.m_ReferenceCount )
  {
    this->m_Object.ptrConst = ptr.m_Object.ptrConst;
    this->m_ReferenceCount->Increment();
  }
  
  /** Copy constructor to prevent compiler-generated copy constructor.
   * Increment reference counter in the process.
   */
  SmartConstPointer( const Self& ptr ) 
    : m_ReferenceCount( ptr.m_ReferenceCount )
  {
    this->m_Object.ptrConst = ptr.m_Object.ptrConst;
    this->m_ReferenceCount->Increment();
  }
  
  /// Destruct: decrease reference pointer and free dumb pointer if necessary.
  ~SmartConstPointer() 
  {
    assert( this->m_ReferenceCount != NULL ); // we may have m_Object=NULL, but m_ReferenceCount should never be!
    if ( ! this->m_ReferenceCount->Decrement() ) 
      {
      delete this->m_ReferenceCount;
#ifdef DEBUG
      this->m_ReferenceCount = NULL;
#endif
      if ( this->m_Object.ptrConst ) 
	{
	delete this->m_Object.ptrConst;
#ifdef DEBUG
	this->m_Object.ptrConst = NULL;
#endif
	}
      }
  }

  /// Get dumb pointer.
  const T* GetPtr() const { return this->m_Object.ptrConst; }

  /// De-referencing operator (returns constant object).
  const T& operator*() const { return *this->m_Object.ptrConst; }

  /// De-referencing operator (returns constant object pointer).
  const T* operator->() const { return this->m_Object.ptrConst; }

  /// Implicit conversion to constant pointer.
  operator const T*() const { return m_Object.ptrConst; }
  
  /// Explicit conversion to constant pointer.
  const T* GetConstPtr() const { return this->m_Object.ptrConst; }

  /** Release control of this pointer.
   *\note This is a dangerous function. Be sure you know what you are doing!
   */
  const T* ReleasePtr() 
  { 
    const T* object = this->m_Object.ptrConst; 
    this->m_Object.ptrConst = NULL; 
    return object; 
  }

  /** Assignment operator.
   * This is implemented using the std::swap function.
   *\warning The "other" parameter HAS TO USE CALL BY VALUE for this function to work,
   *  because we are not creating an explicit copy of the original object before 
   *  calling Swap() (see Effective C++, 3rd, Item 11, p.56).
   *\warning Open question: given that we pass the parameter by value, not reference, does
   *  this really prevent the definition of a compiler-generated assignment (which passes
   *  parameter by reference)?
   */
  const Self& operator= ( const Self other ) const
  {
    using std::swap;
    swap( this->m_ReferenceCount, other.m_ReferenceCount );
    swap( this->m_Object.ptrConst, other.m_Object.ptrConst );

    return *this;
  }

  /** Equality operator using pointer.
   */
  bool operator== ( const Self& other ) const 
  {
    return (this->m_Object.ptrConst == other.m_Object.ptrConst);
  }
  
  /** Inequality operator.
   */
  bool operator!= ( const Self& other ) const 
  {
    return (this->m_Object.ptrConst != other.m_Object.ptrConst);
  }
  
  /** "Smaller than" operator (necessay for storing objects in std::map).
   */
  bool operator< ( const Self& other ) const 
  {
    return ( this->m_Object.ptrConst < other.m_Object.ptrConst );
  }
  
  ///Dynamic cast between smart pointer types.
  template<class T2> 
  static Self DynamicCastFrom( const T2& from_P )
  {
    return Self( dynamic_cast<typename Self::PointerType>( from_P.GetConstPtr() ), from_P.m_ReferenceCount );
  }

protected:
  /// Pointer to detached reference counter for this object.
  mutable SafeCounter* m_ReferenceCount;
  
  /// Union for const and non-const pointer for reference-counted object.
  mutable union
  {
    /// Pointer to const.
    const T* ptrConst;
    /// Pointer to non-const.
    T* ptr;
  } m_Object;
  
  /** Construct from dumb pointer and existing reference counter.
   * The reference counter is increased in the process.
   */
  SmartConstPointer( const T *const object, SafeCounter *const counter ) 
  {
    this->m_Object.ptrConst = object;
    this->m_ReferenceCount = counter;
    this->m_ReferenceCount->Increment();
  }
  
  /// Make all template instances friends for easy type casting.
  template<class T2> friend class SmartConstPointer;
};

//@}

} // namespace cmtk

#endif // define __cmtkSmartConstPtr_h_included_