WeakPtr.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>
12 
13 namespace ememory {
14  template<typename> class SharedPtr;
15  template<typename> class EnableSharedFromThis;
16  template<typename EMEMORY_TYPE>
20  class WeakPtr {
21  friend class EnableSharedFromThis<EMEMORY_TYPE>;
22  friend class SharedPtr<EMEMORY_TYPE>;
23  private:
24  EMEMORY_TYPE* m_element;
25  ememory::Counter* m_counter;
26  public:
30  WeakPtr();
34  WeakPtr(std::nullptr_t);
35  private:
40  WeakPtr(EMEMORY_TYPE* _element); // this is only for enable shared from this ...
41  public:
42  ~WeakPtr();
47  WeakPtr(const WeakPtr<EMEMORY_TYPE>& _obj);
53  WeakPtr<EMEMORY_TYPE>& operator= (const WeakPtr<EMEMORY_TYPE>& _obj);
58  WeakPtr(WeakPtr&& _obj);
63  WeakPtr(const SharedPtr<EMEMORY_TYPE>& _obj);
69  WeakPtr<EMEMORY_TYPE>& operator= (const SharedPtr<EMEMORY_TYPE>& _obj);
74  WeakPtr<EMEMORY_TYPE>& operator= (std::nullptr_t);
79  template<class EMEMORY_TYPE2,
80  typename std::enable_if< std::is_void<EMEMORY_TYPE>::value
81  && !std::is_void<EMEMORY_TYPE2>::value
82  , int>::type = 0>
83  WeakPtr(const SharedPtr<EMEMORY_TYPE2>& _obj);
89  template<class EMEMORY_TYPE2,
90  typename std::enable_if< std::is_void<EMEMORY_TYPE>::value
91  && !std::is_void<EMEMORY_TYPE2>::value
92  , int>::type = 0>
93  WeakPtr<EMEMORY_TYPE>& operator= (const SharedPtr<EMEMORY_TYPE2>& _obj);
94  public:
95  /*
96  template<class EMEMORY_TYPE2,
97  typename std::enable_if< std::is_base_of<EMEMORY_TYPE, EMEMORY_TYPE2>::value
98  , int>::type = 0>
99  WeakPtr(const WeakPtr<EMEMORY_TYPE2>& _obj);
100  template<class EMEMORY_TYPE2,
101  typename std::enable_if< std::is_base_of<EMEMORY_TYPE, EMEMORY_TYPE2>::value
102  , int>::type = 0>
103  WeakPtr& operator= (const WeakPtr<EMEMORY_TYPE2>& _obj);
104  */
105  public:
109  void reset();
114  int useCount() const;
120  bool expired() const;
131  bool operator==(const WeakPtr& _obj);
136  bool operator==(std::nullptr_t) const;
142  bool operator!=(const WeakPtr& _obj);
147  bool operator!=(std::nullptr_t) const;
152  void swap(WeakPtr& _obj);
158  return m_counter;
159  }
160  };
161 }
162 
163 #include <ememory/details/WeakPtr.hxx>
bool operator!=(const WeakPtr &_obj)
Check if two WeakPtr are different data.
ememory::SharedPtr< EMEMORY_TYPE > lock()
Lock in SharedPtr the current Weak pointer.
bool operator==(const WeakPtr &_obj)
Check if two WeakPtr are the same data.
WeakPtr< EMEMORY_TYPE > & operator=(const WeakPtr< EMEMORY_TYPE > &_obj)
Const copy asignement.
bool expired() const
Get the status of the pointer.
int useCount() const
Get the number of conencted SharedPtr.
void swap(WeakPtr &_obj)
Swap 2 Object inside the WeakPtr.
Couter is an important part of the SharedPtr/WeakPtr implementation. This use a simple refcounting me...
Definition: Counter.hpp:18
ememory::Counter * getCounter() const
Get Counter pointer.
Definition: WeakPtr.hpp:157
Ememory is a namespace to represent the.
Definition: Counter.hpp:14
ememory::SharedPtr is a smart pointer that retains shared ownership of an object through a pointer...
Definition: SharedPtr.hpp:34
void reset()
Reset the WeakPtr ==> Remove data if needed.
WeakPtr()
Empty Contructor.