VTK
vtkWeakPointer.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkWeakPointer.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 =========================================================================*/
46 #ifndef vtkWeakPointer_h
47 #define vtkWeakPointer_h
48 
49 #include "vtkWeakPointerBase.h"
50 
51 template <class T>
53 {
54 public:
59 
64 
69 
71 
75  {
77  return *this;
78  }
80 
82 
86  {
88  return *this;
89  }
91 
93 
96  T* GetPointer() const
97  {
98  return static_cast<T*>(this->Object);
99  }
100  T* Get() const
101  {
102  return static_cast<T*>(this->Object);
103  }
105 
109  operator T* () const
110  {
111  return static_cast<T*>(this->Object);
112  }
113 
118  T& operator*() const
119  {
120  return *static_cast<T*>(this->Object);
121  }
122 
126  T* operator->() const
127  {
128  return static_cast<T*>(this->Object);
129  }
130 
131  // Work-around for HP and IBM overload resolution bug. Since
132  // NullPointerOnly is a private type the only pointer value that can
133  // be passed by user code is a null pointer. This operator will be
134  // chosen by the compiler when comparing against null explicitly and
135  // avoid the bogus ambiguous overload error.
136 #if defined(__HP_aCC) || defined(__IBMCPP__)
137 # define VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
138  bool operator op (NullPointerOnly*) const \
139  { \
140  return ::operator op (*this, 0); \
141  }
142 private:
143  class NullPointerOnly {};
144 public:
145  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
146  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
147  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
148  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
149  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
150  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
151 # undef VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND
152 #endif
153 protected:
155 private:
156  // These are purposely not implemented to prevent callers from
157  // trying to take references from other smart pointers.
158  void TakeReference(const vtkWeakPointerBase&) = delete;
159  static void Take(const vtkWeakPointerBase&) = delete;
160 };
161 
162 #define VTK_WEAK_POINTER_DEFINE_OPERATOR(op) \
163  template <class T> \
164  inline bool \
165  operator op (const vtkWeakPointer<T>& l, const vtkWeakPointer<T>& r) \
166  { \
167  return (l.GetPointer() op r.GetPointer()); \
168  } \
169  template <class T> \
170  inline bool operator op (T* l, const vtkWeakPointer<T>& r) \
171  { \
172  return (l op r.GetPointer()); \
173  } \
174  template <class T> \
175  inline bool operator op (const vtkWeakPointer<T>& l, T* r) \
176  { \
177  return (l.GetPointer() op r); \
178  }
179 
188 
189 #undef VTK_WEAK_POINTER_DEFINE_OPERATOR
190 
194 template <class T>
195 inline ostream& operator << (ostream& os, const vtkWeakPointer<T>& p)
196 {
197  return os << static_cast<const vtkWeakPointerBase&>(p);
198 }
199 
200 
201 #endif
202 
203 
204 // VTK-HeaderTest-Exclude: vtkWeakPointer.h
vtkWeakPointer()
Initialize smart pointer to nullptr.
vtkObjectBase * Object
vtkWeakPointerBase & operator=(vtkObjectBase *r)
Assign object to reference.
vtkWeakPointer(T *r)
Initialize smart pointer to given object.
T & operator*() const
Dereference the pointer and return a reference to the contained object.
vtkWeakPointer(const vtkWeakPointerBase &r)
Initialize smart pointer with the given smart pointer.
T * operator->() const
Provides normal pointer target member access using operator ->.
vtkWeakPointer & operator=(const vtkWeakPointerBase &r)
Assign object to reference.
vtkWeakPointer(T *r, const NoReference &n)
a weak reference to a vtkObject.
Non-templated superclass for vtkWeakPointer.
vtkWeakPointer & operator=(T *r)
Assign object to reference.
T * GetPointer() const
Get the contained pointer.
T * Get() const
Get the contained pointer.
#define VTK_WEAK_POINTER_DEFINE_OPERATOR(op)