TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_id_manager.h
Go to the documentation of this file.
1#pragma once
2
3#include <gui_core/gui_hash.h>
4#include <gui_core/gui_id.h>
6
7#include <array>
8#include <cstdint>
9#include <stdint.h>
10#include <source_location>
11#include <vector>
12#include <string_view>
13
14// Uncomment to enable debug ID tracking
15//#define TACHYON_ID_DEBUG
16
17// ID system: static, dynamic and ranged identifiers for GUI elements.
18namespace tachyon
19{
22
23 struct ID;
31 {
32 // ------------------------------------------------------------------------
33 // Constructor
34 // ------------------------------------------------------------------------
35 public: IDManager()
36 {
37 m_parent_stack.reserve(4096);
38 m_static_slots.reserve(4096);
39 m_static_free_list.reserve(4096);
40 }
41
42 // ------------------------------------------------------------------------
43 // Frame Management
44 // ------------------------------------------------------------------------
45
48 public: [[nodiscard]] bool BeginFrame();
49
51 public: void EndFrame();
52
54 public: [[nodiscard]] inline bool IsInsideFrame() const { return !m_parent_stack.empty(); }
55
56 // ------------------------------------------------------------------------
57 // Parent Stack
58 // ------------------------------------------------------------------------
59
62 public: [[nodiscard]] bool PushParent(ID parent_id);
63
65 public: [[nodiscard]] bool PopParent();
66
67 // ------------------------------------------------------------------------
68 // Static IDs
69 // ------------------------------------------------------------------------
70
72 public: [[nodiscard]] ID CreateStaticID();
73
76 public: bool ReleaseStaticID(ID id);
77
81 public: void ReleaseStaticIDsNotIn(std::vector<ID>& ids, std::vector<ID>& keep);
82
83 // Tests reach it through IDManagerTestAccess (defined in the tests).
84 private: bool SetGenerationForSlot(uint32_t index, int32_t generation);
85 friend struct IDManagerTestAccess;
86
88 public: bool IsStaticIDValid(ID id) const;
89
90 // ------------------------------------------------------------------------
91 // Dynamic IDs
92 // ------------------------------------------------------------------------
93
94 private: [[nodiscard]] ID AllocateDynamicID(uint64_t hash);
95
96#ifdef TACHYON_SUPPORT_CPP20
99 public: [[nodiscard]] inline ID AllocateDynamicID(const std::source_location location = std::source_location::current())
100 {
101 return AllocateDynamicID(Hash::Combine(FileHash(location.file_name()), static_cast<uint64_t>(location.line())));
102 }
103#endif
104
107 public: [[nodiscard]] ID AllocateDynamicID(std::string_view file, int line);
108
111 public: [[nodiscard]] ID AllocateDynamicID(std::string_view hint);
112
114 public: [[nodiscard]] bool IsDynamicIDValid(ID id) const;
115
120 public: [[nodiscard]] static ID ScopedID(ID scope, ID key)
121 {
122 uint64_t hash = Hash::Combine(scope.value.integer, key.value.integer);
123 if (hash == 0) hash = 1;
124 ID id = ID::Invalid();
125 id.value.dynamicParts.mode = IDMode::Dynamic;
126 id.value.dynamicParts.hashValue = hash;
127 return id;
128 }
129
130 // ------------------------------------------------------------------------
131 // Ranged IDs
132 // ------------------------------------------------------------------------
133
134 private: [[nodiscard]] ID AllocateRangedID(uint32_t size, uint64_t hash);
135
136#ifdef TACHYON_SUPPORT_CPP20
139 public: [[nodiscard]] inline ID AllocateRangedID(uint32_t size, const std::source_location location = std::source_location::current())
140 {
141 return AllocateRangedID(size, Hash::Combine(FileHash(location.file_name()), static_cast<uint64_t>(location.line())));
142 }
143#endif
146 public: [[nodiscard]] ID AllocateRangedID(uint32_t size, std::string_view file, int line);
147
149 public: [[nodiscard]] ID AllocateRangedID(uint32_t size, std::string_view hint);
150
152 public: uint32_t GetRangeSize(ID ranged_id) const;
153
155 public: bool IsRangedIDValid(ID id) const;
156
159 public: [[nodiscard]] ID GetSubID(ID ranged_id, uint32_t index) const;
160
162 public: bool IsSubRangeIDValid(ID id) const;
163
164 // ------------------------------------------------------------------------
165 // Parent Stack Utilities
166 // ------------------------------------------------------------------------
167
169 public: bool IsInParentStack(ID id) const;
170
172 public: ID GetStackParent() const;
173
174
175 // ------------------------------------------------------------------------
176 // Validation
177 // ------------------------------------------------------------------------
178
180 public: bool IsIDValid(ID id) const;
181
184 public: inline size_t GetStaticIDCount() const { return m_static_id_count; }
185 public: inline size_t GetDynamicIDCount() const { return m_dynamic_id_total; }
186 public: inline size_t GetRangedIDCount() const { return m_ranged_count; }
188
189
190
191 private:
192 struct ParentData
193 {
194 ID id;
195 };
196
197 // Parent stack for hierarchical hashing
198 std::vector<ParentData> m_parent_stack;
202 uint64_t FileHash(const char* file)
203 {
204 FileHashSlot& slot = m_file_hashes[(reinterpret_cast<uintptr_t>(file) >> 4) & (m_file_hashes.size() - 1)];
205 if (slot.file != file)
206 slot = { file, Hash::Calculate(std::string_view(file)) };
207 return slot.hash;
208 }
209 struct FileHashSlot { const char* file = nullptr; uint64_t hash = 0; };
210 std::array<FileHashSlot, 64> m_file_hashes {};
211
212 // Dynamic IDs made so far this frame per call site (or hint) and parent: only a
213 // repeat of the same site renumbers, so a widget appearing elsewhere doesn't. A flat
214 // open-addressed table: a slot stamped with an older frame is empty, so a new frame
215 // clears nothing and allocates nothing once the table has grown.
216 struct SiteSlot { uint64_t site = 0; uint32_t count = 0; uint32_t frame = 0; };
217 std::vector<SiteSlot> m_site_slots = std::vector<SiteSlot>(1024);
218 uint32_t m_site_used = 0;
219 uint32_t m_frame = 0;
220 uint32_t NextSiteCount(uint64_t site);
221
222 // Static ID management (slot map with generations)
223 std::vector<int32_t> m_static_slots;
224 std::vector<uint32_t> m_static_free_list;
225 size_t m_static_id_count = 0;
226
227 // Dynamic ID tracking
228 size_t m_dynamic_id_total = 0;
229
230 // Ranged ID tracking
231 size_t m_ranged_count = 0;
232
233#ifdef TACHYON_ID_DEBUG
234 std::vector<ID> m_debug_dynamic_ids;
235 std::vector<ID> m_debug_ranges;
236#endif
237 };
238
240} // namespace tachyon
ID GetSubID(ID ranged_id, uint32_t index) const
The ID of item index (< range size) in a ranged ID, for indexed content such as list rows.
bool IsInsideFrame() const
True between BeginFrame and EndFrame.
Definition gui_id_manager.h:54
ID AllocateRangedID(uint32_t size, std::string_view file, int line)
A ranged ID of size sub-IDs from file and line (FILE, LINE) and the parent stack.
size_t GetDynamicIDCount() const
Live static IDs, and dynamic / ranged IDs made so far this frame.
Definition gui_id_manager.h:185
bool PopParent()
Pops the last PushParent. Never pops the root; false on underflow or outside a frame.
bool IsStaticIDValid(ID id) const
True if the slot is active and the generation matches.
ID GetStackParent() const
The innermost parent; Invalid at the root or outside a frame.
ID AllocateDynamicID(std::string_view file, int line)
A dynamic ID from file and line (FILE, LINE) and the parent stack; different per call site.
void EndFrame()
Clears the parent stack; reports misuse when a PushParent was left unpopped.
size_t GetStaticIDCount() const
Live static IDs, and dynamic / ranged IDs made so far this frame.
Definition gui_id_manager.h:184
friend struct IDManagerTestAccess
Definition gui_id_manager.h:85
IDManager()
Definition gui_id_manager.h:35
void ReleaseStaticIDsNotIn(std::vector< ID > &ids, std::vector< ID > &keep)
Releases each static ID in ids that isn't in keep: what one side of a change holds and the other does...
bool BeginFrame()
Resets the parent stack and dynamic/ranged counters.
ID AllocateRangedID(uint32_t size, std::string_view hint)
A ranged ID of size sub-IDs from hint and the parent stack; different per hint.
static ID ScopedID(ID scope, ID key)
The ID of the item key names (a node's stableID, say) inside scope (the container showing it): the sa...
Definition gui_id_manager.h:120
uint32_t GetRangeSize(ID ranged_id) const
The number of sub-IDs in a ranged ID; 0 (and a misuse report) for other modes.
bool ReleaseStaticID(ID id)
Frees the slot and bumps its generation, invalidating copies of id.
bool IsRangedIDValid(ID id) const
Debug: checks stored ranges (slow). Release: true for any non-Invalid Ranged ID.
bool IsInParentStack(ID id) const
True when id is anywhere on the parent stack. Slow: linear search.
bool IsIDValid(ID id) const
True when id is valid for its mode (see the Is*IDValid functions); slow in debug builds.
bool IsSubRangeIDValid(ID id) const
Debug: checks stored ranges (slow). Release: true for any non-Invalid SubRange ID.
bool PushParent(ID parent_id)
Makes parent_id the parent: dynamic and ranged IDs created under it inherit its hash.
bool IsDynamicIDValid(ID id) const
Debug: checks the tracking list (slow). Release: true for any non-Invalid Dynamic ID.
ID CreateStaticID()
A persistent ID tracked by slot and generation; release it with ReleaseStaticID.
ID AllocateDynamicID(std::string_view hint)
A dynamic ID from hint and the parent stack; different per hint.
size_t GetRangedIDCount() const
Live static IDs, and dynamic / ranged IDs made so far this frame.
Definition gui_id_manager.h:186
@ Dynamic
remade each frame from the call site or a hint and the parent stack
Definition gui_id.h:15
uint64_t Calculate(std::string_view str)
FNV-1a hash of str; same result as CalculateConst.
Definition gui_hash.h:29
constexpr uint64_t Combine(uint64_t a, uint64_t b)
Order-dependent combine; repeated values don't cancel out.
Definition gui_hash.h:65
Definition gui_logging.h:24
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
static constexpr ID Invalid()
The "no element" ID (all bits zero); also the default for optional ID parameters.
Definition gui_id.h:66
union tachyon::ID::Value value
uint64_t integer
Definition gui_id.h:40