ITK  4.6.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 SP_ITK_NULLPTR nullptr
49 #else
50 #define SP_ITK_NULLPTR NULL
51 #endif
52 template< typename TObjectType >
54 {
55 public:
56  typedef TObjectType ObjectType;
57 
61 
65  { this->Register(); }
66 
69  m_Pointer(p)
70  { this->Register(); }
71 
74  {
75  this->UnRegister();
77  }
79 
82  { return m_Pointer; }
83 
85  operator ObjectType *() const
86  { return m_Pointer; }
87 
89  bool IsNotNull() const
90  { return m_Pointer != SP_ITK_NULLPTR; }
91  bool IsNull() const
92  { return m_Pointer == SP_ITK_NULLPTR; }
94 
96  template< typename TR >
97  bool operator==(TR r) const
98  { return ( m_Pointer == static_cast< const ObjectType * >( r ) ); }
99 
100  template< typename TR >
101  bool operator!=(TR r) const
102  { return ( m_Pointer != static_cast< const ObjectType * >( r ) ); }
103 
106  { return 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  bool operator<=(const SmartPointer & r) const
118  { return (void *)m_Pointer <= (void *)r.m_Pointer; }
119 
121  bool operator>=(const SmartPointer & r) const
122  { return (void *)m_Pointer >= (void *)r.m_Pointer; }
123 
125  // cppcheck-suppress operatorEqVarError
127  { return this->operator=( r.GetPointer() ); }
128 
131  {
132  SmartPointer temp(r);
133  temp.swap(*this);
135 
136  return *this;
137  }
138 
140  ObjectType * Print(std::ostream & os) const
141  {
142  if( this->IsNull() )
143  {
144  os << "(null)";
145  }
146  else
147  {
148  // This prints the object pointed to by the pointer
149  ( *m_Pointer ).Print(os);
150  }
151  return m_Pointer;
152  }
154 
155  void swap(SmartPointer &other)
156  {
157  ObjectType *tmp = this->m_Pointer;
158  this->m_Pointer = other.m_Pointer;
159  other.m_Pointer = tmp;
160  }
161 
162 private:
165 
166  void Register()
167  {
168  if ( m_Pointer ) { m_Pointer->Register(); }
169  }
170 
171  void UnRegister()
172  {
173  if ( m_Pointer ) { m_Pointer->UnRegister(); }
174  }
175 };
176 
177 template< typename T >
178 std::ostream & operator<<(std::ostream & os, SmartPointer< T > p)
179 {
180  p.Print(os);
181  return os;
182 }
183 
184 template<typename T>
185 inline void swap( SmartPointer<T> &a, SmartPointer<T> &b )
186 {
187  a.swap(b);
188 }
189 
190 } // end namespace itk
191 
192 #endif
SmartPointer & operator=(const SmartPointer &r)
void swap(SmartPointer &other)
bool operator<(const SmartPointer &r) const
TObjectType ObjectType
SmartPointer(ObjectType *p)
SmartPointer(const SmartPointer< ObjectType > &p)
ObjectType * GetPointer() const
bool IsNotNull() const
#define SP_ITK_NULLPTR
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