00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: $RCSfile: vtkWeakPointerBase.h,v $ 00005 00006 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 00007 All rights reserved. 00008 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 00009 00010 This software is distributed WITHOUT ANY WARRANTY; without even 00011 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00012 PURPOSE. See the above copyright notice for more information. 00013 00014 =========================================================================*/ 00024 #ifndef __vtkWeakPointerBase_h 00025 #define __vtkWeakPointerBase_h 00026 00027 #include "vtkObject.h" 00028 00029 class VTK_COMMON_EXPORT vtkWeakPointerBase 00030 { 00031 public: 00033 vtkWeakPointerBase(); 00034 00036 vtkWeakPointerBase(vtkObject* r); 00037 00039 vtkWeakPointerBase(const vtkWeakPointerBase& r); 00040 00042 ~vtkWeakPointerBase(); 00043 00045 00047 vtkWeakPointerBase& operator=(vtkObject* r); 00048 vtkWeakPointerBase& operator=(const vtkWeakPointerBase& r); 00050 00052 00053 vtkObject* GetPointer() const 00054 { 00055 // Inline implementation so smart pointer comparisons can be fully 00056 // inlined. 00057 return this->Object; 00058 } 00060 00061 protected: 00062 00063 // Initialize smart pointer to given object, but do not increment 00064 // reference count. The destructor will still decrement the count. 00065 // This effectively makes it an auto-ptr. 00066 class NoReference {}; 00067 vtkWeakPointerBase(vtkObject* r, const NoReference&); 00068 00069 // Pointer to the actual object. 00070 vtkObject* Object; 00071 00072 private: 00073 // Internal utility methods. 00074 void RemoveObserver(); 00075 void AddObserver(); 00076 00077 class vtkObserver; 00078 friend class vtkObserver; 00079 vtkObserver* Observer; 00080 }; 00081 00082 //---------------------------------------------------------------------------- 00083 // Need to use vtkstd_bool type because std: :less requires bool return 00084 // type from operators. This example should not be used to justify 00085 // using bool elsewhere in VTK. 00086 00087 #define VTK_WEAK_POINTER_BASE_DEFINE_OPERATOR(op) \ 00088 inline vtkstd_bool \ 00089 operator op (const vtkWeakPointerBase& l, const vtkWeakPointerBase& r) \ 00090 { \ 00091 return (static_cast<void*>(l.GetPointer()) op \ 00092 static_cast<void*>(r.GetPointer())); \ 00093 } \ 00094 inline vtkstd_bool \ 00095 operator op (vtkObject* l, const vtkWeakPointerBase& r) \ 00096 { \ 00097 return (static_cast<void*>(l) op static_cast<void*>(r.GetPointer())); \ 00098 } \ 00099 inline vtkstd_bool \ 00100 operator op (const vtkWeakPointerBase& l, vtkObject* r) \ 00101 { \ 00102 return (static_cast<void*>(l.GetPointer()) op static_cast<void*>(r)); \ 00103 } 00104 00105 VTK_WEAK_POINTER_BASE_DEFINE_OPERATOR(==) 00106 VTK_WEAK_POINTER_BASE_DEFINE_OPERATOR(!=) 00107 VTK_WEAK_POINTER_BASE_DEFINE_OPERATOR(<) 00108 VTK_WEAK_POINTER_BASE_DEFINE_OPERATOR(<=) 00109 VTK_WEAK_POINTER_BASE_DEFINE_OPERATOR(>) 00110 VTK_WEAK_POINTER_BASE_DEFINE_OPERATOR(>=) 00111 00112 #undef VTK_WEAK_POINTER_BASE_DEFINE_OPERATOR 00113 00115 00116 VTK_COMMON_EXPORT ostream& operator << (ostream& os, 00117 const vtkWeakPointerBase& p); 00119 00120 #endif