DGtal
0.6.devel
Main Page
Related Pages
Modules
Namespaces
Data Structures
Examples
All
Data Structures
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Groups
Pages
src
DGtal
base
OwningOrAliasingPtr.ih
1
30
31
#include <cstdlib>
33
35
// IMPLEMENTATION of inline methods.
37
39
// ----------------------- Standard services ------------------------------
40
41
42
template
<
typename
T>
43
inline
44
DGtal::OwningOrAliasingPtr<T>::OwningOrAliasingPtr
(
const
T& aValue)
45
:myPtr( new T(aValue) ), myFlagIsOwning( true )
46
{
47
}
48
49
template
<
typename
T>
50
inline
51
DGtal::OwningOrAliasingPtr<T>::OwningOrAliasingPtr
(T* aPtr,
bool
aFlagIsOwning)
52
:myPtr( aPtr ), myFlagIsOwning( aFlagIsOwning )
53
{
54
}
55
56
template
<
typename
T>
57
inline
58
DGtal::OwningOrAliasingPtr<T>::OwningOrAliasingPtr
(
const
DGtal::OwningOrAliasingPtr<T>
& other)
59
: myFlagIsOwning( other.myFlagIsOwning )
60
{
61
ASSERT(
myFlagIsOwning
== other.
myFlagIsOwning
);
62
if
(
myFlagIsOwning
)
63
myPtr
=
new
Value
( *other.
myPtr
);
//copy of the data
64
else
65
myPtr
= other.
myPtr
;
//copy of the alias
66
}
67
68
template
<
typename
T>
69
inline
70
DGtal::OwningOrAliasingPtr<T>
&
71
DGtal::OwningOrAliasingPtr<T>::operator=
(
const
DGtal::OwningOrAliasingPtr<T>
& other)
72
{
73
if
(
this
!= &other )
74
{
75
//free old data (if needed)
76
if
(myFlagIsOwning)
77
delete
(myPtr);
78
//acquire new data
79
myFlagIsOwning = other.
myFlagIsOwning
;
80
if
(myFlagIsOwning)
81
myPtr =
new
Value
( *other.
myPtr
);
//copy of the data
82
else
83
myPtr = other.
myPtr
;
//copy of the alias
84
}
85
return
*
this
;
86
}
87
88
template
<
typename
T>
89
inline
90
DGtal::OwningOrAliasingPtr<T>::~OwningOrAliasingPtr
()
91
{
92
//free if @a myPtr owns the data
93
if
(myFlagIsOwning)
94
delete
(myPtr);
95
}
96
98
// Interface - public :
99
100
template
<
typename
T>
101
inline
102
typename
DGtal::OwningOrAliasingPtr<T>::Pointer
103
DGtal::OwningOrAliasingPtr<T>::get
()
const
104
{
105
return
myPtr;
106
}
107
108
template
<
typename
T>
109
inline
110
typename
DGtal::OwningOrAliasingPtr<T>::Pointer
111
DGtal::OwningOrAliasingPtr<T>::operator->
()
const
112
{
113
return
myPtr;
114
}
115
116
template
<
typename
T>
117
inline
118
typename
DGtal::OwningOrAliasingPtr<T>::Reference
119
DGtal::OwningOrAliasingPtr<T>::operator*
()
const
120
{
121
ASSERT( myPtr != NULL );
122
return
*myPtr;
123
}
124
125
template
<
typename
T>
126
inline
127
bool
128
DGtal::OwningOrAliasingPtr<T>::isOwning
()
const
129
{
130
return
myFlagIsOwning;
131
}
132
133
template
<
typename
T>
134
inline
135
bool
136
DGtal::OwningOrAliasingPtr<T>::isValid
()
const
137
{
138
return
true
;
139
}
140
141
template
<
typename
T>
142
inline
143
void
144
DGtal::OwningOrAliasingPtr<T>::selfDisplay
( std::ostream & out )
const
145
{
146
out <<
"[OwningOrAliasingPtr]"
;
147
if
(myPtr != NULL)
148
out <<
" "
<< myPtr <<
" "
<< (*myPtr);
149
else
150
out <<
" "
<< myPtr <<
" "
<<
"NULL"
;
151
}
152
153
155
// Implementation of inline functions //
156
157
template
<
typename
T>
158
inline
159
std::ostream&
160
DGtal::operator<<
( std::ostream & out,
161
const
OwningOrAliasingPtr<T>
&
object
)
162
{
163
object
.selfDisplay( out );
164
return
out;
165
}
166
167
// //
169
170
Generated on Wed Dec 19 2012 19:10:32 for DGtal by
1.8.1.1