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: Open IGT Link Library
4  Module: $HeadURL: http://svn.na-mic.org/NAMICSandBox/trunk/OpenIGTLink/Source/igtlSmartPointer.h $
5  Language: C++
6  Date: $Date: 2008-12-22 19:05:42 -0500 (Mon, 22 Dec 2008) $
7  Version: $Revision: 3460 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10 
11  This software is distributed WITHOUT ANY WARRANTY; without even
12  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13  PURPOSE. See the above copyright notices for more information.
14 
15 =========================================================================*/
16 /*=========================================================================
17 
18  Program: Insight Segmentation & Registration Toolkit
19  Module: $RCSfile: itkSmartPointer.h,v $
20  Language: C++
21  Date: $Date: 2008-12-22 19:05:42 -0500 (Mon, 22 Dec 2008) $
22  Version: $Revision: 3460 $
23 
24  Copyright (c) Insight Software Consortium. All rights reserved.
25  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
26 
27  This software is distributed WITHOUT ANY WARRANTY; without even
28  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
29  PURPOSE. See the above copyright notices for more information.
30 
31 =========================================================================*/
32 #ifndef __igtlSmartPointer_h
33 #define __igtlSmartPointer_h
34 
35 #include "igtlMacro.h"
36 #include <iostream>
37 
38 namespace igtl
39 {
40 
57 template <class TObjectType>
59 {
60 public:
61  typedef TObjectType ObjectType;
62 
65  { m_Pointer = 0; }
66 
69  m_Pointer(p.m_Pointer)
70  { this->Register(); }
71 
73  SmartPointer (ObjectType *p):
74  m_Pointer(p)
75  { this->Register(); }
76 
79  {
80  this->UnRegister();
81  m_Pointer = 0;
82  }
84 
86  ObjectType *operator -> () const
87  { return m_Pointer; }
88 
90  operator ObjectType * () const
91  { return m_Pointer; }
92 
94  bool IsNotNull() const
95  { return m_Pointer != 0; }
96  bool IsNull() const
97  { return m_Pointer == 0; }
99 
101  template <typename R>
102  bool operator == ( R r ) const
103  { return (m_Pointer == static_cast<const ObjectType*>(r) ); }
104 
105  template <typename R>
106  bool operator != ( R r ) const
107  { return (m_Pointer != static_cast<const ObjectType*>(r) ); }
108 
110  ObjectType *GetPointer () const
111  { return m_Pointer; }
112 
114  bool operator < (const SmartPointer &r) const
115  { return (void*)m_Pointer < (void*) r.m_Pointer; }
116 
118  bool operator > (const SmartPointer &r) const
119  { return (void*)m_Pointer > (void*) r.m_Pointer; }
120 
122  bool operator <= (const SmartPointer &r) const
123  { return (void*)m_Pointer <= (void*) r.m_Pointer; }
124 
126  bool operator >= (const SmartPointer &r) const
127  { return (void*)m_Pointer >= (void*) r.m_Pointer; }
128 
130  SmartPointer &operator = (const SmartPointer &r)
131  { return this->operator = (r.GetPointer()); }
132 
134  SmartPointer &operator = (ObjectType *r)
135  {
136  if (m_Pointer != r)
137  {
138  ObjectType* tmp = m_Pointer; //avoid recursive unregisters by retaining temporarily
139  m_Pointer = r;
140  this->Register();
141  if ( tmp ) { tmp->UnRegister(); }
142  }
143  return *this;
144  }
146 
148  ObjectType *Print (std::ostream& os) const
149  {
150  // This prints the object pointed to by the pointer
151  (*m_Pointer).Print(os);
152  return m_Pointer;
153  }
155 
156 private:
158  ObjectType* m_Pointer;
159 
160  void Register()
161  {
162  if(m_Pointer) { m_Pointer->Register(); }
163  }
164 
165  void UnRegister()
166  {
167  if(m_Pointer) { m_Pointer->UnRegister(); }
168  }
169 };
170 
171 
172 template <typename T>
173 std::ostream& operator<< (std::ostream& os, SmartPointer<T> p)
174 {
175  p.Print(os);
176  return os;
177 }
178 
179 } // end namespace igtl
180 
181 #endif
182 

Generated at Thu Mar 20 2014 16:32:06 for OpenIGTLink by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2000