MimmoSharedPointer.tpp
1 /*---------------------------------------------------------------------------*\
2  *
3  * mimmo
4  *
5  * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6  *
7  * -------------------------------------------------------------------------
8  * License
9  * This file is part of mimmo.
10  *
11  * mimmo is free software: you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License v3 (LGPL)
13  * as published by the Free Software Foundation.
14  *
15  * mimmo is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with mimmo. If not, see <http://www.gnu.org/licenses/>.
22  *
23 \*---------------------------------------------------------------------------*/
24 
25 
32 template< class T>
34  T* object;
35  std::size_t* counter;
36  buffer >> object;
37  buffer >> counter;
38  element._reset(object, counter);
39  return buffer;
40 };
41 
48 template< class T >
50 {
51  buffer << element.m_object;
52  buffer << element.m_counter;
53  return buffer;
54 };
55 
56 
57 namespace mimmo{
58 
62 template<class O>
64 {
65  _init(nullptr, nullptr);
66 }
67 
75 template<class O>
77 {
78  _init(object, nullptr);
79 }
80 
86 template<class O>
88  _decrement();
89 }
90 
96 template<class O>
98 {
99  m_object = other.m_object;
100  m_counter = other.m_counter;
101  if (m_counter){
102  (*m_counter)++;
103  }
104 }
105 
112 template<class O>
114 {
115  //attach the new contents to the class
116  m_object = other.m_object;
117  m_counter = other.m_counter;
118 
119  //empty the target source contents. They are owned by this class now
120  other.m_object = nullptr;
121  other.m_counter = nullptr;
122 }
123 
131 template<class O>
133 {
134  //check to avoid useless operations in case of auto-copy assignment.
135  if(&other == this)
136  return *this;
137 
138  //decrement the old contents in the class(if any).
139  _decrement();
140 
141  //assign new contents from source
142  m_object = other.m_object;
143  m_counter = other.m_counter;
144 
145  //incrementing my counter.
146  if (m_counter){
147  (*m_counter)++;
148  }
149  return *this;
150 }
151 
160 template<class O>
162 {
163  //check to avoid auto-move assignment.
164  if(&other == this)
165  return *this;
166 
167  //decrement the old contents in the class(if any).
168  _decrement();
169 
170  //attach the new contents to the class
171  m_object = other.m_object;
172  m_counter = other.m_counter;
173 
174  //empty the target source contents. They are owned by this class now
175  other.m_object = nullptr;
176  other.m_counter = nullptr;
177 
178  return *this;
179 }
180 
186  template<class O>
188 {
189  std::swap(m_object, other.m_object);
190  std::swap(m_counter, other.m_counter);
191 }
192 
193 
197 template<class O>
199 {
200  return m_object;
201 }
202 
208 template<class O>
210 {
211  if(m_counter) return *(m_counter);
212  else return 0;
213 }
214 
215 
225 template<class O>
227 {
228  _reset(object, nullptr);
229 }
230 
235 template<class O>
237 {
238  return *(m_object);
239 }
240 
245 template<class O>
247 {
248  return m_object;
249 };
250 
255 template<class O>
257 {
258  return !m_object;
259 }
260 
265 template<class O>
267 {
268  return bool(m_object);
269 }
270 
276 template<class O>
277 void MimmoSharedPointer<O>::_init(O* object, std::size_t* counter )
278 {
279  m_object = object;
280  m_counter = counter;
281  if (m_object){
282  if(!m_counter)
283  m_counter = new std::size_t(0);
284  (*m_counter)++;
285  }
286  else{
287  m_counter = nullptr;
288  }
289 };
290 
297 template<class O>
298 void MimmoSharedPointer<O>::_reset(O* object, std::size_t* counter )
299 {
300  _decrement(),
301  _init(object, counter);
302 };
303 
308 template<class O>
310  if (m_counter){
311  ( *m_counter )-- ;
312  if ( (*m_counter) == 0 ){
313  delete m_object;
314  delete m_counter;
315  }
316  }
317  m_object = nullptr;
318  m_counter = nullptr;
319 }
320 
321 } //end namespace mimmo
322 
323 namespace std{
324 
329 template<class O>
330 size_t hash<mimmo::MimmoSharedPointer<O>>::operator()(const mimmo::MimmoSharedPointer<O> & obj) const
331 {
332  return hash<const O*>()(obj.get());
333 }
334 
339 template<class O>
340 size_t hash<mimmo::MimmoSharedPointer<O>>::operator()(mimmo::MimmoSharedPointer<O> & obj)
341 {
342  return hash<O*>()(obj.get());
343 }
344 
345 } // end namespace std
std::size_t getCounter() const
MimmoSharedPointer & operator=(const MimmoSharedPointer< O > &other)
void _init(O *object=nullptr, std::size_t *counter=nullptr)
mimmo custom derivation of bitpit OBinaryStream (see relative doc)
mimmo::OBinaryStream & operator<<(mimmo::OBinaryStream &buf, const std::string &element)
mimmo::IBinaryStream & operator>>(mimmo::IBinaryStream &buffer, mimmo::MimmoSharedPointer< T > &element)
void swap(MimmoSharedPointer< O > &other) noexcept
void reset(O *object=nullptr)
mimmo custom derivation of bitpit IBinaryStream (see relative doc)
MimmoSharedPointer is a custom implementation of shared pointer.
void _reset(O *object=nullptr, std::size_t *counter=nullptr)