Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  
igtlSmartPointer.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: The OpenIGTLink Library
4 Language: C++
5 Web page: http://openigtlink.org/
6
7 Copyright (c) Insight Software Consortium. All rights reserved.
8
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notices for more information.
12
13=========================================================================*/
14/*=========================================================================
15
16 Program: Insight Segmentation & Registration Toolkit
17 Module: $RCSfile: itkSmartPointer.h,v $
18 Language: C++
19 Date: $Date: 2008-12-22 19:05:42 -0500 (Mon, 22 Dec 2008) $
20 Version: $Revision: 3460 $
21
22 Copyright (c) Insight Software Consortium. All rights reserved.
23 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
24
25 This software is distributed WITHOUT ANY WARRANTY; without even
26 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
27 PURPOSE. See the above copyright notices for more information.
28
29=========================================================================*/
30#ifndef __igtlSmartPointer_h
31#define __igtlSmartPointer_h
32
33#include "igtlMacro.h"
34#include <iostream>
35
36namespace igtl
37{
38
55template <class TObjectType>
57{
58public:
59 typedef TObjectType ObjectType;
60
63 { m_Pointer = 0; }
64
67 m_Pointer(p.m_Pointer)
68 { this->Register(); }
69
72 m_Pointer(p)
73 { this->Register(); }
74
77 {
78 this->UnRegister();
79 m_Pointer = 0;
80 }
82
84 ObjectType *operator -> () const
85 { return m_Pointer; }
86
88 operator ObjectType * () const
89 { return m_Pointer; }
90
92 bool IsNotNull() const
93 { return m_Pointer != 0; }
94 bool IsNull() const
95 { return m_Pointer == 0; }
97
99 template <typename R>
100 bool operator == ( R r ) const
101 { return (m_Pointer == static_cast<const ObjectType*>(r) ); }
102
103 template <typename R>
104 bool operator != ( R r ) const
105 { return (m_Pointer != static_cast<const ObjectType*>(r) ); }
106
109 { return m_Pointer; }
110
112 bool operator < (const SmartPointer &r) const
113 { return (void*)m_Pointer < (void*) r.m_Pointer; }
114
116 bool operator > (const SmartPointer &r) const
117 { return (void*)m_Pointer > (void*) r.m_Pointer; }
118
120 bool operator <= (const SmartPointer &r) const
121 { return (void*)m_Pointer <= (void*) r.m_Pointer; }
122
124 bool operator >= (const SmartPointer &r) const
125 { return (void*)m_Pointer >= (void*) r.m_Pointer; }
126
128 SmartPointer &operator = (const SmartPointer &r)
129 { return this->operator = (r.GetPointer()); }
130
132 SmartPointer &operator = (ObjectType *r)
133 {
134 if (m_Pointer != r)
135 {
136 ObjectType* tmp = m_Pointer; //avoid recursive unregisters by retaining temporarily
137 m_Pointer = r;
138 this->Register();
139 if ( tmp ) { tmp->UnRegister(); }
140 }
141 return *this;
142 }
144
146 ObjectType *Print (std::ostream& os) const
147 {
148 // This prints the object pointed to by the pointer
149 (*m_Pointer).Print(os);
150 return m_Pointer;
151 }
153
154private:
156 ObjectType* m_Pointer;
157
158 void Register()
159 {
160 if(m_Pointer) { m_Pointer->Register(); }
161 }
162
163 void UnRegister()
164 {
165 if(m_Pointer) { m_Pointer->UnRegister(); }
166 }
167};
168
169
170template <typename T>
171std::ostream& operator<< (std::ostream& os, SmartPointer<T> p)
172{
173 p.Print(os);
174 return os;
175}
176
177} // end namespace igtl
178
179#endif
Implements transparent reference counting.
ObjectType * GetPointer() const
SmartPointer(ObjectType *p)
ObjectType * Print(std::ostream &os) const
SmartPointer(const SmartPointer< ObjectType > &p)
#define IGTL_EXPORT
The "igtl" namespace contains all OpenIGTLink classes. There are several nested namespaces within the...
std::ostream & operator<<(std::ostream &os, SmartPointer< T > p)

Generated for OpenIGTLink by Doxygen 1.9.8 written by Dimitri van Heesch, © 1997-2012