Photon Editor Library 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
THandleDispatcher.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <Utility/Concurrent/TAtomicQuasiQueue.h>
6#include <Common/assertion.h>
7
8#include <queue>
9#include <utility>
10
11namespace ph::editor
12{
13
16template<CWeakHandle Handle>
18{
19 using Index = typename Handle::IndexType;
20 using Generation = typename Handle::GenerationType;
21
22public:
23 using HandleType = Handle;
24
26 : m_handles()
27 , m_nextNewIdx(0)
28 {}
29
31 : m_handles(other.m_handles)
32 , m_nextNewIdx(other.m_nextNewIdx)
33 {}
34
35 inline THandleDispatcher(THandleDispatcher&& other) noexcept
37 {
38 swap(*this, other);
39 }
40
42 {
43 swap(*this, rhs);
44
45 return *this;
46 }
47
50 [[nodiscard]]
51 inline Handle dispatchOne()
52 {
53 if(!m_handles.empty())
54 {
55 Handle handle = m_handles.front();
56 m_handles.pop();
57 return handle;
58 }
59
60 // Create new handle if we cannot obtain an existing one
61 constexpr auto initialGeneration = Handle::nextGeneration(Handle::INVALID_GENERATION);
62 const Index newIdx = m_nextNewIdx++;
63 return Handle(newIdx, initialGeneration);
64 }
65
68 inline void returnOne(const Handle& handle)
69 {
70 PH_ASSERT(handle);
71 m_handles.push(handle);
72 }
73
74 inline friend void swap(THandleDispatcher& first, THandleDispatcher& second) noexcept
75 {
76 // Enable ADL
77 using std::swap;
78
79 swap(first.m_handles, second.m_handles);
80 swap(first.m_nextNewIdx, second.m_nextNewIdx);
81 }
82
83private:
84 std::queue<Handle> m_handles;
85 Index m_nextNewIdx;
86};
87
88}// end namespace ph::editor
Sequential handle dispatcher meant for single-threaded use.
Definition THandleDispatcher.h:18
Handle HandleType
Definition THandleDispatcher.h:23
Handle dispatchOne()
Get one handle.
Definition THandleDispatcher.h:51
THandleDispatcher(THandleDispatcher &&other) noexcept
Definition THandleDispatcher.h:35
THandleDispatcher & operator=(THandleDispatcher rhs)
Definition THandleDispatcher.h:41
THandleDispatcher(const THandleDispatcher &other)
Definition THandleDispatcher.h:30
THandleDispatcher()
Definition THandleDispatcher.h:25
friend void swap(THandleDispatcher &first, THandleDispatcher &second) noexcept
Definition THandleDispatcher.h:74
void returnOne(const Handle &handle)
Recycle one handle.
Definition THandleDispatcher.h:68
Definition ph_editor.h:10