inkcpp
Toggle main menu visibility
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
13
namespace
ink::runtime
14
{
15
class
snapshot
;
16
21
class
globals_interface
22
{
23
public
:
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
70
virtual
config::statistics::global
statistics
()
const
= 0;
71
75
virtual
snapshot
*
create_snapshot
()
const
= 0;
76
77
virtual
~globals_interface
() =
default
;
78
79
protected
:
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
91
template
<>
92
inline
optional<value>
globals_interface::get<value>
(
const
char
* name)
const
93
{
94
return
get_var(
hash_string
(name));
95
}
96
97
template
<>
98
inline
bool
globals_interface::set<value>
(
const
char
* name,
const
value
& val)
99
{
100
return
set_var(
hash_string
(name), val);
101
}
102
103
template
<>
104
inline
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
113
template
<>
114
inline
bool
globals_interface::set<bool>
(
const
char
* name,
const
bool
& val)
115
{
116
return
set_var(
hash_string
(name),
value
(val));
117
}
118
119
template
<>
120
inline
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
129
template
<>
130
inline
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
135
template
<>
136
inline
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
145
template
<>
146
inline
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
151
template
<>
152
inline
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
161
template
<>
162
inline
bool
globals_interface::set<float>
(
const
char
* name,
const
float
& val)
163
{
164
return
set_var(
hash_string
(name),
value
(val));
165
}
166
167
template
<>
168
inline
optional<const char*>
globals_interface::get<const char*>
(
const
char
* name)
const
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
177
template
<>
178
inline
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
183
template
<>
184
inline
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
193
template
<>
194
inline
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
ink::runtime::globals_interface
Represents a global store to be shared amongst ink runners.
Definition
globals.h:22
ink::runtime::globals_interface::observe
void observe(const char *name, F callback)
Observers global variable.
Definition
globals.h:64
ink::runtime::globals_interface::set
bool set(const char *, const T &)
Write new value in global store.
Definition
globals.h:44
ink::runtime::globals_interface::get
optional< T > get(const char *) const
Access global variable of Ink runner.
Definition
globals.h:31
ink::runtime::globals_interface::statistics
virtual config::statistics::global statistics() const =0
Get usage statistics for global.
ink::runtime::globals_interface::create_snapshot
virtual snapshot * create_snapshot() const =0
create a snapshot of the current runtime state.
ink::runtime::snapshot
Container for an InkCPP runtime snapshot, which can be migrated.
Definition
snapshot.h:59
ink::runtime
Contaning all modules and classes used for the inkles ink runtime.
Definition
choice.h:13
ink::runtime::list
list_interface * list
alias for ink::runtime::list_interface pointer
Definition
types.h:24
ink::uint32_t
unsigned int uint32_t
define basic numeric type
Definition
system.h:71
ink::optional
std::optional< T > optional
custom optional implementation for usage if STL is disabled
Definition
system.h:335
ink::nullopt
constexpr std::nullopt_t nullopt
an empty optional
Definition
system.h:337
ink::hash_string
hash_t hash_string(const char *string)
Simple hash for serialization of strings.
Definition
system.h:138
ink::hash_t
uint32_t hash_t
Name hash (used for temporary variables).
Definition
system.h:85
ink::config::statistics::global
Stastics for state managed for one runtime.
Definition
config.h:109
ink::runtime::value
A Ink variable.
Definition
types.h:36
ink::runtime::value::Type::Float
@ Float
containing a float
Definition
types.h:54
ink::runtime::value::Type::String
@ String
contaning a const char*
Definition
types.h:53
ink::runtime::value::Type::Uint32
@ Uint32
containing uint32_t
Definition
types.h:51
ink::runtime::value::Type::List
@ List
containing a list_interface
Definition
types.h:55
ink::runtime::value::Type::Int32
@ Int32
containing int32_t
Definition
types.h:52
ink::runtime::value::Type::Bool
@ Bool
containing bool
Definition
types.h:50
inkcpp
include
globals.h
Generated by
1.17.0