Orfeo Toolbox  4.2
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  }
78 
81  { return m_Pointer; }
82 
84  operator ObjectType *() const
85  { return m_Pointer; }
86 
88  bool IsNotNull() const
89  { return m_Pointer != SP_ITK_NULLPTR; }
90  bool IsNull() const
91  { return m_Pointer == SP_ITK_NULLPTR; }
92 
94  template< typename TR >
95  bool operator==(TR r) const
96  { return ( m_Pointer == static_cast< const ObjectType * >( r ) ); }
97 
98  template< typename TR >
99  bool operator!=(TR r) const
100  { return ( m_Pointer != static_cast< const ObjectType * >( r ) ); }
101 
104  { return m_Pointer; }
105 
107  bool operator<(const SmartPointer & r) const
108  { return (void *)m_Pointer < (void *)r.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  // cppcheck-suppress operatorEqVarError
125  { return this->operator=( r.GetPointer() ); }
126 
129  {
130  SmartPointer temp(r);
131  temp.swap(*this);
132 
133  return *this;
134  }
135 
137  ObjectType * Print(std::ostream & os) const
138  {
139  if( this->IsNull() )
140  {
141  os << "(null)";
142  }
143  else
144  {
145  // This prints the object pointed to by the pointer
146  ( *m_Pointer ).Print(os);
147  }
148  return m_Pointer;
149  }
150 
151  void swap(SmartPointer &other)
152  {
153  ObjectType *tmp = this->m_Pointer;
154  this->m_Pointer = other.m_Pointer;
155  other.m_Pointer = tmp;
156  }
157 
158 private:
161 
162  void Register()
163  {
164  if ( m_Pointer ) { m_Pointer->Register(); }
165  }
166 
167  void UnRegister()
168  {
169  if ( m_Pointer ) { m_Pointer->UnRegister(); }
170  }
171 };
172 
173 template< typename T >
174 std::ostream & operator<<(std::ostream & os, SmartPointer< T > p)
175 {
176  p.Print(os);
177  return os;
178 }
179 
180 template<typename T>
182 {
183  a.swap(b);
184 }
185 
186 } // end namespace itk
187 
188 #endif

Generated at Sat Aug 30 2014 15:38:11 for Orfeo Toolbox with doxygen 1.8.3.1