ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkTreeIteratorClone.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 __itkTreeIteratorClone_h
00019 #define __itkTreeIteratorClone_h
00020 
00021 #include "itkMacro.h"
00022 #include <iostream>
00023 
00024 namespace itk
00025 {
00031 template< class TObjectType >
00032 class ITK_EXPORT TreeIteratorClone
00033 {
00034 public:
00035 
00037   typedef TreeIteratorClone< TObjectType > Self;
00038   typedef TObjectType                      ObjectType;
00039 
00041   TreeIteratorClone () { m_Pointer = 0; }
00042 
00044   TreeIteratorClone (const TreeIteratorClone< ObjectType > & p)
00045   {
00046     m_Pointer = 0;
00047     if ( p.m_Pointer != NULL )
00048       {
00049       m_Pointer = p.m_Pointer->Clone();
00050       }
00051   }
00053 
00055   TreeIteratorClone (ObjectType *p)
00056   {
00057     m_Pointer = 0;
00058     if ( p != NULL )
00059       {
00060       m_Pointer = p->Clone();
00061       }
00062   }
00064 
00066   TreeIteratorClone (const ObjectType & p)
00067   {
00068     m_Pointer = 0;
00069     m_Pointer = const_cast< ObjectType * >( &p )->Clone();
00070   }
00072 
00074   ~TreeIteratorClone ()
00075   {
00076     delete m_Pointer;
00077     m_Pointer = 0;
00078   }
00079 
00081   ObjectType * operator->() const { return m_Pointer; }
00082 
00084   bool IsNotNull() const { return m_Pointer != 0; }
00085   bool IsNull() const { return m_Pointer == 0; }
00087 
00089   template< typename TR >
00090   bool operator==(TR r) const { return ( m_Pointer == (ObjectType *)( r ) ); }
00091 
00092   template< typename TR >
00093   bool operator!=(TR r) const { return ( m_Pointer != (ObjectType *)( r ) ); }
00094 
00096   ObjectType * GetPointer() const { return m_Pointer; }
00097 
00099   bool operator<(const TreeIteratorClone & r) const { return (void *)m_Pointer < (void *)r.m_Pointer;  }
00100 
00102   bool operator>(const TreeIteratorClone & r) const { return (void *)m_Pointer > (void *)r.m_Pointer; }
00103 
00105   bool operator<=(const TreeIteratorClone & r) const { return (void *)m_Pointer <= (void *)r.m_Pointer; }
00106 
00108   bool operator>=(const TreeIteratorClone & r) const { return (void *)m_Pointer >= (void *)r.m_Pointer; }
00109 
00111   TreeIteratorClone & operator=(const TreeIteratorClone & r) { return this->operator=( r.GetPointer() ); }
00112 
00114   TreeIteratorClone & operator=(const ObjectType *r)
00115   {
00116     if ( m_Pointer != r )
00117       {
00118       delete m_Pointer;
00119       m_Pointer = 0;
00120       if ( r != NULL )
00121         {
00122         m_Pointer = const_cast< ObjectType * >( r )->Clone();
00123         }
00124       }
00125     return *this;
00126   }
00128 
00129   Self &
00130   operator++()
00131   {
00132     if ( m_Pointer )
00133       {
00134       ++( *m_Pointer );
00135       }
00136     return *this;
00137   }
00138 
00139   const Self
00140   operator++(int)
00141   {
00142     if ( m_Pointer )
00143       {
00144       const Self oldValue(m_Pointer);   // create a copy of the iterator behind
00145                                         // the pointer (Clone())
00146       ++( *m_Pointer );
00147       return oldValue;
00148       }
00149   }
00150 
00152   ObjectType * Print(std::ostream & os) const
00153   {
00154     // This prints the object pointed to by the pointer
00155     ( *m_Pointer ).Print(os);
00156     return m_Pointer;
00157   }
00159 
00160 private:
00162   ObjectType *m_Pointer;
00163 };
00164 
00165 template< typename T >
00166 std::ostream & operator<<(std::ostream & os, TreeIteratorClone< T > p)
00167 {
00168   p.Print(os);
00169   return os;
00170 }
00171 } // end namespace itk
00172 
00173 #endif
00174