Main Page | Directories | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

vtkSmartPointerBase.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    $RCSfile: vtkSmartPointerBase.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 =========================================================================*/
00036 #ifndef __vtkSmartPointerBase_h
00037 #define __vtkSmartPointerBase_h
00038 
00039 #include "vtkObjectBase.h"
00040 
00041 class VTK_COMMON_EXPORT vtkSmartPointerBase
00042 {
00043 private:
00044   struct SafeBoolDummy { void Dummy() {} };
00045   typedef void (SafeBoolDummy::* SafeBool)();
00046 public:
00048   vtkSmartPointerBase();
00049   
00051   vtkSmartPointerBase(vtkObjectBase* r);
00052   
00055   vtkSmartPointerBase(const vtkSmartPointerBase& r);
00056   
00058   ~vtkSmartPointerBase();
00059   
00061 
00063   vtkSmartPointerBase& operator=(vtkObjectBase* r);
00064   vtkSmartPointerBase& operator=(const vtkSmartPointerBase& r);
00066   
00068 
00069   vtkObjectBase* GetPointer() const
00070     {
00071     // Inline implementation so smart pointer comparisons can be fully
00072     // inlined.
00073     return this->Object;
00074     }
00076 
00078 
00079   operator SafeBool()
00080     {
00081     return this->Object? &SafeBoolDummy::Dummy : 0;
00082     }
00084 
00086 
00087   SafeBool operator!()
00088     {
00089     return this->Object? 0 : &SafeBoolDummy::Dummy;
00090     }
00092 protected:
00093 
00094   // Initialize smart pointer to given object, but do not increment
00095   // reference count.  The destructor will still decrement the count.
00096   // This effectively makes it an auto-ptr.
00097   class NoReference {};
00098   vtkSmartPointerBase(vtkObjectBase* r, const NoReference&);
00099   
00100   // Pointer to the actual object.
00101   vtkObjectBase* Object;
00102 
00103 private:
00104   // Internal utility methods.
00105   void Swap(vtkSmartPointerBase& r);
00106   void Register();
00107 };
00108 
00109 //----------------------------------------------------------------------------
00110 // Need to use vtkstd_bool type because std: :less requires bool return
00111 // type from operators.  This example should not be used to justify
00112 // using bool elsewhere in VTK.
00113 
00114 #define VTK_SMART_POINTER_BASE_DEFINE_OPERATOR(op) \
00115   inline vtkstd_bool \
00116   operator op (const vtkSmartPointerBase& l, const vtkSmartPointerBase& r) \
00117     { \
00118     return (static_cast<void*>(l.GetPointer()) op \
00119             static_cast<void*>(r.GetPointer())); \
00120     } \
00121   inline vtkstd_bool \
00122   operator op (vtkObjectBase* l, const vtkSmartPointerBase& r) \
00123     { \
00124     return (static_cast<void*>(l) op static_cast<void*>(r.GetPointer())); \
00125     } \
00126   inline vtkstd_bool \
00127   operator op (const vtkSmartPointerBase& l, vtkObjectBase* r) \
00128     { \
00129     return (static_cast<void*>(l.GetPointer()) op static_cast<void*>(r)); \
00130     }
00131 
00132 VTK_SMART_POINTER_BASE_DEFINE_OPERATOR(==)  
00133 VTK_SMART_POINTER_BASE_DEFINE_OPERATOR(!=)
00134 VTK_SMART_POINTER_BASE_DEFINE_OPERATOR(<)
00135 VTK_SMART_POINTER_BASE_DEFINE_OPERATOR(<=)
00136 VTK_SMART_POINTER_BASE_DEFINE_OPERATOR(>)
00137 VTK_SMART_POINTER_BASE_DEFINE_OPERATOR(>=)
00138 
00139 #undef VTK_SMART_POINTER_BASE_DEFINE_OPERATOR
00140 
00142 
00143 VTK_COMMON_EXPORT ostream& operator << (ostream& os,
00144                                         const vtkSmartPointerBase& p);
00146 
00147 #endif