Photon Editor Library 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TWeakHandle.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <Common/Utility/string_utils.h>
6#include <Utility/traits.h>
7#include <Utility/utility.h>
8#include <Math/hash.h>
9
10#include <cstddef>
11#include <limits>
12#include <string>
13#include <functional>
14
15namespace ph::editor
16{
17
21template<typename Item, typename Index = std::size_t, typename Generation = Index>
22class TWeakHandle final
23{
24public:
25 using WeakHandleTag = void;
26 using ItemType = Item;
27 using IndexType = Index;
28 using GenerationType = Generation;
29
30 inline static constexpr auto INVALID_INDEX = std::numeric_limits<Index>::max();
31 inline static constexpr auto INVALID_GENERATION = 0;
32
34
35 inline TWeakHandle(Index itemIdx, Generation itemGeneration)
36 : m_itemIdx(itemIdx)
37 , m_itemGeneration(itemGeneration)
38 {}
39
44 template<CDerived<Item> DerivedItem>
46 : TWeakHandle(otherHandle.getIndex(), otherHandle.getGeneration())
47 {}
48
49 inline Index getIndex() const
50 {
51 return m_itemIdx;
52 }
53
54 inline Generation getGeneration() const
55 {
56 return m_itemGeneration;
57 }
58
62 inline bool isEmpty() const
63 {
64 return m_itemIdx == INVALID_INDEX || m_itemGeneration == INVALID_GENERATION;
65 }
66
67 inline std::string toString() const
68 {
69 std::string indexStr = m_itemIdx != INVALID_INDEX
70 ? std::to_string(m_itemIdx) : "invalid";
71 std::string generationStr = m_itemGeneration != INVALID_GENERATION
72 ? std::to_string(m_itemGeneration) : "invalid";
73 return "[index=" + indexStr + ", generation=" + generationStr + "]";
74 }
75
79 inline operator bool () const
80 {
81 return !isEmpty();
82 }
83
84 inline bool operator == (const TWeakHandle& rhs) const = default;
85
92 inline static constexpr Generation nextGeneration(const Generation currentGeneration)
93 {
94 Index nextGen = currentGeneration + 1;
95 if(nextGen == INVALID_GENERATION)
96 {
97 ++nextGen;
98 }
99 return nextGen;
100 }
101
102private:
103 Index m_itemIdx = INVALID_INDEX;
104 Generation m_itemGeneration = INVALID_GENERATION;
105};
106
107}// end namespace ph::editor
108
109template<typename Item, typename Index, typename Generation>
111
112namespace std
113{
114
115template<ph::editor::CWeakHandle Handle>
116struct hash<Handle>
117{
118 std::size_t operator () (const Handle& handle) const
119 {
120 return ph::math::murmur3_32(handle, 0);
121 }
122};
123
124}// end namespace std
PH_DEFINE_INLINE_TO_STRING_FORMATTER_TEMPLATE(ph::editor::TWeakHandle< Item, Index, Generation >)
Handle with weak reference semantics. Default constructor creates empty handle.
Definition TWeakHandle.h:23
bool operator==(const TWeakHandle &rhs) const =default
TWeakHandle(const TWeakHandle< DerivedItem, Index, Generation > &otherHandle)
Definition TWeakHandle.h:45
static constexpr auto INVALID_INDEX
Definition TWeakHandle.h:30
Generation GenerationType
Definition TWeakHandle.h:28
std::string toString() const
Definition TWeakHandle.h:67
Index IndexType
Definition TWeakHandle.h:27
Item ItemType
Definition TWeakHandle.h:26
Index getIndex() const
Definition TWeakHandle.h:49
PH_DEFINE_INLINE_RULE_OF_5_MEMBERS(TWeakHandle)
Generation getGeneration() const
Definition TWeakHandle.h:54
static constexpr Generation nextGeneration(const Generation currentGeneration)
Get the next generation given a current generation.
Definition TWeakHandle.h:92
static constexpr auto INVALID_GENERATION
Definition TWeakHandle.h:31
void WeakHandleTag
Definition TWeakHandle.h:25
bool isEmpty() const
Definition TWeakHandle.h:62
TWeakHandle(Index itemIdx, Generation itemGeneration)
Definition TWeakHandle.h:35
Definition ph_editor.h:10
Definition TWeakHandle.h:113