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 =========================================================================*/
45 #ifndef __vtkWeakPointer_h
46 #define __vtkWeakPointer_h
47 
48 #include "vtkWeakPointerBase.h"
49 
50 template <class T>
52 {
53 public:
56 
59 
62 
64 
66  {
68  return *this;
69  }
71 
73 
75  {
77  return *this;
78  }
80 
82 
83  T* GetPointer() const
84  {
85  return static_cast<T*>(this->Object);
86  }
88 
90 
91  operator T* () const
92  {
93  return static_cast<T*>(this->Object);
94  }
96 
98 
100  T& operator*() const
101  {
102  return *static_cast<T*>(this->Object);
103  }
105 
107 
108  T* operator->() const
109  {
110  return static_cast<T*>(this->Object);
111  }
113 
114  // Work-around for HP and IBM overload resolution bug. Since
115  // NullPointerOnly is a private type the only pointer value that can
116  // be passed by user code is a null pointer. This operator will be
117  // chosen by the compiler when comparing against null explicitly and
118  // avoid the bogus ambiguous overload error.
119 #if defined(__HP_aCC) || defined(__IBMCPP__)
120 # define VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(op) \
121  vtkstd_bool operator op (NullPointerOnly*) const \
122  { \
123  return ::operator op (*this, 0); \
124  }
125 private:
126  class NullPointerOnly {};
127 public:
128  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(==)
129  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(!=)
130  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<)
131  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(<=)
132  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>)
133  VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND(>=)
134 # undef VTK_WEAK_POINTER_DEFINE_OPERATOR_WORKAROUND
135 #endif
136 protected:
138 private:
139  // These are purposely not implemented to prevent callers from
140  // trying to take references from other smart pointers.
141  void TakeReference(const vtkWeakPointerBase&); // Not implemented.
142  static void Take(const vtkWeakPointerBase&); // Not implemented.
143 };
144 
145 #define VTK_WEAK_POINTER_DEFINE_OPERATOR(op) \
146  template <class T> \
147  inline vtkstd_bool \
148  operator op (const vtkWeakPointer<T>& l, const vtkWeakPointer<T>& r) \
149  { \
150  return (l.GetPointer() op r.GetPointer()); \
151  } \
152  template <class T> \
153  inline vtkstd_bool operator op (T* l, const vtkWeakPointer<T>& r) \
154  { \
155  return (l op r.GetPointer()); \
156  } \
157  template <class T> \
158  inline vtkstd_bool operator op (const vtkWeakPointer<T>& l, T* r) \
159  { \
160  return (l.GetPointer() op r); \
161  }
162 
163 
171 
172 #undef VTK_WEAK_POINTER_DEFINE_OPERATOR
173 
175 
176 template <class T>
177 inline ostream& operator << (ostream& os, const vtkWeakPointer<T>& p)
178 {
179  return os << static_cast<const vtkWeakPointerBase&>(p);
180 }
182 
183 
184 #endif
185 
186 
vtkObjectBase * Object
vtkWeakPointerBase & operator=(vtkObjectBase *r)
T & operator*() const
vtkWeakPointer(const vtkWeakPointerBase &r)
T * operator->() const
vtkWeakPointer & operator=(const vtkWeakPointerBase &r)
vtkWeakPointer(T *r, const NoReference &n)
a weak reference to a vtkObject.
Non-templated superclass for vtkWeakPointer.
vtkWeakPointer & operator=(T *r)
T * GetPointer() const
#define VTK_WEAK_POINTER_DEFINE_OPERATOR(op)