MimmoSharedPointer.hpp
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 #ifndef __MIMMOSHAREDPOINTER_HPP__
25 #define __MIMMOSHAREDPOINTER_HPP__
26 
27 #include <mimmo_binary_stream.hpp>
28 
29 //forward declaration.
30 namespace mimmo{
31  template<class O>
33 }
34 
39 // Providing binary stream for class MimmoSharedPointer
40 template< class T>
42 
43 template< class T >
49 namespace mimmo{
50 
139 template<class O>
140 class MimmoSharedPointer
141 {
142 
143 template< class T>
144 friend mimmo::IBinaryStream& (::operator>>) (mimmo::IBinaryStream &buffer, mimmo::MimmoSharedPointer<T>& element);
145 
146 template< class T >
147 friend mimmo::OBinaryStream& (::operator<<) (mimmo::OBinaryStream &buffer, const mimmo::MimmoSharedPointer<T>& element);
148 
149 public:
150 
152  MimmoSharedPointer(O* object);
158 
159  //methods
160  void swap(MimmoSharedPointer<O>& other) noexcept;
161  O* get() const;
162  std::size_t getCounter() const;
163 
164  void reset(O* object = nullptr);
165 
166  //derefencing operators
167  O& operator*() const;
168  O* operator->() const;
169 
170  //boolean operators
171  bool operator!() const;
172  operator bool() const;
173 
174  //inline compare operators
179  inline bool operator==(const mimmo::MimmoSharedPointer<O>& rhs) const
180  {
181  return (get() == rhs.get());
182  };
183 
188  inline bool operator!=(const mimmo::MimmoSharedPointer<O>& rhs) const
189  {
190  return !(*this == rhs);
191  }
192 
197  inline bool operator==(const O* _rhs) const
198  {
199  // Check only pointer to object
200  return (m_object == _rhs);
201  };
202 
207  inline bool operator!=(const O* _rhs) const
208  {
209  return !(*this == _rhs);
210  }
211 
216  inline bool operator==(const void* _rhs) const
217  {
218  // Check only pointer to object
219  return (m_object == _rhs);
220  };
221 
226  inline bool operator!=(const void* _rhs) const
227  {
228  return !(*this == _rhs);
229  }
230 
235  inline bool operator==(const std::nullptr_t _rhs) const
236  {
237  // Check only pointer to object
238  return (m_object == _rhs);
239  };
240 
245  inline bool operator!=(const std::nullptr_t _rhs) const
246  {
247  return !(*this == _rhs);
248  }
249 
254  inline bool operator==(const long int lrhs) const
255  {
256  // Check only pointer to object
257  if (lrhs == 0){
258  return (!m_object);
259  }
260  else{
261  return bool(m_object);
262  }
263  };
264 
269  inline bool operator!=(const long int lrhs) const
270  {
271  return !(*this == lrhs);
272  }
273 
274 
275 protected:
276 
277  void _init(O* object = nullptr, std::size_t* counter = nullptr);
278  void _reset(O* object = nullptr, std::size_t* counter = nullptr);
279  void _decrement();
280 
281  O* m_object;
282  std::size_t* m_counter;
285 };
286 
287 } // end namespace mimmo
288 
289 
290 namespace std{
291 
295 template<class O>
296 struct hash<mimmo::MimmoSharedPointer<O> >
297 {
298  size_t
299  operator()(const mimmo::MimmoSharedPointer<O> & obj) const;
300 
301  size_t
302  operator()(mimmo::MimmoSharedPointer<O> & obj);
303 };
304 
305 } // end namespace std
306 
307 #include "MimmoSharedPointer.tpp"
308 
309 #endif /* __MIMMOSHAREDPOINTER_HPP__ */
std::size_t getCounter() const
MimmoSharedPointer & operator=(const MimmoSharedPointer< O > &other)
void _init(O *object=nullptr, std::size_t *counter=nullptr)
bool operator==(const O *_rhs) const
mimmo custom derivation of bitpit OBinaryStream (see relative doc)
bool operator!=(const std::nullptr_t _rhs) const
bool operator==(const void *_rhs) const
mimmo::OBinaryStream & operator<<(mimmo::OBinaryStream &buffer, const mimmo::MimmoSharedPointer< T > &element)
bool operator!=(const long int lrhs) const
bool operator==(const long int lrhs) const
bool operator!=(const void *_rhs) const
bool operator!=(const O *_rhs) const
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)
bool operator==(const std::nullptr_t _rhs) const
bool operator!=(const mimmo::MimmoSharedPointer< O > &rhs) const
MimmoSharedPointer is a custom implementation of shared pointer.
bool operator==(const mimmo::MimmoSharedPointer< O > &rhs) const
void _reset(O *object=nullptr, std::size_t *counter=nullptr)