guillaume 1.0.0
Guillaume is a component-based UI framework library built around a primitive rendering system. It's designed for building user interfaces across XR platforms and traditional platforms with custom rendering backends and input handling. The framework is lightweight, flexible, and easy to integrate into existing projects.
Loading...
Searching...
No Matches
state.hpp
1/*
2 Copyright (c) 2025 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#pragma once
24
25#include <any>
26#include <functional>
27#include <map>
28#include <string>
29
37class State {
38private:
39 std::map<std::string, std::any> _data;
40 std::function<void()> _onStateChange;
41
42public:
43 State(void) : _data(), _onStateChange(nullptr) {}
44 ~State(void) = default;
45
50 void setState(const std::map<std::string, std::any> &newState) {
51 for (const auto &[key, value] : newState) {
52 _data[key] = value;
53 }
54 if (_onStateChange) {
55 _onStateChange();
56 }
57 }
58
66 template <typename T> T get(const std::string &key) const {
67 auto it = _data.find(key);
68 if (it != _data.end()) {
69 return std::any_cast<T>(it->second);
70 }
71 return T();
72 }
73
80 template <typename T> void set(const std::string &key, const T &value) {
81 _data[key] = value;
82 if (_onStateChange) {
83 _onStateChange();
84 }
85 }
86
91 void setOnStateChange(std::function<void()> callback) {
92 _onStateChange = callback;
93 }
94
99 const std::map<std::string, std::any> &getData(void) const { return _data; }
100};
Manages dynamic component state with type-safe storage.
Definition state.hpp:37
void setState(const std::map< std::string, std::any > &newState)
Replace the entire state with new values.
Definition state.hpp:50
void setOnStateChange(std::function< void()> callback)
Register a callback to be called when state changes.
Definition state.hpp:91
const std::map< std::string, std::any > & getData(void) const
Get the entire state data map.
Definition state.hpp:99
void set(const std::string &key, const T &value)
Set a state value by key.
Definition state.hpp:80
T get(const std::string &key) const
Get a state value by key with type casting.
Definition state.hpp:66