TachyonUI
1.1.0
High-performance UI for building tools.
Toggle main menu visibility
Loading...
Searching...
No Matches
gui_element_data_store.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <
gui_core/gui_id.h
>
4
5
#include <assert.h>
6
#include <stddef.h>
7
#include <new>
8
#include <typeindex>
9
#include <unordered_map>
10
#include <utility>
11
12
namespace
tachyon
13
{
16
21
class
ElementDataStore
22
{
23
public
:
24
ElementDataStore
() =
default
;
25
ElementDataStore
(
const
ElementDataStore
&) =
delete
;
26
ElementDataStore
&
operator=
(
const
ElementDataStore
&) =
delete
;
27
30
template
<
typename
T,
typename
... Args>
31
T&
GetOrCreate
(
ID
id
, Args&&... args);
32
34
template
<
typename
T>
35
T*
Get
(
ID
id
);
36
38
void
Touch
(
ID
id
);
39
40
void
Remove
(
ID
id
);
41
42
void
Clear
();
43
45
void
BeginFrame
();
46
48
void
EndFrame
();
49
50
size_t
EntryCount
()
const
{
return
m_entries.size(); }
51
52
private
:
54
static
constexpr
size_t
kInlineSize = 80;
55
56
struct
Entry
57
{
58
uint64_t touchedFrame = 0;
59
std::type_index typeIdx{
typeid
(void) };
60
61
alignas
(std::max_align_t) std::byte inlineBuffer[kInlineSize];
62
64
void
* data =
nullptr
;
65
void (*destroyer)(
void
*) =
nullptr
;
66
bool
heapAllocated =
false
;
67
68
Entry() =
default
;
69
Entry(
const
Entry&) =
delete
;
70
Entry& operator=(
const
Entry&) =
delete
;
72
Entry(Entry&&) =
delete
;
73
Entry& operator=(Entry&&) =
delete
;
74
75
~Entry() { Reset(); }
76
77
void
Reset()
78
{
79
if
(!data)
return
;
80
destroyer(data);
// also frees heap storage, with the type's own alignment
81
data =
nullptr
;
82
destroyer =
nullptr
;
83
heapAllocated =
false
;
84
typeIdx = std::type_index(
typeid
(
void
));
85
}
86
};
87
88
std::unordered_map<ID, Entry> m_entries;
89
uint64_t m_currentFrame = 1;
90
};
91
92
template
<
typename
T,
typename
... Args>
93
T&
ElementDataStore::GetOrCreate
(
ID
id
, Args&&... args)
94
{
95
// try_emplace constructs in place; Entry is non-movable.
96
auto
[it, _] = m_entries.try_emplace(
id
);
97
Entry& entry = it->second;
98
const
std::type_index wanted{
typeid
(T) };
99
100
assert((!entry.data || entry.typeIdx == wanted) &&
"ID collision between different handler types"
);
101
102
if
(entry.data && entry.typeIdx == wanted)
103
{
104
entry.touchedFrame = m_currentFrame;
105
return
*
static_cast<
T*
>
(entry.data);
106
}
107
108
// Wrong-type entry (assertion violated in debug); rebuild for release.
109
entry.Reset();
110
111
void
* mem;
112
if
constexpr
(
sizeof
(T) <= kInlineSize &&
alignof
(T) <=
alignof
(std::max_align_t))
113
{
114
mem = entry.inlineBuffer;
115
entry.heapAllocated =
false
;
116
}
117
else
118
{
119
mem = ::operator
new
(
sizeof
(T), std::align_val_t{
alignof
(T) });
120
entry.heapAllocated =
true
;
121
}
122
123
T* ptr = ::new (mem) T(std::forward<Args>(args)...);
124
entry.data = ptr;
125
if
(entry.heapAllocated)
126
entry.destroyer = +[](
void
* p)
127
{
128
static_cast<
T*
>
(p)->~T();
129
::operator
delete
(p, std::align_val_t{
alignof
(T) });
130
};
131
else
132
entry.destroyer = +[](
void
* p) {
static_cast<
T*
>
(p)->~T(); };
133
entry.typeIdx = wanted;
134
entry.touchedFrame = m_currentFrame;
135
return
*ptr;
136
}
137
138
template
<
typename
T>
139
T*
ElementDataStore::Get
(
ID
id
)
140
{
141
auto
it = m_entries.find(
id
);
142
if
(it == m_entries.end())
return
nullptr
;
143
const
std::type_index wanted{
typeid
(T) };
144
assert(it->second.typeIdx == wanted &&
"ID collision between different handler types"
);
145
if
(it->second.typeIdx != wanted)
return
nullptr
;
146
it->second.touchedFrame = m_currentFrame;
147
return
static_cast<
T*
>
(it->second.data);
148
}
149
150
}
tachyon::ElementDataStore::ElementDataStore
ElementDataStore()=default
tachyon::ElementDataStore::ElementDataStore
ElementDataStore(const ElementDataStore &)=delete
tachyon::ElementDataStore::Clear
void Clear()
tachyon::ElementDataStore::Remove
void Remove(ID id)
tachyon::ElementDataStore::operator=
ElementDataStore & operator=(const ElementDataStore &)=delete
tachyon::ElementDataStore::EntryCount
size_t EntryCount() const
Definition
gui_element_data_store.h:50
tachyon::ElementDataStore::BeginFrame
void BeginFrame()
Starts a frame; call before any Get, GetOrCreate or Touch of the frame.
tachyon::ElementDataStore::EndFrame
void EndFrame()
Destroys the entries not touched since BeginFrame.
tachyon::ElementDataStore::Touch
void Touch(ID id)
Keeps id's entry alive through this frame's EndFrame.
tachyon::Entry
tachyon::ElementDataStore::GetOrCreate
T & GetOrCreate(ID id, Args &&... args)
The T stored for id, constructed from args when absent.
Definition
gui_element_data_store.h:93
tachyon::ElementDataStore::Get
T * Get(ID id)
The T stored for id, or nullptr (also on a type mismatch). Touches the entry if found.
Definition
gui_element_data_store.h:139
gui_id.h
tachyon
Definition
gui_logging.h:24
tachyon::ID
A 64-bit element ID whose bit layout depends on its IDMode.
Definition
gui_id.h:23
gui_core
elements
gui_element_data_store.h
Generated by
1.18.0