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 =========================================================================*/
43 #ifndef vtkWeakPointer_h
44 #define vtkWeakPointer_h
45 
46 #include "vtkWeakPointerBase.h"
47 
48 template <class T>
50 {
51 public:
56 
61 
66 
68 
72  {
74  return *this;
75  }
77 
79 
83  {
85  return *this;
86  }
88 
90 
93  T* GetPointer() const
94  {
95  return static_cast<T*>(this->Object);
96  }
97  T* Get() const
98  {
99  return static_cast<T*>(this->Object);
100  }
102 
106  operator T* () const
107  {
108  return static_cast<T*>(this->Object);
109  }
110 
115  T& operator*() const
116  {
117  return *static_cast<T*>(this->Object);
118  }
119 
123  T* operator->() const
124  {
125  return static_cast<T*>(this->Object);
126  }
127 
128  // Work-around for HP and IBM overload resolution bug. Since
129  // NullPointerOnly is a private type the only pointer value that can
130  // be passed by user code is a null pointer. This operator will be
131  // chosen by the compiler when comparing against null explicitly and
132  // avoid the bogus ambiguous overload error.
133 #if defined(__HP_aCC) || defined(__IBMCPP__)
134 # define VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
135  bool operator op (NullPointerOnly*) const \
136  { \
137  return ::operator op (*this, 0); \
138  }
139 private:
140  class NullPointerOnly {};
141 public:
142  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
143  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
144  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
145  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
146  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
147  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
148 # undef VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND
149 #endif
150 protected:
152 private:
153  // These are purposely not implemented to prevent callers from
154  // trying to take references from other smart pointers.
155  void TakeReference(const vtkWeakPointerBase&) VTK_DELETE_FUNCTION;
156  static void Take(const vtkWeakPointerBase&) VTK_DELETE_FUNCTION;
157 };
158 
159 #define VTK_WEAK_POINTER_DEFINE_OPERATOR(op) \
160  template <class T> \
161  inline bool \
162  operator op (const vtkWeakPointer<T>& l, const vtkWeakPointer<T>& r) \
163  { \
164  return (l.GetPointer() op r.GetPointer()); \
165  } \
166  template <class T> \
167  inline bool operator op (T* l, const vtkWeakPointer<T>& r) \
168  { \
169  return (l op r.GetPointer()); \
170  } \
171  template <class T> \
172  inline bool operator op (const vtkWeakPointer<T>& l, T* r) \
173  { \
174  return (l.GetPointer() op r); \
175  }
176 
185 
186 #undef VTK_WEAK_POINTER_DEFINE_OPERATOR
187 
191 template <class T>
192 inline ostream& operator << (ostream& os, const vtkWeakPointer<T>& p)
193 {
194  return os << static_cast<const vtkWeakPointerBase&>(p);
195 }
196 
197 
198 #endif
199 
200 
201 // VTK-HeaderTest-Exclude: vtkWeakPointer.h
vtkWeakPointer()
Initialize smart pointer to NULL.
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 -&gt;.
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)