ITK  4.3.0
Insight Segmentation and Registration Toolkit
itkWeakPointer.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 __itkWeakPointer_h
19 #define __itkWeakPointer_h
20 
21 #include "itkMacro.h"
22 #include <iostream>
23 
24 namespace itk
25 {
43 template< class TObjectType >
44 class ITK_EXPORT WeakPointer
45 {
46 public:
48  typedef TObjectType ObjectType;
49 
52  { m_Pointer = 0; }
53 
55  WeakPointer (const WeakPointer< ObjectType > & p):m_Pointer(p.m_Pointer) {}
56 
58  WeakPointer (ObjectType *p):m_Pointer(p) {}
59 
62  { m_Pointer = 0; }
63 
65  ObjectType * operator->() const
66  { return m_Pointer; }
67 
69  operator ObjectType *() const
70  { return m_Pointer; }
71 
73  template< typename R >
74  bool operator==(R r) const
75  {
76  return ( m_Pointer == (ObjectType *)r );
77  }
78 
79  template< typename R >
80  bool operator!=(R r) const
81  {
82  return ( m_Pointer != (ObjectType *)r );
83  }
84 
86  ObjectType * GetPointer() const
87  { return m_Pointer; }
88 
90  bool operator<(const WeakPointer & r) const
91  { return (void *)m_Pointer < (void *)r.m_Pointer; }
92 
94  bool operator>(const WeakPointer & r) const
95  { return (void *)m_Pointer > (void *)r.m_Pointer; }
96 
98  bool operator<=(const WeakPointer & r) const
99  { return (void *)m_Pointer <= (void *)r.m_Pointer; }
100 
102  bool operator>=(const WeakPointer & r) const
103  { return (void *)m_Pointer >= (void *)r.m_Pointer; }
104 
106  // cppcheck-suppress operatorEqVarError
107  WeakPointer & operator=(const WeakPointer & r)
108  { return this->operator=( r.GetPointer() ); }
109 
111  WeakPointer & operator=(ObjectType *r)
112  {
113  m_Pointer = r;
114  return *this;
115  }
116 
118  ObjectType * Print(std::ostream & os) const
119  {
120  // This prints the object pointed to by the pointer
121  ( *m_Pointer ).Print(os);
122  return m_Pointer;
123  }
125 
126 private:
129 };
130 
131 template< typename T >
132 std::ostream & operator<<(std::ostream & os, WeakPointer< T > p)
133 {
134  p.Print(os);
135  return os;
136 }
137 } // end namespace itk
138 
139 #endif
140