Orfeo Toolbox
3.16
Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
OTB
Utilities
ITK
Code
Common
itkSmartPointer.h
Go to the documentation of this file.
1
/*=========================================================================
2
3
Program: Insight Segmentation & Registration Toolkit
4
Module: $RCSfile: itkSmartPointer.h,v $
5
Language: C++
6
Date: $Date: 2009-03-03 15:09:28 $
7
Version: $Revision: 1.40 $
8
9
Copyright (c) Insight Software Consortium. All rights reserved.
10
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12
This software is distributed WITHOUT ANY WARRANTY; without even
13
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14
PURPOSE. See the above copyright notices for more information.
15
16
=========================================================================*/
17
#ifndef __itkSmartPointer_h
18
#define __itkSmartPointer_h
19
20
#include <iostream>
21
22
namespace
itk
23
{
24
41
template
<
class
TObjectType>
42
class
SmartPointer
43
{
44
public
:
45
typedef
TObjectType
ObjectType
;
46
48
SmartPointer
()
49
{
m_Pointer
= 0; }
50
52
SmartPointer
(
const
SmartPointer<ObjectType>
&p):
53
m_Pointer
(p.
m_Pointer
)
54
{ this->
Register
(); }
55
57
SmartPointer
(
ObjectType
*p):
58
m_Pointer
(p)
59
{ this->
Register
(); }
60
62
~SmartPointer
()
63
{
64
this->
UnRegister
();
65
m_Pointer
= 0;
66
}
67
69
ObjectType
*
operator ->
()
const
70
{
return
m_Pointer
; }
71
73
operator
ObjectType
* ()
const
74
{
return
m_Pointer
; }
75
77
bool
IsNotNull
()
const
78
{
return
m_Pointer
!= 0; }
79
bool
IsNull
()
const
80
{
return
m_Pointer
== 0; }
81
83
template
<
typename
TR>
84
bool
operator ==
( TR r )
const
85
{
return
(
m_Pointer
== static_cast<const ObjectType*>(r) ); }
86
87
template
<
typename
TR>
88
bool
operator !=
( TR r )
const
89
{
return
(
m_Pointer
!= static_cast<const ObjectType*>(r) ); }
90
92
ObjectType
*
GetPointer
()
const
93
{
return
m_Pointer
; }
94
96
bool
operator <
(
const
SmartPointer
&r)
const
97
{
return
(
void
*)
m_Pointer
< (
void
*) r.
m_Pointer
; }
98
100
bool
operator >
(
const
SmartPointer
&r)
const
101
{
return
(
void
*)
m_Pointer
> (
void
*) r.
m_Pointer
; }
102
104
bool
operator <=
(
const
SmartPointer
&r)
const
105
{
return
(
void
*)
m_Pointer
<= (
void
*) r.
m_Pointer
; }
106
108
bool
operator >=
(
const
SmartPointer
&r)
const
109
{
return
(
void
*)
m_Pointer
>= (
void
*) r.
m_Pointer
; }
110
112
SmartPointer
&
operator =
(
const
SmartPointer
&r)
113
{
return
this->
operator =
(r.
GetPointer
()); }
114
116
SmartPointer
&
operator =
(
ObjectType
*r)
117
{
118
if
(
m_Pointer
!= r)
119
{
120
ObjectType
* tmp =
m_Pointer
;
//avoid recursive unregisters by retaining temporarily
121
m_Pointer
= r;
122
this->
Register
();
123
if
( tmp ) { tmp->UnRegister(); }
124
}
125
return
*
this
;
126
}
127
129
ObjectType
*
Print
(std::ostream& os)
const
130
{
131
// This prints the object pointed to by the pointer
132
(*m_Pointer).Print(os);
133
return
m_Pointer
;
134
}
135
136
private
:
138
ObjectType
*
m_Pointer
;
139
140
void
Register
()
141
{
142
if
(
m_Pointer
) {
m_Pointer
->Register(); }
143
}
144
145
void
UnRegister
()
146
{
147
if
(
m_Pointer
) {
m_Pointer
->UnRegister(); }
148
}
149
};
150
151
152
template
<
typename
T>
153
std::ostream& operator<< (std::ostream& os, SmartPointer<T> p)
154
{
155
p.Print(os);
156
return
os;
157
}
158
159
}
// end namespace itk
160
161
#endif
Generated at Sun Feb 3 2013 00:07:40 for
Orfeo Toolbox
with
doxygen 1.8.1.1