00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: vtkWeakPointerBase.h 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 "vtkObjectBase.h" 00028 00029 class vtkObjectBaseToWeakPointerBaseFriendship; 00030 00031 class VTK_COMMON_EXPORT vtkWeakPointerBase 00032 { 00033 public: 00035 vtkWeakPointerBase() : Object(0) {}; 00036 00038 vtkWeakPointerBase(vtkObjectBase* r); 00039 00041 vtkWeakPointerBase(const vtkWeakPointerBase& r); 00042 00044 ~vtkWeakPointerBase(); 00045 00047 00049 vtkWeakPointerBase& operator=(vtkObjectBase* r); 00050 vtkWeakPointerBase& operator=(const vtkWeakPointerBase& r); 00052 00054 00055 vtkObjectBase* GetPointer() const 00056 { 00057 // Inline implementation so smart pointer comparisons can be fully 00058 // inlined. 00059 return this->Object; 00060 } 00062 00063 private: 00064 friend class vtkObjectBaseToWeakPointerBaseFriendship; 00065 00066 protected: 00067 00068 // Initialize weak pointer to given object. 00069 class NoReference {}; 00070 vtkWeakPointerBase(vtkObjectBase* r, const NoReference&); 00071 00072 // Pointer to the actual object. 00073 vtkObjectBase* Object; 00074 }; 00075 00076 //---------------------------------------------------------------------------- 00077 // Need to use vtkstd_bool type because std: :less requires bool return 00078 // type from operators. This example should not be used to justify 00079 // using bool elsewhere in VTK. 00080 00081 #define VTK_WEAK_POINTER_BASE_DEFINE_OPERATOR(op) \ 00082 inline vtkstd_bool \ 00083 operator op (const vtkWeakPointerBase& l, const vtkWeakPointerBase& r) \ 00084 { \ 00085 return (static_cast<void*>(l.GetPointer()) op \ 00086 static_cast<void*>(r.GetPointer())); \ 00087 } \ 00088 inline vtkstd_bool \ 00089 operator op (vtkObjectBase* l, const vtkWeakPointerBase& r) \ 00090 { \ 00091 return (static_cast<void*>(l) op static_cast<void*>(r.GetPointer())); \ 00092 } \ 00093 inline vtkstd_bool \ 00094 operator op (const vtkWeakPointerBase& l, vtkObjectBase* r) \ 00095 { \ 00096 return (static_cast<void*>(l.GetPointer()) op static_cast<void*>(r)); \ 00097 } 00098 00099 00100 VTK_WEAK_POINTER_BASE_DEFINE_OPERATOR(==) 00101 VTK_WEAK_POINTER_BASE_DEFINE_OPERATOR(!=) 00102 VTK_WEAK_POINTER_BASE_DEFINE_OPERATOR(<) 00103 VTK_WEAK_POINTER_BASE_DEFINE_OPERATOR(<=) 00104 VTK_WEAK_POINTER_BASE_DEFINE_OPERATOR(>) 00105 VTK_WEAK_POINTER_BASE_DEFINE_OPERATOR(>=) 00107 00108 #undef VTK_WEAK_POINTER_BASE_DEFINE_OPERATOR 00109 00111 00112 VTK_COMMON_EXPORT ostream& operator << (ostream& os, 00113 const vtkWeakPointerBase& p); 00115 00116 #endif