ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkSmartPointer.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 __itkSmartPointer_h
00019 #define __itkSmartPointer_h
00020 
00021 #include <iostream>
00022 
00023 namespace itk
00024 {
00042 template< class TObjectType >
00043 class SmartPointer
00044 {
00045 public:
00046   typedef TObjectType ObjectType;
00047 
00049   SmartPointer ()
00050   { m_Pointer = 0; }
00051 
00053   SmartPointer (const SmartPointer< ObjectType > & p):
00054     m_Pointer(p.m_Pointer)
00055   { this->Register(); }
00056 
00058   SmartPointer (ObjectType *p):
00059     m_Pointer(p)
00060   { this->Register(); }
00061 
00063   ~SmartPointer ()
00064   {
00065     this->UnRegister();
00066     m_Pointer = 0;
00067   }
00069 
00071   ObjectType * operator->() const
00072   { return m_Pointer; }
00073 
00075   operator ObjectType *() const
00076         { return m_Pointer; }
00077 
00079   bool IsNotNull() const
00080   { return m_Pointer != 0; }
00081   bool IsNull() const
00082   { return m_Pointer == 0; }
00084 
00086   template< typename TR >
00087   bool operator==(TR r) const
00088   { return ( m_Pointer == static_cast< const ObjectType * >( r ) ); }
00089 
00090   template< typename TR >
00091   bool operator!=(TR r) const
00092   { return ( m_Pointer != static_cast< const ObjectType * >( r ) ); }
00093 
00095   ObjectType * GetPointer() const
00096   { return m_Pointer; }
00097 
00099   bool operator<(const SmartPointer & r) const
00100   { return (void *)m_Pointer < (void *)r.m_Pointer; }
00101 
00103   bool operator>(const SmartPointer & r) const
00104   { return (void *)m_Pointer > (void *)r.m_Pointer; }
00105 
00107   bool operator<=(const SmartPointer & r) const
00108   { return (void *)m_Pointer <= (void *)r.m_Pointer; }
00109 
00111   bool operator>=(const SmartPointer & r) const
00112   { return (void *)m_Pointer >= (void *)r.m_Pointer; }
00113 
00115   // cppcheck-suppress operatorEqVarError
00116   SmartPointer & operator=(const SmartPointer & r)
00117   { return this->operator=( r.GetPointer() ); }
00118 
00120   SmartPointer & operator=(ObjectType *r)
00121   {
00122     if ( m_Pointer != r )
00123       {
00124       ObjectType *tmp = m_Pointer; //avoid recursive unregisters by retaining
00125                                    // temporarily
00126       m_Pointer = r;
00127       this->Register();
00128       if ( tmp ) { tmp->UnRegister(); }
00129       }
00130     return *this;
00131   }
00133 
00135   ObjectType * Print(std::ostream & os) const
00136   {
00137     // This prints the object pointed to by the pointer
00138     ( *m_Pointer ).Print(os);
00139     return m_Pointer;
00140   }
00142 
00143 private:
00145   ObjectType *m_Pointer;
00146 
00147   void Register()
00148   {
00149     if ( m_Pointer ) { m_Pointer->Register(); }
00150   }
00151 
00152   void UnRegister()
00153   {
00154     if ( m_Pointer ) { m_Pointer->UnRegister(); }
00155   }
00156 };
00157 
00158 template< typename T >
00159 std::ostream & operator<<(std::ostream & os, SmartPointer< T > p)
00160 {
00161   p.Print(os);
00162   return os;
00163 }
00164 } // end namespace itk
00165 
00166 #endif
00167