DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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
45  :myPtr( new T(aValue) ), myFlagIsOwning( true )
46 {
47 }
48 
49 template <typename T>
50 inline
52  :myPtr( aPtr ), myFlagIsOwning( aFlagIsOwning )
53 {
54 }
55 
56 template <typename T>
57 inline
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
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
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
104 {
105  return myPtr;
106 }
107 
108 template <typename T>
109 inline
112 {
113  return myPtr;
114 }
115 
116 template <typename T>
117 inline
120 {
121  ASSERT( myPtr != NULL );
122  return *myPtr;
123 }
124 
125 template <typename T>
126 inline
127 bool
129 {
130  return myFlagIsOwning;
131 }
132 
133 template <typename T>
134 inline
135 bool
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