ITK  4.6.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< typename TObjectType >
45 {
46 public:
48  typedef TObjectType ObjectType;
49 
52  { m_Pointer = ITK_NULLPTR; }
53 
56 
59 
62  { m_Pointer = ITK_NULLPTR; }
63 
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 
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
108  { return this->operator=( r.GetPointer() ); }
109 
112  {
113  m_Pointer = r;
114  return *this;
115  }
116 
118  ObjectType * Print(std::ostream & os) const
119  {
120  if( this->IsNull() )
121  {
122  os << "(null)";
123  }
124  else
125  {
126  // This prints the object pointed to by the pointer
127  ( *m_Pointer ).Print(os);
128  }
129  return m_Pointer;
130  }
132 
133 private:
136 };
137 
138 template< typename T >
139 std::ostream & operator<<(std::ostream & os, WeakPointer< T > p)
140 {
141  p.Print(os);
142  return os;
143 }
144 } // end namespace itk
145 
146 #endif
ObjectType * m_Pointer
bool operator==(R r) const
WeakPointer(const WeakPointer< ObjectType > &p)
bool operator!=(R r) const
Implements a weak reference to an object.
ObjectType * Print(std::ostream &os) const
WeakPointer & operator=(const WeakPointer &r)
TObjectType ObjectType
WeakPointer(ObjectType *p)
bool operator>(const WeakPointer &r) const
bool operator>=(const WeakPointer &r) const
ObjectType * GetPointer() const
ObjectType * operator->() const
WeakPointer & operator=(ObjectType *r)
bool operator<(const WeakPointer &r) const
bool operator<=(const WeakPointer &r) const