ITK  4.2.0
Insight Segmentation and Registration Toolkit
itkTreeIteratorClone.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkTreeIteratorClone_h
19 #define __itkTreeIteratorClone_h
20 
21 #include "itkMacro.h"
22 #include <iostream>
23 
24 namespace itk
25 {
31 template< class TObjectType >
32 class ITK_EXPORT TreeIteratorClone
33 {
34 public:
35 
38  typedef TObjectType ObjectType;
39 
41  TreeIteratorClone () { m_Pointer = 0; }
42 
45  {
46  m_Pointer = 0;
47  if ( p.m_Pointer != NULL )
48  {
49  m_Pointer = p.m_Pointer->Clone();
50  }
51  }
53 
56  {
57  m_Pointer = 0;
58  if ( p != NULL )
59  {
60  m_Pointer = p->Clone();
61  }
62  }
64 
67  {
68  m_Pointer = 0;
69  m_Pointer = const_cast< ObjectType * >( &p )->Clone();
70  }
72 
75  {
76  delete m_Pointer;
77  m_Pointer = 0;
78  }
79 
81  ObjectType * operator->() const { return m_Pointer; }
82 
84  bool IsNotNull() const { return m_Pointer != 0; }
85  bool IsNull() const { return m_Pointer == 0; }
87 
89  template< typename TR >
90  bool operator==(TR r) const { return ( m_Pointer == (ObjectType *)( r ) ); }
91 
92  template< typename TR >
93  bool operator!=(TR r) const { return ( m_Pointer != (ObjectType *)( r ) ); }
94 
96  ObjectType * GetPointer() const { return m_Pointer; }
97 
99  bool operator<(const TreeIteratorClone & r) const { return (void *)m_Pointer < (void *)r.m_Pointer; }
100 
102  bool operator>(const TreeIteratorClone & r) const { return (void *)m_Pointer > (void *)r.m_Pointer; }
103 
105  bool operator<=(const TreeIteratorClone & r) const { return (void *)m_Pointer <= (void *)r.m_Pointer; }
106 
108  bool operator>=(const TreeIteratorClone & r) const { return (void *)m_Pointer >= (void *)r.m_Pointer; }
109 
111  TreeIteratorClone & operator=(const TreeIteratorClone & r) { return this->operator=( r.GetPointer() ); }
112 
114  TreeIteratorClone & operator=(const ObjectType *r)
115  {
116  if ( m_Pointer != r )
117  {
118  delete m_Pointer;
119  m_Pointer = 0;
120  if ( r != NULL )
121  {
122  m_Pointer = const_cast< ObjectType * >( r )->Clone();
123  }
124  }
125  return *this;
126  }
128 
129  Self &
130  operator++()
131  {
132  if ( m_Pointer )
133  {
134  ++( *m_Pointer );
135  }
136  return *this;
137  }
138 
139  const Self
140  operator++(int)
141  {
142  if ( m_Pointer )
143  {
144  const Self oldValue(m_Pointer); // create a copy of the iterator behind
145  // the pointer (Clone())
146  ++( *m_Pointer );
147  return oldValue;
148  }
149  }
150 
152  ObjectType * Print(std::ostream & os) const
153  {
154  // This prints the object pointed to by the pointer
155  ( *m_Pointer ).Print(os);
156  return m_Pointer;
157  }
159 
160 private:
163 };
164 
165 template< typename T >
166 std::ostream & operator<<(std::ostream & os, TreeIteratorClone< T > p)
167 {
168  p.Print(os);
169  return os;
170 }
171 } // end namespace itk
172 
173 #endif
174