ITK  4.13.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 #include "itkConfigure.h"
23 
24 namespace itk
25 {
43 template< typename TObjectType >
45 {
46 public:
47  typedef TObjectType ObjectType;
48 
51  { m_Pointer = ITK_NULLPTR; }
52 
56  { this->Register(); }
57 
60  m_Pointer(p)
61  { this->Register(); }
62 
65  {
66  this->UnRegister();
67  m_Pointer = ITK_NULLPTR;
68  }
70 
73  { return m_Pointer; }
74 
76  operator ObjectType *() const
77  { return m_Pointer; }
78 
80  bool IsNotNull() const
81  { return m_Pointer != ITK_NULLPTR; }
82 
84  bool IsNull() const
85  { return m_Pointer == ITK_NULLPTR; }
86 
88  template< typename TR >
89  bool operator==(TR r) const
90  { return ( m_Pointer == static_cast< const ObjectType * >( r ) ); }
91 
92  template< typename TR >
93  bool operator!=(TR r) const
94  { return ( m_Pointer != static_cast< const ObjectType * >( r ) ); }
95 
98  { return 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  bool operator>=(const SmartPointer & r) const
114  { return (void *)m_Pointer >= (void *)r.m_Pointer; }
115 
117  // cppcheck-suppress operatorEqVarError
119  { return this->operator=( r.GetPointer() ); }
120 
123  {
124  SmartPointer temp(r);
125  temp.Swap(*this);
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  }
146 
147 #if ! defined ( ITK_FUTURE_LEGACY_REMOVE )
148  void swap(SmartPointer &other)
149  {
150  this->Swap(other);
151  }
152 #endif
153 
154  void Swap(SmartPointer &other)
155  {
156  ObjectType *tmp = this->m_Pointer;
157  this->m_Pointer = other.m_Pointer;
158  other.m_Pointer = tmp;
159  }
160 
161 private:
164 
165  void Register()
166  {
167  if ( m_Pointer ) { m_Pointer->Register(); }
168  }
169 
170  void UnRegister() ITK_NOEXCEPT
171  {
172  if ( m_Pointer ) { m_Pointer->UnRegister(); }
173  }
174 };
175 
176 template< typename T >
177 std::ostream & operator<<(std::ostream & os, SmartPointer< T > p)
178 {
179  p.Print(os);
180  return os;
181 }
182 
183 template<typename T>
184 inline void swap( SmartPointer<T> &a, SmartPointer<T> &b )
185 {
186  a.Swap(b);
187 }
188 
189 } // end namespace itk
190 
191 #endif
void Swap(SmartPointer &other)
SmartPointer & operator=(const SmartPointer &r)
void swap(SmartPointer &other)
void UnRegister() noexcept
bool operator<(const SmartPointer &r) const
TObjectType ObjectType
SmartPointer(ObjectType *p)
SmartPointer(const SmartPointer< ObjectType > &p)
ObjectType * GetPointer() const
bool IsNotNull() const
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:215
bool operator>=(const SmartPointer &r) const
ObjectType * operator->() const
bool operator<=(const SmartPointer &r) const
bool operator!=(TR r) const
bool IsNull() const
bool operator>(const SmartPointer &r) const
Implements transparent reference counting.
ObjectType * Print(std::ostream &os) const
SmartPointer & operator=(ObjectType *r)
ObjectType * m_Pointer
bool operator==(TR r) const