Orfeo Toolbox  4.0
itkSmartPointer.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 __itkSmartPointer_h
19 #define __itkSmartPointer_h
20 
21 #include <iostream>
22 
23 namespace itk
24 {
42 template< typename TObjectType >
44 {
45 public:
46  typedef TObjectType ObjectType;
47 
50  { m_Pointer = 0; }
51 
55  { this->Register(); }
56 
59  m_Pointer(p)
60  { this->Register(); }
61 
64  {
65  this->UnRegister();
66  m_Pointer = 0;
67  }
68 
71  { return m_Pointer; }
72 
74  operator ObjectType *() const
75  { return m_Pointer; }
76 
78  bool IsNotNull() const
79  { return m_Pointer != 0; }
80  bool IsNull() const
81  { return m_Pointer == 0; }
82 
84  template< typename TR >
85  bool operator==(TR r) const
86  { return ( m_Pointer == static_cast< const ObjectType * >( r ) ); }
87 
88  template< typename TR >
89  bool operator!=(TR r) const
90  { return ( m_Pointer != static_cast< const ObjectType * >( r ) ); }
91 
94  { return m_Pointer; }
95 
97  bool operator<(const SmartPointer & r) const
98  { return (void *)m_Pointer < (void *)r.m_Pointer; }
99 
101  bool operator>(const SmartPointer & r) const
102  { return (void *)m_Pointer > (void *)r.m_Pointer; }
103 
105  bool operator<=(const SmartPointer & r) const
106  { return (void *)m_Pointer <= (void *)r.m_Pointer; }
107 
109  bool operator>=(const SmartPointer & r) const
110  { return (void *)m_Pointer >= (void *)r.m_Pointer; }
111 
113  // cppcheck-suppress operatorEqVarError
115  { return this->operator=( r.GetPointer() ); }
116 
119  {
120  if ( m_Pointer != r )
121  {
122  ObjectType *tmp = m_Pointer; //avoid recursive unregisters by retaining
123  // temporarily
124  m_Pointer = r;
125  this->Register();
126  if ( tmp ) { tmp->UnRegister(); }
127  }
128  return *this;
129  }
130 
132  ObjectType * Print(std::ostream & os) const
133  {
134  if( this->IsNull() )
135  {
136  os << "(null)";
137  }
138  else
139  {
140  // This prints the object pointed to by the pointer
141  ( *m_Pointer ).Print(os);
142  }
143  return m_Pointer;
144  }
145 
146 private:
149 
150  void Register()
151  {
152  if ( m_Pointer ) { m_Pointer->Register(); }
153  }
154 
155  void UnRegister()
156  {
157  if ( m_Pointer ) { m_Pointer->UnRegister(); }
158  }
159 };
160 
161 template< typename T >
162 std::ostream & operator<<(std::ostream & os, SmartPointer< T > p)
163 {
164  p.Print(os);
165  return os;
166 }
167 } // end namespace itk
168 
169 #endif

Generated at Sat Mar 8 2014 15:33:40 for Orfeo Toolbox with doxygen 1.8.3.1