ITK  4.8.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  void swap(SmartPointer &other)
159  {
160  ObjectType *tmp = this->m_Pointer;
161  this->m_Pointer = other.m_Pointer;
162  other.m_Pointer = tmp;
163  }
164 
165 private:
168 
169  void Register()
170  {
171  if ( m_Pointer ) { m_Pointer->Register(); }
172  }
173 
175  {
176  if ( m_Pointer ) { m_Pointer->UnRegister(); }
177  }
178 };
179 
180 template< typename T >
181 std::ostream & operator<<(std::ostream & os, SmartPointer< T > p)
182 {
183  p.Print(os);
184  return os;
185 }
186 
187 template<typename T>
188 inline void swap( SmartPointer<T> &a, SmartPointer<T> &b )
189 {
190  a.swap(b);
191 }
192 
193 } // end namespace itk
194 
195 #ifdef ITK_SP_NULLPTR
196 #undef ITK_SP_NULLPTR
197 #endif
198 #ifdef ITK_SP_NOEXECPT
199 #undef ITK_SP_NOEXCEPT
200 #endif
201 
202 #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