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 "types.h"
10#include "functional.h"
11
12namespace ink::runtime
13{
14class snapshot;
15
21{
22public:
29 template<typename T>
30 optional<T> get(const char* name) const
31 {
32 static_assert(internal::always_false<T>::value, "Requested Type is not supported");
33 }
34
42 template<typename T>
43 bool set(const char* name, const T& val)
44 {
45 static_assert(internal::always_false<T>::value, "Requested Type is not supported");
46 return false;
47 }
48
62 template<typename F>
63 void observe(const char* name, F callback)
64 {
65 internal_observe(hash_string(name), new internal::callback(callback));
66 }
67
71 virtual snapshot* create_snapshot() const = 0;
72
73 virtual ~globals_interface() = default;
74
75protected:
77 virtual optional<value> get_var(hash_t name) const = 0;
79 virtual bool set_var(hash_t name, const value& val) = 0;
81 virtual void internal_observe(hash_t name, internal::callback_base* callback) = 0;
82};
83
86
87template<>
88inline optional<value> globals_interface::get<value>(const char* name) const
89{
90 return get_var(hash_string(name));
91}
92
93template<>
94inline bool globals_interface::set<value>(const char* name, const value& val)
95{
96 return set_var(hash_string(name), val);
97}
98
99template<>
100inline optional<bool> globals_interface::get<bool>(const char* name) const
101{
102 auto var = get_var(hash_string(name));
103 if (var && var->type == value::Type::Bool) {
104 return {var->get<value::Type::Bool>()};
105 }
106 return nullopt;
107}
108
109template<>
110inline bool globals_interface::set<bool>(const char* name, const bool& val)
111{
112 return set_var(hash_string(name), value(val));
113}
114
115template<>
116inline optional<uint32_t> globals_interface::get<uint32_t>(const char* name) const
117{
118 auto var = get_var(hash_string(name));
119 if (var && var->type == value::Type::Uint32) {
120 return {var->get<value::Type::Uint32>()};
121 }
122 return nullopt;
123}
124
125template<>
126inline bool globals_interface::set<uint32_t>(const char* name, const uint32_t& val)
127{
128 return set_var(hash_string(name), value(val));
129}
130
131template<>
132inline optional<int32_t> globals_interface::get<int32_t>(const char* name) const
133{
134 auto var = get_var(hash_string(name));
135 if (var && var->type == value::Type::Int32) {
136 return {var->get<value::Type::Int32>()};
137 }
138 return nullopt;
139}
140
141template<>
142inline bool globals_interface::set<int32_t>(const char* name, const int32_t& val)
143{
144 return set_var(hash_string(name), value(val));
145}
146
147template<>
148inline optional<float> globals_interface::get<float>(const char* name) const
149{
150 auto var = get_var(hash_string(name));
151 if (var && var->type == value::Type::Float) {
152 return {var->get<value::Type::Float>()};
153 }
154 return nullopt;
155}
156
157template<>
158inline bool globals_interface::set<float>(const char* name, const float& val)
159{
160 return set_var(hash_string(name), value(val));
161}
162
163template<>
164inline optional<const char*> globals_interface::get<const char*>(const char* name) const
165{
166 auto var = get_var(hash_string(name));
167 if (var && var->type == value::Type::String) {
168 return {var->get<value::Type::String>()};
169 }
170 return nullopt;
171}
172
173template<>
174inline bool globals_interface::set<const char*>(const char* name, const char* const& val)
175{
176 return set_var(hash_string(name), value(val));
177}
178
179template<>
180inline optional<list> globals_interface::get<list>(const char* name) const
181{
182 auto var = get_var(hash_string(name));
183 if (var && var->type == value::Type::List) {
184 return {var->get<value::Type::List>()};
185 }
186 return nullopt;
187}
188
189template<>
190inline bool globals_interface::set<list>(const char* name, const list& val)
191{
192 return set_var(hash_string(name), value(val));
193}
194
196} // namespace ink::runtime
Represents a global store to be shared amongst ink runners.
Definition globals.h:21
void observe(const char *name, F callback)
Observers global variable.
Definition globals.h:63
optional< T > get(const char *name) const
Access global variable of Ink runner.
Definition globals.h:30
bool set(const char *name, const T &val)
Write new value in global store.
Definition globals.h:43
virtual snapshot * create_snapshot() const =0
create a snapshot of the current runtime state.
Interface for accessing a Ink list.
Definition list.h:43
Container for an InkCPP runtime snapshot.
Definition snapshot.h:27
Contaning all modules and classes used for the inkles ink runtime.
Definition choice.h:13
unsigned int uint32_t
define basic numeric type
Definition system.h:47
std::optional< T > optional
custom optional implementation for usage if STL is disabled
Definition system.h:229
constexpr std::nullopt_t nullopt
an empty optional
Definition system.h:231
hash_t hash_string(const char *string)
Simple hash for serialization of strings.
Definition system.h:57
uint32_t hash_t
Name hash (used for temporary variables)
Definition system.h:50
A Ink variable.
Definition types.h:36
@ Float
containing a float
@ String
contaning a const char*
@ Uint32
containing uint32_t
@ List
containing a list_interface
@ Int32
containing int32_t
@ Bool
containing bool