ITK  4.2.0
Insight Segmentation and Registration Toolkit
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< class 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  }
69 
72  { return m_Pointer; }
73 
75  operator ObjectType *() const
76  { return m_Pointer; }
77 
79  bool IsNotNull() const
80  { return m_Pointer != 0; }
81  bool IsNull() const
82  { return m_Pointer == 0; }
84 
86  template< typename TR >
87  bool operator==(TR r) const
88  { return ( m_Pointer == static_cast< const ObjectType * >( r ) ); }
89 
90  template< typename TR >
91  bool operator!=(TR r) const
92  { return ( m_Pointer != static_cast< const ObjectType * >( r ) ); }
93 
96  { return m_Pointer; }
97 
99  bool operator<(const SmartPointer & r) const
100  { return (void *)m_Pointer < (void *)r.m_Pointer; }
101 
103  bool operator>(const SmartPointer & r) const
104  { return (void *)m_Pointer > (void *)r.m_Pointer; }
105 
107  bool operator<=(const SmartPointer & r) const
108  { return (void *)m_Pointer <= (void *)r.m_Pointer; }
109 
111  bool operator>=(const SmartPointer & r) const
112  { return (void *)m_Pointer >= (void *)r.m_Pointer; }
113 
115  // cppcheck-suppress operatorEqVarError
117  { return this->operator=( r.GetPointer() ); }
118 
121  {
122  if ( m_Pointer != r )
123  {
124  ObjectType *tmp = m_Pointer; //avoid recursive unregisters by retaining
125  // temporarily
126  m_Pointer = r;
127  this->Register();
128  if ( tmp ) { tmp->UnRegister(); }
129  }
130  return *this;
131  }
133 
135  ObjectType * Print(std::ostream & os) const
136  {
137  // This prints the object pointed to by the pointer
138  ( *m_Pointer ).Print(os);
139  return m_Pointer;
140  }
142 
143 private:
146 
147  void Register()
148  {
149  if ( m_Pointer ) { m_Pointer->Register(); }
150  }
151 
152  void UnRegister()
153  {
154  if ( m_Pointer ) { m_Pointer->UnRegister(); }
155  }
156 };
157 
158 template< typename T >
159 std::ostream & operator<<(std::ostream & os, SmartPointer< T > p)
160 {
161  p.Print(os);
162  return os;
163 }
164 } // end namespace itk
165 
166 #endif
167