RavEngine
Loading...
Searching...
No Matches
WeakRef.hpp
1//
2// WeakSharedObjectRef.hpp
3// RavEngine_Static
4//
5// Copyright © 2020 Ravbug.
6//
7
8# pragma once
9#include <memory>
10
11template<typename T>
12using WeakRef = std::weak_ptr<T>;
13
17template <class T>
19 T * m_ptr; //for checking pointer validity
20 std::weak_ptr<T> m_wptr;
21public:
22 WeakPtrKey() :m_ptr(nullptr) {}
23 WeakPtrKey(std::weak_ptr<T> wptr) : m_ptr(wptr.lock().get()), m_wptr(wptr) {}
24
25 WeakPtrKey(std::shared_ptr<T> sptr):m_ptr(sptr.get()),m_wptr(sptr) {}
26
27 constexpr inline bool operator<(const WeakPtrKey<T> &other) const {
28 return m_ptr < other.m_ptr;
29
30 }
31
32 constexpr inline bool operator==(const WeakPtrKey<T> &other) const {
33 return m_ptr == other.m_ptr;
34
35 }
36
41 inline constexpr std::size_t hash() const{
42 return reinterpret_cast<std::size_t>(m_ptr);
43 }
44
45 inline std::weak_ptr<T> get_weak() const { return m_wptr;}
46 inline std::shared_ptr<T> lock() const { return m_wptr.lock();}
47};
48
49namespace std{
50template<typename T>
51struct hash<WeakPtrKey<T>>{
52 inline constexpr std::size_t operator()(const WeakPtrKey<T>& ptr) const{
53 return ptr.hash();
54 }
55};
56}
Definition WeakRef.hpp:18
constexpr std::size_t hash() const
Definition WeakRef.hpp:41