ITK  4.8.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 IsNotNull() const
91  { return m_Pointer != ITK_NULLPTR; }
92 
94  bool IsNull() const
95  { return m_Pointer == ITK_NULLPTR; }
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  bool operator<=(const WeakPointer & r) const
107  { return (void *)m_Pointer <= (void *)r.m_Pointer; }
108 
110  bool operator>=(const WeakPointer & r) const
111  { return (void *)m_Pointer >= (void *)r.m_Pointer; }
112 
114  // cppcheck-suppress operatorEqVarError
116  { return this->operator=( r.GetPointer() ); }
117 
120  {
121  m_Pointer = r;
122  return *this;
123  }
124 
126  ObjectType * Print(std::ostream & os) const
127  {
128  if( this->IsNull() )
129  {
130  os << "(null)";
131  }
132  else
133  {
134  // This prints the object pointed to by the pointer
135  ( *m_Pointer ).Print(os);
136  }
137  return m_Pointer;
138  }
140 
141 private:
144 };
145 
146 template< typename T >
147 std::ostream & operator<<(std::ostream & os, WeakPointer< T > p)
148 {
149  p.Print(os);
150  return os;
151 }
152 } // end namespace itk
153 
154 #endif
ObjectType * m_Pointer
bool IsNotNull() const
bool operator==(R r) const
bool IsNull() 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