ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkWeakPointer.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkWeakPointer_h
00019 #define __itkWeakPointer_h
00020 
00021 #include "itkMacro.h"
00022 #include <iostream>
00023 
00024 namespace itk
00025 {
00043 template< class TObjectType >
00044 class ITK_EXPORT WeakPointer
00045 {
00046 public:
00048   typedef TObjectType ObjectType;
00049 
00051   WeakPointer ()
00052   { m_Pointer = 0; }
00053 
00055   WeakPointer (const WeakPointer< ObjectType > & p):m_Pointer(p.m_Pointer) {}
00056 
00058   WeakPointer (ObjectType *p):m_Pointer(p) {}
00059 
00061   ~WeakPointer ()
00062   { m_Pointer = 0; }
00063 
00065   ObjectType * operator->() const
00066   { return m_Pointer; }
00067 
00069   operator ObjectType *() const
00070         { return m_Pointer; }
00071 
00073   template< typename R >
00074   bool operator==(R r) const
00075   {
00076     return ( m_Pointer == (ObjectType *)r );
00077   }
00078 
00079   template< typename R >
00080   bool operator!=(R r) const
00081   {
00082     return ( m_Pointer != (ObjectType *)r );
00083   }
00084 
00086   ObjectType * GetPointer() const
00087   { return m_Pointer; }
00088 
00090   bool operator<(const WeakPointer & r) const
00091   { return (void *)m_Pointer < (void *)r.m_Pointer; }
00092 
00094   bool operator>(const WeakPointer & r) const
00095   { return (void *)m_Pointer > (void *)r.m_Pointer; }
00096 
00098   bool operator<=(const WeakPointer & r) const
00099   { return (void *)m_Pointer <= (void *)r.m_Pointer; }
00100 
00102   bool operator>=(const WeakPointer & r) const
00103   { return (void *)m_Pointer >= (void *)r.m_Pointer; }
00104 
00106   // cppcheck-suppress operatorEqVarError
00107   WeakPointer & operator=(const WeakPointer & r)
00108   { return this->operator=( r.GetPointer() ); }
00109 
00111   WeakPointer & operator=(ObjectType *r)
00112   {
00113     m_Pointer = r;
00114     return *this;
00115   }
00116 
00118   ObjectType * Print(std::ostream & os) const
00119   {
00120     // This prints the object pointed to by the pointer
00121     ( *m_Pointer ).Print(os);
00122     return m_Pointer;
00123   }
00125 
00126 private:
00128   ObjectType *m_Pointer;
00129 };
00130 
00131 template< typename T >
00132 std::ostream & operator<<(std::ostream & os, WeakPointer< T > p)
00133 {
00134   p.Print(os);
00135   return os;
00136 }
00137 } // end namespace itk
00138 
00139 #endif
00140