DGtal 1.3.0
Loading...
Searching...
No Matches
OwningOrAliasingPtr.ih
1/**
2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU Lesser General Public License as
4 * published by the Free Software Foundation, either version 3 of the
5 * License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *
15 **/
16
17/**
18 * @file OwningOrAliasingPtr.ih
19 * @author Tristan Roussillon (\c tristan.roussillon@liris.cnrs.fr )
20 * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21 *
22 * @date 2012/11/14
23 *
24 * Implementation of inline methods defined in OwningOrAliasingPtr.h
25 *
26 * This file is part of the DGtal library.
27 */
28
29
30//////////////////////////////////////////////////////////////////////////////
31#include <cstdlib>
32//////////////////////////////////////////////////////////////////////////////
33
34///////////////////////////////////////////////////////////////////////////////
35// IMPLEMENTATION of inline methods.
36///////////////////////////////////////////////////////////////////////////////
37
38///////////////////////////////////////////////////////////////////////////////
39// ----------------------- Standard services ------------------------------
40
41
42template <typename T>
43inline
44DGtal::OwningOrAliasingPtr<T>::OwningOrAliasingPtr(const T& aValue)
45 :myPtr( new T(aValue) ), myFlagIsOwning( true )
46{
47}
48
49template <typename T>
50inline
51DGtal::OwningOrAliasingPtr<T>::OwningOrAliasingPtr(T* aPtr, bool aFlagIsOwning)
52 :myPtr( aPtr ), myFlagIsOwning( aFlagIsOwning )
53{
54}
55
56template <typename T>
57inline
58DGtal::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
68template <typename T>
69inline
70DGtal::OwningOrAliasingPtr<T>&
71DGtal::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
88template <typename T>
89inline
90DGtal::OwningOrAliasingPtr<T>::~OwningOrAliasingPtr()
91{
92 //free if @a myPtr owns the data
93 if (myFlagIsOwning)
94 delete(myPtr);
95}
96
97///////////////////////////////////////////////////////////////////////////////
98// Interface - public :
99
100template <typename T>
101inline
102typename DGtal::OwningOrAliasingPtr<T>::Pointer
103DGtal::OwningOrAliasingPtr<T>::get() const
104{
105 return myPtr;
106}
107
108template <typename T>
109inline
110typename DGtal::OwningOrAliasingPtr<T>::Pointer
111DGtal::OwningOrAliasingPtr<T>::operator->() const
112{
113 return myPtr;
114}
115
116template <typename T>
117inline
118typename DGtal::OwningOrAliasingPtr<T>::Reference
119DGtal::OwningOrAliasingPtr<T>::operator*() const
120{
121 ASSERT( myPtr != NULL );
122 return *myPtr;
123}
124
125template <typename T>
126inline
127bool
128DGtal::OwningOrAliasingPtr<T>::isOwning() const
129{
130 return myFlagIsOwning;
131}
132
133template <typename T>
134inline
135bool
136DGtal::OwningOrAliasingPtr<T>::isValid() const
137{
138 return true;
139}
140
141template <typename T>
142inline
143void
144DGtal::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
154///////////////////////////////////////////////////////////////////////////////
155// Implementation of inline functions //
156
157template <typename T>
158inline
159std::ostream&
160DGtal::operator<< ( std::ostream & out,
161 const OwningOrAliasingPtr<T> & object )
162{
163 object.selfDisplay( out );
164 return out;
165}
166
167// //
168///////////////////////////////////////////////////////////////////////////////
169
170