ITK  4.7.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  bool IsNull() const
94  { return m_Pointer == ITK_SP_NULLPTR; }
96 
98  template< typename TR >
99  bool operator==(TR r) const
100  { return ( m_Pointer == static_cast< const ObjectType * >( r ) ); }
101 
102  template< typename TR >
103  bool operator!=(TR r) const
104  { return ( m_Pointer != static_cast< const ObjectType * >( r ) ); }
105 
108  { return m_Pointer; }
109 
111  bool operator<(const SmartPointer & r) const
112  { return (void *)m_Pointer < (void *)r.m_Pointer; }
113 
115  bool operator>(const SmartPointer & r) const
116  { return (void *)m_Pointer > (void *)r.m_Pointer; }
117 
119  bool operator<=(const SmartPointer & r) const
120  { return (void *)m_Pointer <= (void *)r.m_Pointer; }
121 
123  bool operator>=(const SmartPointer & r) const
124  { return (void *)m_Pointer >= (void *)r.m_Pointer; }
125 
127  // cppcheck-suppress operatorEqVarError
129  { return this->operator=( r.GetPointer() ); }
130 
133  {
134  SmartPointer temp(r);
135  temp.swap(*this);
137 
138  return *this;
139  }
140 
142  ObjectType * Print(std::ostream & os) const
143  {
144  if( this->IsNull() )
145  {
146  os << "(null)";
147  }
148  else
149  {
150  // This prints the object pointed to by the pointer
151  ( *m_Pointer ).Print(os);
152  }
153  return m_Pointer;
154  }
156 
157  void swap(SmartPointer &other)
158  {
159  ObjectType *tmp = this->m_Pointer;
160  this->m_Pointer = other.m_Pointer;
161  other.m_Pointer = tmp;
162  }
163 
164 private:
167 
168  void Register()
169  {
170  if ( m_Pointer ) { m_Pointer->Register(); }
171  }
172 
174  {
175  if ( m_Pointer ) { m_Pointer->UnRegister(); }
176  }
177 };
178 
179 template< typename T >
180 std::ostream & operator<<(std::ostream & os, SmartPointer< T > p)
181 {
182  p.Print(os);
183  return os;
184 }
185 
186 template<typename T>
187 inline void swap( SmartPointer<T> &a, SmartPointer<T> &b )
188 {
189  a.swap(b);
190 }
191 
192 } // end namespace itk
193 
194 #ifdef ITK_SP_NULLPTR
195 #undef ITK_SP_NULLPTR
196 #endif
197 #ifdef ITK_SP_NOEXECPT
198 #undef ITK_SP_NOEXCEPT
199 #endif
200 
201 #endif
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:207
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