VTK
vtkNew.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkNew.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
44 #ifndef vtkNew_h
45 #define vtkNew_h
46 
47 #include "vtkIOStream.h"
48 #include "vtkMeta.h" // for IsComplete
49 
50 #include <type_traits> // for is_base_of
51 
52 class vtkObjectBase;
53 
54 template <class T>
55 class vtkNew
56 {
57  // Allow other smart pointers friend access:
58  template <typename U>
59  friend class vtkNew;
60  template <typename U>
61  friend class vtkSmartPointer;
62  template <typename U>
63  friend class vtkWeakPointer;
64 
65  // These static asserts only fire when the function calling CheckTypes is
66  // used. Thus, this smart pointer class may still be used as a member variable
67  // with a forward declared T, so long as T is defined by the time the calling
68  // function is used.
69  template <typename U = T>
70  static void CheckTypes() noexcept
71  {
73  "vtkNew<T>'s T type has not been defined. Missing include?");
75  "Cannot store an object with undefined type in "
76  "vtkNew. Missing include?");
77  static_assert(std::is_base_of<T, U>::value,
78  "Argument type is not compatible with vtkNew<T>'s "
79  "T type.");
81  "vtkNew can only be used with subclasses of vtkObjectBase.");
82  }
83 
84 public:
89  : Object(T::New())
90  {
91  vtkNew::CheckTypes();
92  }
93 
99  vtkNew(vtkNew&& o) noexcept : Object(o.Object) { o.Object = nullptr; }
100 
101  template <typename U>
102  vtkNew(vtkNew<U>&& o) noexcept : Object(o.Object)
103  {
104  vtkNew::CheckTypes<U>();
105 
106  o.Object = nullptr;
107  }
109 
111 
114  ~vtkNew() { this->Reset(); }
115 
116  void Reset()
117  {
118  T* obj = this->Object;
119  if (obj)
120  {
121  this->Object = nullptr;
122  obj->Delete();
123  }
124  }
126 
131  T* operator->() const noexcept { return this->Object; }
132 
134 
140  T* GetPointer() const noexcept { return this->Object; }
141  T* Get() const noexcept { return this->Object; }
142  operator T*() const noexcept { return static_cast<T*>(this->Object); }
144 
150  T& operator*() const noexcept { return *static_cast<T*>(this->Object); }
151 
152 private:
153  vtkNew(vtkNew<T> const&) = delete;
154  void operator=(vtkNew<T> const&) = delete;
155  T* Object;
156 };
157 
158 #endif
159 // VTK-HeaderTest-Exclude: vtkNew.h
vtkWeakPointerBase::Object
vtkObjectBase * Object
Definition: vtkWeakPointerBase.h:92
vtkWeakPointer::operator=
vtkWeakPointer & operator=(const vtkWeakPointer &r)
Assign object to reference.
Definition: vtkWeakPointer.h:136
vtkNew::vtkNew
vtkNew(vtkNew &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition: vtkNew.h:99
vtkX3D::value
Definition: vtkX3D.h:226
vtkMeta.h
vtkSmartPointer
Hold a reference to a vtkObjectBase instance.
Definition: vtkMeta.h:32
vtkNew::Get
T * Get() const noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition: vtkNew.h:141
vtk::detail::IsComplete
Definition: vtkMeta.h:76
vtkNew::GetPointer
T * GetPointer() const noexcept
Get a raw pointer to the contained object.
Definition: vtkNew.h:140
vtkObjectBase::Delete
virtual void Delete()
Delete a VTK object.
vtkNew::operator*
T & operator*() const noexcept
Dereference the pointer and return a reference to the contained object.
Definition: vtkNew.h:150
vtkObjectBase
abstract base class for most VTK objects
Definition: vtkObjectBase.h:63
vtkNew::~vtkNew
~vtkNew()
Deletes reference to instance of T.
Definition: vtkNew.h:114
vtkNew::operator->
T * operator->() const noexcept
Enable pointer-like dereference syntax.
Definition: vtkNew.h:131
vtkNew::vtkNew
vtkNew(vtkNew< U > &&o) noexcept
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition: vtkNew.h:102
vtkNew
Allocate and hold a VTK object.
Definition: vtkMeta.h:30
vtkNew::vtkNew
vtkNew()
Create a new T on construction.
Definition: vtkNew.h:88
vtkIOStream.h
vtkNew::Reset
void Reset()
Move the object into the constructed vtkNew wrapper, stealing its reference.
Definition: vtkNew.h:116
vtkWeakPointer
a weak reference to a vtkObject.
Definition: vtkMeta.h:34