DGtal  0.9.2
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 
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 
97 ///////////////////////////////////////////////////////////////////////////////
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 
154 ///////////////////////////////////////////////////////////////////////////////
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 // //
168 ///////////////////////////////////////////////////////////////////////////////
169 
170