SharedPtr.hpp
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <vector>
9 #include <mutex>
10 #include <ememory/debug.hpp>
11 #include <ememory/Counter.hpp>
13 
14 namespace ememory {
15  template<typename> class WeakPtr;
16  template<typename> class EnableSharedFromThis;
17  using deleterCall = std::function<void(void* _data)>;
33  template<typename EMEMORY_TYPE>
34  class SharedPtr {
35  friend class WeakPtr<EMEMORY_TYPE>;
36  private:
37  EMEMORY_TYPE* m_element;
38  ememory::Counter* m_counter;
39  deleterCall m_deleter;
40 
44  deleterCall createDeleter() const {
45  return [](void* _data) { delete((EMEMORY_TYPE*)_data);};
46  }
47  public:
48  #ifndef PARSE_DOXYGEN
49  template<class EMEMORY_TYPE2,
50  typename std::enable_if< std::is_same<EMEMORY_TYPE2, EMEMORY_TYPE>::value
51  && std::is_base_of<ememory::EnableSharedFromThisBase, EMEMORY_TYPE2>::value
52  , int>::type = 0>
53  SharedPtr(EMEMORY_TYPE2* _element);
54  template<class EMEMORY_TYPE2,
55  typename std::enable_if< std::is_same<EMEMORY_TYPE2, EMEMORY_TYPE>::value
56  && !std::is_base_of<ememory::EnableSharedFromThisBase, EMEMORY_TYPE2>::value
57  , int>::type = 0>
58  SharedPtr(EMEMORY_TYPE2* _element);
59  #else
60 
64  SharedPtr(EMEMORY_TYPE2* _element);
65  #endif
66  public:
70  SharedPtr(std::nullptr_t);
74  SharedPtr();
80  SharedPtr(EMEMORY_TYPE* _obj, ememory::Counter* _counter);
94  ~SharedPtr();
105  SharedPtr& operator= (std::nullptr_t);
106  public:
107  #ifndef PARSE_DOXYGEN
108  template<class EMEMORY_TYPE2,
109  typename std::enable_if< std::is_base_of<EMEMORY_TYPE, EMEMORY_TYPE2>::value
110  , int>::type = 0>
111  SharedPtr(const SharedPtr<EMEMORY_TYPE2>& _obj);
112  template<class EMEMORY_TYPE2,
113  typename std::enable_if< std::is_base_of<EMEMORY_TYPE, EMEMORY_TYPE2>::value
114  , int>::type = 0>
116  #endif
117  public:
121  void reset();
126  int64_t useCount() const;
131  bool operator==(std::nullptr_t) const;
137  template<class EMEMORY_TYPE2>
138  bool operator==(const SharedPtr<EMEMORY_TYPE2>& _obj) const;
143  bool operator!=(std::nullptr_t) const;
149  template<class EMEMORY_TYPE2>
150  bool operator!=(const SharedPtr<EMEMORY_TYPE2>& _obj) const;
155  const EMEMORY_TYPE* get() const;
160  EMEMORY_TYPE* get();
165  const EMEMORY_TYPE* operator->() const;
170  EMEMORY_TYPE* operator->();
175  const EMEMORY_TYPE& operator*() const;
180  EMEMORY_TYPE& operator*();
185  void swap(SharedPtr<EMEMORY_TYPE>& _obj);
191  return m_counter;
192  }
197  deleterCall getDeleter() const {
198  return m_deleter;
199  }
200  // TODO: unique
201  // TODO: bool
202  };
203 }
204 
205 #include <ememory/details/SharedPtr.hxx>
206 
WeakPtr is an interface that lose the data pointer when all SharedPtr as been released.
Definition: SharedPtr.hpp:15
const EMEMORY_TYPE * operator->() const
Const dereferences the stored pointer.
SharedPtr()
Contructor empty.
~SharedPtr()
Destructor.
bool operator!=(std::nullptr_t) const
Check if the SharedPtr have NOT an internal data (nullptr)
ememory::Counter * getCounter() const
Get Counter pointer.
Definition: SharedPtr.hpp:190
bool operator==(std::nullptr_t) const
Check if the SharedPtr have an internal data (not nullptr)
void reset()
Reset the SharedPtr ==> Remove data if needed.
Couter is an important part of the SharedPtr/WeakPtr implementation. This use a simple refcounting me...
Definition: Counter.hpp:18
const EMEMORY_TYPE & operator*() const
Get a const reference on the data.
Enable the acces of the self sharedPtr inside an object (note: not availlable in contructor and destr...
Definition: EnableSharedFromThis.hpp:23
int64_t useCount() const
Get the number of conencted SharedPtr.
void swap(SharedPtr< EMEMORY_TYPE > &_obj)
Swap 2 Object inside the SharedPtr.
deleterCall getDeleter() const
Get deleter function of the data pointer.
Definition: SharedPtr.hpp:197
Ememory is a namespace to represent the.
Definition: Counter.hpp:14
SharedPtr & operator=(const SharedPtr< EMEMORY_TYPE > &_obj)
Asignement operator.
ememory::SharedPtr is a smart pointer that retains shared ownership of an object through a pointer...
Definition: SharedPtr.hpp:34