ITK  4.9.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 #if __cplusplus >= 201103L
43 // In c++11 there is an explicit nullptr type that introduces a new keyword to
44 // serve as a distinguished null pointer constant: nullptr. It is of type
45 // nullptr_t, which is implicitly convertible and comparable to any pointer type
46 // or pointer-to-member type. It is not implicitly convertible or comparable to
47 // integral types, except for bool.
48 #define ITK_SP_NULLPTR nullptr
49 #define ITK_SP_NOEXCEPT noexcept
50 #else
51 #define ITK_SP_NULLPTR NULL
52 #define ITK_SP_NOEXCEPT
53 #endif
54 template< typename TObjectType >
56 {
57 public:
58  typedef TObjectType ObjectType;
59 
63 
67  { this->Register(); }
68 
71  m_Pointer(p)
72  { this->Register(); }
73 
76  {
77  this->UnRegister();
79  }
81 
84  { return m_Pointer; }
85 
87  operator ObjectType *() const
88  { return m_Pointer; }
89 
91  bool IsNotNull() const
92  { return m_Pointer != ITK_SP_NULLPTR; }
93 
95  bool IsNull() const
96  { return m_Pointer == ITK_SP_NULLPTR; }
97 
99  template< typename TR >
100  bool operator==(TR r) const
101  { return ( m_Pointer == static_cast< const ObjectType * >( r ) ); }
102 
103  template< typename TR >
104  bool operator!=(TR r) const
105  { return ( m_Pointer != static_cast< const ObjectType * >( r ) ); }
106 
109  { return m_Pointer; }
110 
112  bool operator<(const SmartPointer & r) const
113  { return (void *)m_Pointer < (void *)r.m_Pointer; }
114 
116  bool operator>(const SmartPointer & r) const
117  { return (void *)m_Pointer > (void *)r.m_Pointer; }
118 
120  bool operator<=(const SmartPointer & r) const
121  { return (void *)m_Pointer <= (void *)r.m_Pointer; }
122 
124  bool operator>=(const SmartPointer & r) const
125  { return (void *)m_Pointer >= (void *)r.m_Pointer; }
126 
128  // cppcheck-suppress operatorEqVarError
130  { return this->operator=( r.GetPointer() ); }
131 
134  {
135  SmartPointer temp(r);
136  temp.Swap(*this);
138 
139  return *this;
140  }
141 
143  ObjectType * Print(std::ostream & os) const
144  {
145  if( this->IsNull() )
146  {
147  os << "(null)";
148  }
149  else
150  {
151  // This prints the object pointed to by the pointer
152  ( *m_Pointer ).Print(os);
153  }
154  return m_Pointer;
155  }
157 
158 #if ! defined ( ITK_FUTURE_LEGACY_REMOVE )
159  void swap(SmartPointer &other)
160  {
161  this->Swap(other);
162  }
163 #endif
164 
165  void Swap(SmartPointer &other)
166  {
167  ObjectType *tmp = this->m_Pointer;
168  this->m_Pointer = other.m_Pointer;
169  other.m_Pointer = tmp;
170  }
171 
172 private:
175 
176  void Register()
177  {
178  if ( m_Pointer ) { m_Pointer->Register(); }
179  }
180 
182  {
183  if ( m_Pointer ) { m_Pointer->UnRegister(); }
184  }
185 };
186 
187 template< typename T >
188 std::ostream & operator<<(std::ostream & os, SmartPointer< T > p)
189 {
190  p.Print(os);
191  return os;
192 }
193 
194 template<typename T>
195 inline void swap( SmartPointer<T> &a, SmartPointer<T> &b )
196 {
197  a.Swap(b);
198 }
199 
200 } // end namespace itk
201 
202 #ifdef ITK_SP_NULLPTR
203 #undef ITK_SP_NULLPTR
204 #endif
205 #ifdef ITK_SP_NOEXECPT
206 #undef ITK_SP_NOEXCEPT
207 #endif
208 
209 #endif
void Swap(SmartPointer &other)
SmartPointer & operator=(const SmartPointer &r)
void swap(SmartPointer &other)
#define ITK_SP_NULLPTR
bool operator<(const SmartPointer &r) const
#define ITK_SP_NOEXCEPT
TObjectType ObjectType
SmartPointer(ObjectType *p)
SmartPointer(const SmartPointer< ObjectType > &p)
ObjectType * GetPointer() const
void UnRegister() ITK_SP_NOEXCEPT
bool IsNotNull() const
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:214
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