File: vtkSmartPointer.h

package info (click to toggle)
paraview 5.11.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 497,236 kB
  • sloc: cpp: 3,171,290; ansic: 1,315,072; python: 134,290; xml: 103,324; sql: 65,887; sh: 5,286; javascript: 4,901; yacc: 4,383; java: 3,977; perl: 2,363; lex: 1,909; f90: 1,255; objc: 143; makefile: 119; tcl: 59; pascal: 50; fortran: 29
file content (371 lines) | stat: -rw-r--r-- 12,413 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkSmartPointer.h

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
/**
 * @class   vtkSmartPointer
 * @brief   Hold a reference to a vtkObjectBase instance.
 *
 * vtkSmartPointer is a class template that provides automatic casting
 * for objects held by the vtkSmartPointerBase superclass.
 */

#ifndef vtkSmartPointer_h
#define vtkSmartPointer_h

#include "vtkSmartPointerBase.h"

#include "vtkMeta.h" // for IsComplete
#include "vtkNew.h"  // for vtkNew.h

#include <type_traits> // for is_base_of
#include <utility>     // for std::move

VTK_ABI_NAMESPACE_BEGIN
template <class T>
class vtkSmartPointer : public vtkSmartPointerBase
{
  // These static asserts only fire when the function calling CheckTypes is
  // used. Thus, this smart pointer class may still be used as a member variable
  // with a forward declared T, so long as T is defined by the time the calling
  // function is used.
  template <typename U = T>
  static void CheckTypes() noexcept
  {
    static_assert(vtk::detail::IsComplete<T>::value,
      "vtkSmartPointer<T>'s T type has not been defined. Missing "
      "include?");
    static_assert(vtk::detail::IsComplete<U>::value,
      "Cannot store an object with undefined type in "
      "vtkSmartPointer. Missing include?");
    static_assert(std::is_base_of<T, U>::value,
      "Argument type is not compatible with vtkSmartPointer<T>'s "
      "T type.");
    static_assert(std::is_base_of<vtkObjectBase, T>::value,
      "vtkSmartPointer can only be used with subclasses of "
      "vtkObjectBase.");
  }

public:
  /**
   * Initialize smart pointer to nullptr.
   */
  vtkSmartPointer() noexcept
    : vtkSmartPointerBase()
  {
  }

  /**
   * Initialize smart pointer with a new reference to the same object
   * referenced by given smart pointer.
   * @{
   */
  // Need both overloads because the copy-constructor must be non-templated:
  vtkSmartPointer(const vtkSmartPointer& r)
    : vtkSmartPointerBase(r)
  {
  }

  template <class U>
  vtkSmartPointer(const vtkSmartPointer<U>& r)
    : vtkSmartPointerBase(r)
  {
    vtkSmartPointer::CheckTypes<U>();
  }
  /* @} **/

  /**
   * Move the contents of @a r into @a this.
   * @{
   */
  // Need both overloads because the move-constructor must be non-templated:
  vtkSmartPointer(vtkSmartPointer&& r) noexcept
    : vtkSmartPointerBase(std::move(r))
  {
  }

  template <class U>
  vtkSmartPointer(vtkSmartPointer<U>&& r) noexcept
    : vtkSmartPointerBase(std::move(r))
  {
    vtkSmartPointer::CheckTypes<U>();
  }
  /**@}*/

  /**
   * Initialize smart pointer to given object.
   * @{
   */
  vtkSmartPointer(T* r)
    : vtkSmartPointerBase(r)
  {
    vtkSmartPointer::CheckTypes();
  }

  template <typename U>
  vtkSmartPointer(const vtkNew<U>& r)
    : vtkSmartPointerBase(r.Object)
  { // Create a new reference on copy
    vtkSmartPointer::CheckTypes<U>();
  }
  ///@}

  /**
   * Move the pointer from the vtkNew smart pointer to the new vtkSmartPointer,
   * stealing its reference and resetting the vtkNew object to nullptr.
   */
  template <typename U>
  vtkSmartPointer(vtkNew<U>&& r) noexcept
    : vtkSmartPointerBase(r.Object, vtkSmartPointerBase::NoReference{})
  { // Steal the reference on move
    vtkSmartPointer::CheckTypes<U>();

    r.Object = nullptr;
  }

  ///@{
  /**
   * Assign object to reference.  This removes any reference to an old
   * object.
   */
  // Need this since the compiler won't recognize template functions as
  // assignment operators.
  vtkSmartPointer& operator=(const vtkSmartPointer& r)
  {
    this->vtkSmartPointerBase::operator=(r.GetPointer());
    return *this;
  }

  template <class U>
  vtkSmartPointer& operator=(const vtkSmartPointer<U>& r)
  {
    vtkSmartPointer::CheckTypes<U>();

    this->vtkSmartPointerBase::operator=(r.GetPointer());
    return *this;
  }
  ///@}

  /**
   * Assign object to reference.  This removes any reference to an old
   * object.
   */
  template <typename U>
  vtkSmartPointer& operator=(const vtkNew<U>& r)
  {
    vtkSmartPointer::CheckTypes<U>();

    this->vtkSmartPointerBase::operator=(r.Object);
    return *this;
  }

  /**
   * Assign object to reference.  This adds a new reference to an old
   * object.
   */
  template <typename U>
  vtkSmartPointer& operator=(U* r)
  {
    vtkSmartPointer::CheckTypes<U>();

    this->vtkSmartPointerBase::operator=(r);
    return *this;
  }

  ///@{
  /**
   * Get the contained pointer.
   */
  T* GetPointer() const noexcept { return static_cast<T*>(this->Object); }
  T* Get() const noexcept { return static_cast<T*>(this->Object); }
  ///@}

  /**
   * Get the contained pointer.
   */
  operator T*() const noexcept { return static_cast<T*>(this->Object); }

  /**
   * Dereference the pointer and return a reference to the contained
   * object.
   */
  T& operator*() const noexcept { return *static_cast<T*>(this->Object); }

  /**
   * Provides normal pointer target member access using operator ->.
   */
  T* operator->() const noexcept { return static_cast<T*>(this->Object); }

  /**
   * Transfer ownership of one reference to the given VTK object to
   * this smart pointer.  This does not increment the reference count
   * of the object, but will decrement it later.  The caller is
   * effectively passing ownership of one reference to the smart
   * pointer.  This is useful for code like:

   * vtkSmartPointer<vtkFoo> foo;
   * foo.TakeReference(bar->NewFoo());

   * The input argument may not be another smart pointer.
   */
  void TakeReference(T* t) { *this = vtkSmartPointer<T>(t, NoReference()); }

  /**
   * Create an instance of a VTK object.
   */
  static vtkSmartPointer<T> New() { return vtkSmartPointer<T>(T::New(), NoReference()); }
  /**
   * Create an instance of a VTK object in a memkind extended memory space. Note that not all
   * vtkObjects support this yet and that VTK needs to be compiled with VTK_USE_MEMKIND to enable
   * those that do. If not enabled, this is equivalent to calling New()
   */
  static vtkSmartPointer<T> ExtendedNew()
  {
    return vtkSmartPointer<T>(T::ExtendedNew(), NoReference());
  }

  /**
   * Create a new instance of the given VTK object.
   */
  static vtkSmartPointer<T> NewInstance(T* t)
  {
    return vtkSmartPointer<T>(t->NewInstance(), NoReference());
  }

  /**
   * Transfer ownership of one reference to the given VTK object to a
   * new smart pointer.  The returned smart pointer does not increment
   * the reference count of the object on construction but will
   * decrement it on destruction.  The caller is effectively passing
   * ownership of one reference to the smart pointer.  This is useful
   * for code like:

   * vtkSmartPointer<vtkFoo> foo =
   * vtkSmartPointer<vtkFoo>::Take(bar->NewFoo());

   * The input argument may not be another smart pointer.
   */
  static vtkSmartPointer<T> Take(T* t) { return vtkSmartPointer<T>(t, NoReference()); }

  // Work-around for HP and IBM overload resolution bug.  Since
  // NullPointerOnly is a private type the only pointer value that can
  // be passed by user code is a null pointer.  This operator will be
  // chosen by the compiler when comparing against null explicitly and
  // avoid the bogus ambiguous overload error.
#if defined(__HP_aCC) || defined(__IBMCPP__)
#define VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(op)                                           \
  bool operator op(NullPointerOnly*) const { return ::operator op(*this, 0); }

private:
  class NullPointerOnly
  {
  };

public:
  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
  VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
#undef VTK_SMART_POINTER_DEFINE_OPERATOR_WORKAROUND
#endif
protected:
  vtkSmartPointer(T* r, const NoReference& n)
    : vtkSmartPointerBase(r, n)
  {
  }

private:
  // These are purposely not implemented to prevent callers from
  // trying to take references from other smart pointers.
  void TakeReference(const vtkSmartPointerBase&) = delete;
  static void Take(const vtkSmartPointerBase&) = delete;
};

#define VTK_SMART_POINTER_DEFINE_OPERATOR(op)                                                      \
  template <class T, class U>                                                                      \
  inline bool operator op(const vtkSmartPointer<T>& l, const vtkSmartPointer<U>& r)                \
  {                                                                                                \
    return (l.GetPointer() op r.GetPointer());                                                     \
  }                                                                                                \
  template <class T, class U>                                                                      \
  inline bool operator op(T* l, const vtkSmartPointer<U>& r)                                       \
  {                                                                                                \
    return (l op r.GetPointer());                                                                  \
  }                                                                                                \
  template <class T, class U>                                                                      \
  inline bool operator op(const vtkSmartPointer<T>& l, U* r)                                       \
  {                                                                                                \
    return (l.GetPointer() op r);                                                                  \
  }                                                                                                \
  template <class T, class U>                                                                      \
  inline bool operator op(const vtkNew<T>& l, const vtkSmartPointer<U>& r)                         \
  {                                                                                                \
    return (l.GetPointer() op r.GetPointer());                                                     \
  }                                                                                                \
  template <class T, class U>                                                                      \
  inline bool operator op(const vtkSmartPointer<T>& l, const vtkNew<U>& r)                         \
  {                                                                                                \
    return (l.GetPointer() op r.GetPointer);                                                       \
  }

/**
 * Compare smart pointer values.
 */
VTK_SMART_POINTER_DEFINE_OPERATOR(==)
VTK_SMART_POINTER_DEFINE_OPERATOR(!=)
VTK_SMART_POINTER_DEFINE_OPERATOR(<)
VTK_SMART_POINTER_DEFINE_OPERATOR(<=)
VTK_SMART_POINTER_DEFINE_OPERATOR(>)
VTK_SMART_POINTER_DEFINE_OPERATOR(>=)

#undef VTK_SMART_POINTER_DEFINE_OPERATOR
VTK_ABI_NAMESPACE_END

namespace vtk
{
VTK_ABI_NAMESPACE_BEGIN

/// Construct a vtkSmartPointer<T> containing @a obj. A new reference is added
/// to @a obj.
template <typename T>
vtkSmartPointer<T> MakeSmartPointer(T* obj)
{
  return vtkSmartPointer<T>{ obj };
}

/// Construct a vtkSmartPointer<T> containing @a obj. @a obj's reference count
/// is not changed.
template <typename T>
vtkSmartPointer<T> TakeSmartPointer(T* obj)
{
  return vtkSmartPointer<T>::Take(obj);
}

VTK_ABI_NAMESPACE_END
} // end namespace vtk

VTK_ABI_NAMESPACE_BEGIN
/**
 * Streaming operator to print smart pointer like regular pointers.
 */
template <class T>
inline ostream& operator<<(ostream& os, const vtkSmartPointer<T>& p)
{
  return os << static_cast<const vtkSmartPointerBase&>(p);
}

VTK_ABI_NAMESPACE_END
#endif
// VTK-HeaderTest-Exclude: vtkSmartPointer.h