File: itkAutoPointer.h

package info (click to toggle)
insighttoolkit5 5.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704,384 kB
  • sloc: cpp: 783,592; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 464; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (255 lines) | stat: -rw-r--r-- 6,649 bytes parent folder | download
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
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         https://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#ifndef itkAutoPointer_h
#define itkAutoPointer_h

#include "itkMacro.h"
#include <iostream>

namespace itk
{
/** \class AutoPointer
 * \brief Implements an Automatic Pointer to an object.
 *
 * AutoPointer is intended to facilitate the construction of
 * objects on the fly for those objects that are not to be shared.
 * An AutoPointer destroys its object when it goes out of scope.
 * Ownership of the object is transferred from one AutoPointer
 * to another AutoPointer when the assignment operator is used.
 * An AutoPointer can release ownership of the object it holds.
 *
 * This class follows the design of the std::unique_ptr (auto_ptr being
 * deprecated in C++11) class. The main * reason for not using the std
 * version is to avoid templated methods, which greatly increase the
 * difficulty of wrapping for Tcl, Python and Java.
 *
 * \ingroup ITKSystemObjects
 * \ingroup DataAccess
 * \ingroup ITKCommon
 */
template <typename TObjectType>
class AutoPointer
{
public:
  /** Extract information from template parameter. */
  using ObjectType = TObjectType;
  using Self = AutoPointer;

  /** Constructor.  */
  AutoPointer()
    : m_Pointer(nullptr)
  {}

  /** Copy constructor.  */
  explicit AutoPointer(AutoPointer & p)
  {
    m_IsOwner = p.IsOwner();          // Ownership can only be taken from
                                      // another owner
    m_Pointer = p.ReleaseOwnership(); // release ownership if appropriate
  }

  /** Constructor to pointer p.  */
  explicit AutoPointer(ObjectType * p, bool takeOwnership)
    : m_Pointer(p)
    , m_IsOwner(takeOwnership)
  {}

  /** Destructor.  */
  ~AutoPointer() { this->Reset(); }

  /** Overload operator ->.  */
  ObjectType * operator->() const { return m_Pointer; }

  /** Clear the AutoPointer. If it had a pointer the object
      is deleted and the pointer is set to null. */
  void
  Reset()
  {
    if (m_IsOwner)
    {
      delete m_Pointer;
    }
    m_Pointer = nullptr;
    m_IsOwner = false;
  }

  /** Explicitly set the ownership */
  void
  TakeOwnership()
  {
    m_IsOwner = true;
  }

  /** Explicitly set the ownership */
  void
  TakeOwnership(ObjectType * objectptr)
  {
    if (m_IsOwner)
    {
      delete m_Pointer; // remove the current one
    }
    m_Pointer = objectptr;
    m_IsOwner = true;
  }

  /** Explicitly reject ownership */
  void
  TakeNoOwnership(ObjectType * objectptr)
  {
    if (m_IsOwner)
    {
      delete m_Pointer; // remove the current one
    }
    m_Pointer = objectptr;
    m_IsOwner = false;
  }

  /** Query for the ownership */
  bool
  IsOwner() const
  {
    return m_IsOwner;
  }

  /** Release the pointer hold by the current AutoPointer
   *  and return the raw pointer so it can be hold by
   *  another AutoPointer. This operation is intended to
   *  be used for facilitating polymorphism.
   *
   *  Example: if class Cow derives from Mammal,
   *  AutoPointer<Cow> onecow = new Cow;
   *  AutoPointer<Mammal> onemammal = onecow.ReleaseOwnership();
   *
   *  Note that the AutoPointer still points to the object after the
   *  ReleaseOwnership operation, but it doesn't own the object any
   *  more. */
  ObjectType *
  ReleaseOwnership()
  {
    m_IsOwner = false;
    return m_Pointer;
  }

  /** Access function to pointer. */
  ObjectType *
  GetPointer() const
  {
    return m_Pointer;
  }

  /** Comparison of pointers. Equal comparison.  */
  bool
  operator==(const AutoPointer & r) const
  {
    return (void *)m_Pointer == (void *)r.m_Pointer;
  }

  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self);

  /** Comparison of pointers. Less than comparison.  */
  bool
  operator<(const AutoPointer & r) const
  {
    return (void *)m_Pointer < (void *)r.m_Pointer;
  }

  /** Comparison of pointers. Greater than comparison.  */
  bool
  operator>(const AutoPointer & r) const
  {
    return (void *)m_Pointer > (void *)r.m_Pointer;
  }

  /** Comparison of pointers. Less than or equal to comparison.  */
  bool
  operator<=(const AutoPointer & r) const
  {
    return (void *)m_Pointer <= (void *)r.m_Pointer;
  }

  /** Comparison of pointers. Greater than or equal to comparison.  */
  bool
  operator>=(const AutoPointer & r) const
  {
    return (void *)m_Pointer >= (void *)r.m_Pointer;
  }

  /** Overload operator assignment.  */
  AutoPointer &
  operator=(AutoPointer & r)
  {
    AutoPointer(r).Swap(*this);
    return *this;
  }

  /** Casting operator to boolean. This is used in conditional
      statements to check the content of the pointer against null */
  operator bool() const { return (m_Pointer != nullptr); }

  /** Function to print object pointed to.  */
  /*  ObjectType *Print (std::ostream& os) const
      {
      // This prints the object pointed to by the pointer
      m_Pointer->Print(os);
      os << "Owner: " << m_IsOwner << std::endl;
      return m_Pointer;
      }
  */

private:
  /** Exchange the content of two AutoPointers */
  void
  Swap(AutoPointer & r) noexcept
  {
    ObjectType * temp = m_Pointer;

    m_Pointer = r.m_Pointer;
    r.m_Pointer = temp;
  }

  /** The pointer to the object referred to by this smart pointer. */
  ObjectType * m_Pointer;
  bool         m_IsOwner{ false };
};

template <typename T>
std::ostream &
operator<<(std::ostream & os, const AutoPointer<T> p)
{
  p.Print(os);
  os << "Owner: " << p.IsOwner() << std::endl;
  return os;
}

/** This templated function is intended to facilitate the
    transfer between AutoPointers of Derived class to Base class */
template <typename TAutoPointerBase, typename TAutoPointerDerived>
void
TransferAutoPointer(TAutoPointerBase & pa, TAutoPointerDerived & pb)
{
  // give a chance to natural polymorphism
  pa.TakeNoOwnership(pb.GetPointer());
  if (pb.IsOwner())
  {
    pa.TakeOwnership();    // pa Take Ownership
    pb.ReleaseOwnership(); // pb Release Ownership and clears
  }
}
} // end namespace itk

#endif