39 std::map<std::string, std::any> _data;
40 std::function<void()> _onStateChange;
43 State(
void) : _data(), _onStateChange(
nullptr) {}
50 void setState(
const std::map<std::string, std::any> &newState) {
51 for (
const auto &[key, value] : newState) {
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);
80 template <
typename T>
void set(
const std::string &key,
const T &value) {
92 _onStateChange = callback;
99 const std::map<std::string, std::any> &
getData(
void)
const {
return _data; }
Manages dynamic component state with type-safe storage.
void setState(const std::map< std::string, std::any > &newState)
Replace the entire state with new values.
void setOnStateChange(std::function< void()> callback)
Register a callback to be called when state changes.
const std::map< std::string, std::any > & getData(void) const
Get the entire state data map.
void set(const std::string &key, const T &value)
Set a state value by key.
T get(const std::string &key) const
Get a state value by key with type casting.