ITK  5.0.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  using ObjectType = TObjectType;
49 
52  { m_Pointer = nullptr; }
53 
55  WeakPointer (const WeakPointer< ObjectType > & p) = default;
56 
58  WeakPointer (WeakPointer< ObjectType > && p) = default;
59 
62 
64  ~WeakPointer () = default;
65 
68  { return m_Pointer; }
69 
71  operator ObjectType *() const
72  { return m_Pointer; }
73 
75  template< typename R >
76  bool operator==(R r) const
77  {
78  return ( m_Pointer == (ObjectType *)r );
79  }
80 
81  template< typename R >
82  bool operator!=(R r) const
83  {
84  return ( m_Pointer != (ObjectType *)r );
85  }
86 
89  { return m_Pointer; }
90 
92  bool IsNotNull() const
93  { return m_Pointer != nullptr; }
94 
96  bool IsNull() const
97  { return m_Pointer == nullptr; }
98 
100  bool operator<(const WeakPointer & r) const
101  { return (void *)m_Pointer < (void *)r.m_Pointer; }
102 
104  bool operator>(const WeakPointer & r) const
105  { return (void *)m_Pointer > (void *)r.m_Pointer; }
106 
108  bool operator<=(const WeakPointer & r) const
109  { return (void *)m_Pointer <= (void *)r.m_Pointer; }
110 
112  bool operator>=(const WeakPointer & r) const
113  { return (void *)m_Pointer >= (void *)r.m_Pointer; }
114 
116  // cppcheck-suppress operatorEqVarError
117  WeakPointer & operator=(const WeakPointer & r) = default;
118 
119  WeakPointer & operator=(WeakPointer && r) = default;
120 
123  {
124  m_Pointer = r;
125  return *this;
126  }
127 
129  ObjectType * Print(std::ostream & os) const
130  {
131  if( this->IsNull() )
132  {
133  os << "(null)";
134  }
135  else
136  {
137  // This prints the object pointed to by the pointer
138  ( *m_Pointer ).Print(os);
139  }
140  return m_Pointer;
141  }
143 
144 private:
147 };
148 
149 template< typename T >
150 std::ostream & operator<<(std::ostream & os, WeakPointer< T > p)
151 {
152  p.Print(os);
153  return os;
154 }
155 } // end namespace itk
156 
157 #endif
ObjectType * m_Pointer
bool IsNotNull() const
bool operator==(R r) const
bool IsNull() const
bool operator!=(R r) const
Implements a weak reference to an object.
ObjectType * Print(std::ostream &os) const
WeakPointer & operator=(const WeakPointer &r)=default
WeakPointer(ObjectType *p)
bool operator>(const WeakPointer &r) const
~WeakPointer()=default
bool operator>=(const WeakPointer &r) const
ObjectType * GetPointer() const
ObjectType * operator->() const
WeakPointer & operator=(ObjectType *r)
bool operator<(const WeakPointer &r) const
TObjectType ObjectType
bool operator<=(const WeakPointer &r) const