inkcpp
Loading...
Searching...
No Matches
globals.h
1/* Copyright (c) 2024 Julian Benda
2 *
3 * This file is part of inkCPP which is released under MIT license.
4 * See file LICENSE.txt or go to
5 * https://github.com/JBenda/inkcpp for full license details.
6 */
7#pragma once
8
9#include "config.h"
10#include "types.h"
11#include "functional.h"
12
13namespace ink::runtime
14{
15class snapshot;
16
22{
23public:
30 template<typename T>
31 optional<T> get(const char* /*name*/) const
32 {
33 static_assert(internal::always_false<T>::value, "Requested Type is not supported");
34 }
35
43 template<typename T>
44 bool set(const char* /*name*/, const T& /*val*/)
45 {
46 static_assert(internal::always_false<T>::value, "Requested Type is not supported");
47 return false;
48 }
49
63 template<typename F>
64 void observe(const char* name, F callback)
65 {
66 internal_observe(hash_string(name), new internal::callback(callback));
67 }
68
71
75 virtual snapshot* create_snapshot() const = 0;
76
77 virtual ~globals_interface() = default;
78
79protected:
81 virtual optional<value> get_var(hash_t name) const = 0;
83 virtual bool set_var(hash_t name, const value& val) = 0;
85 virtual void internal_observe(hash_t name, internal::callback_base* callback) = 0;
86};
87
90
91template<>
92inline optional<value> globals_interface::get<value>(const char* name) const
93{
94 return get_var(hash_string(name));
95}
96
97template<>
98inline bool globals_interface::set<value>(const char* name, const value& val)
99{
100 return set_var(hash_string(name), val);
101}
102
103template<>
104inline optional<bool> globals_interface::get<bool>(const char* name) const
105{
106 auto var = get_var(hash_string(name));
107 if (var && var->type == value::Type::Bool) {
108 return {var->get<value::Type::Bool>()};
109 }
110 return nullopt;
111}
112
113template<>
114inline bool globals_interface::set<bool>(const char* name, const bool& val)
115{
116 return set_var(hash_string(name), value(val));
117}
118
119template<>
120inline optional<uint32_t> globals_interface::get<uint32_t>(const char* name) const
121{
122 auto var = get_var(hash_string(name));
123 if (var && var->type == value::Type::Uint32) {
124 return {var->get<value::Type::Uint32>()};
125 }
126 return nullopt;
127}
128
129template<>
130inline bool globals_interface::set<uint32_t>(const char* name, const uint32_t& val)
131{
132 return set_var(hash_string(name), value(val));
133}
134
135template<>
136inline optional<int32_t> globals_interface::get<int32_t>(const char* name) const
137{
138 auto var = get_var(hash_string(name));
139 if (var && var->type == value::Type::Int32) {
140 return {var->get<value::Type::Int32>()};
141 }
142 return nullopt;
143}
144
145template<>
146inline bool globals_interface::set<int32_t>(const char* name, const int32_t& val)
147{
148 return set_var(hash_string(name), value(val));
149}
150
151template<>
152inline optional<float> globals_interface::get<float>(const char* name) const
153{
154 auto var = get_var(hash_string(name));
155 if (var && var->type == value::Type::Float) {
156 return {var->get<value::Type::Float>()};
157 }
158 return nullopt;
159}
160
161template<>
162inline bool globals_interface::set<float>(const char* name, const float& val)
163{
164 return set_var(hash_string(name), value(val));
165}
166
167template<>
169{
170 auto var = get_var(hash_string(name));
171 if (var && var->type == value::Type::String) {
172 return {var->get<value::Type::String>()};
173 }
174 return nullopt;
175}
176
177template<>
178inline bool globals_interface::set<const char*>(const char* name, const char* const& val)
179{
180 return set_var(hash_string(name), value(val));
181}
182
183template<>
184inline optional<list> globals_interface::get<list>(const char* name) const
185{
186 auto var = get_var(hash_string(name));
187 if (var && var->type == value::Type::List) {
188 return {var->get<value::Type::List>()};
189 }
190 return nullopt;
191}
192
193template<>
194inline bool globals_interface::set<list>(const char* name, const list& val)
195{
196 return set_var(hash_string(name), value(val));
197}
198
200} // namespace ink::runtime
Represents a global store to be shared amongst ink runners.
Definition globals.h:22
void observe(const char *name, F callback)
Observers global variable.
Definition globals.h:64
bool set(const char *, const T &)
Write new value in global store.
Definition globals.h:44
optional< T > get(const char *) const
Access global variable of Ink runner.
Definition globals.h:31
virtual config::statistics::global statistics() const =0
Get usage statistics for global.
virtual snapshot * create_snapshot() const =0
create a snapshot of the current runtime state.
Container for an InkCPP runtime snapshot, which can be migrated.
Definition snapshot.h:59
Contaning all modules and classes used for the inkles ink runtime.
Definition choice.h:13
list_interface * list
alias for ink::runtime::list_interface pointer
Definition types.h:24
unsigned int uint32_t
define basic numeric type
Definition system.h:71
std::optional< T > optional
custom optional implementation for usage if STL is disabled
Definition system.h:335
constexpr std::nullopt_t nullopt
an empty optional
Definition system.h:337
hash_t hash_string(const char *string)
Simple hash for serialization of strings.
Definition system.h:138
uint32_t hash_t
Name hash (used for temporary variables).
Definition system.h:85
Stastics for state managed for one runtime.
Definition config.h:109
A Ink variable.
Definition types.h:36
@ Float
containing a float
Definition types.h:54
@ String
contaning a const char*
Definition types.h:53
@ Uint32
containing uint32_t
Definition types.h:51
@ List
containing a list_interface
Definition types.h:55
@ Int32
containing int32_t
Definition types.h:52
@ Bool
containing bool
Definition types.h:50