inkcpp
Loading...
Searching...
No Matches
types.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 "list.h"
10#include "story_ptr.h"
11#include "system.h"
12
13namespace ink::runtime
14{
17class snapshot;
18
25
43
54struct value {
55private:
56 union {
57 bool v_bool;
58 uint32_t v_uint32;
59 int32_t v_int32;
60 const char* v_string;
61 float v_float;
62 list v_list;
63 };
64
65public:
75
76 value()
77 : v_int32{0}
78 , type{Type::Int32}
79 {
80 }
81
83
84 value(bool v)
85 : v_bool{v}
86 , type{Type::Bool}
87 {
88 }
89
91 : v_uint32{v}
92 , type{Type::Uint32}
93 {
94 }
95
96 value(int32_t v)
97 : v_int32{v}
98 , type{Type::Int32}
99 {
100 }
101
102 value(const char* v)
103 : v_string{v}
104 , type{Type::String}
105 {
106 }
107
108 value(float v)
109 : v_float{v}
110 , type{Type::Float}
111 {
112 }
113
115 : v_list{list}
116 , type{Type::List}
117 {
118 }
119
120 value(const value& v)
121 : v_string{nullptr}
122 , type{v.type}
123 {
124 switch (type) {
125 case Type::Bool: v_bool = v.v_bool; break;
126 case Type::Uint32: v_uint32 = v.v_uint32; break;
127 case Type::Int32: v_int32 = v.v_int32; break;
128 case Type::String: v_string = v.v_string; break;
129 case Type::Float: v_float = v.v_float; break;
130 case Type::List: v_list = v.v_list; break;
131 }
132 }
133
134 constexpr value& operator=(const value& v)
135 {
136 type = v.type;
137 switch (type) {
138 case Type::Bool: v_bool = v.v_bool; break;
139 case Type::Uint32: v_uint32 = v.v_uint32; break;
140 case Type::Int32: v_int32 = v.v_int32; break;
141 case Type::String: v_string = v.v_string; break;
142 case Type::Float: v_float = v.v_float; break;
143 case Type::List: v_list = v.v_list; break;
144 }
145 return *this;
146 }
147
149
150#ifdef __GNUC__
151# pragma GCC diagnostic push
152# pragma GCC diagnostic ignored "-Wtautological-compare"
153#endif
158 template<Type Ty>
159 const auto& get() const
160 {
161 static_assert(Ty != Ty, "No value getter for the selected type");
162 }
163#ifdef __GNUC__
164# pragma GCC diagnostic pop
165#endif
166};
167
169template<>
170inline const auto& value::get<value::Type::Bool>() const
171{
172 inkAssert(type == value::Type::Bool, "Expected type bool, if a boolean should be readed");
173 return v_bool;
174}
175
177template<>
178inline const auto& value::get<value::Type::Uint32>() const
179{
180 inkAssert(type == value::Type::Uint32, "Expected type uint32, if a boolean should be readed");
181 return v_uint32;
182}
183
185template<>
186inline const auto& value::get<value::Type::Int32>() const
187{
188 inkAssert(type == value::Type::Int32, "Expected type int32, if a boolean should be readed");
189 return v_int32;
190}
191
193template<>
194inline const auto& value::get<value::Type::String>() const
195{
196 inkAssert(type == value::Type::String, "Expected type string, if a boolean should be readed");
197 return v_string;
198}
199
201template<>
202inline const auto& value::get<value::Type::Float>() const
203{
204 inkAssert(type == value::Type::Float, "Expected type float, if a boolean should be readed");
205 return v_float;
206}
207
209template<>
210inline const auto& value::get<value::Type::List>() const
211{
212 inkAssert(type == value::Type::List, "Expected type list, if a boolean should be readed");
213 return v_list;
214}
215} // namespace ink::runtime
Represents a global store to be shared amongst ink runners.
Definition globals.h:22
Interface for accessing a Ink list.
Definition list.h:43
A runner to execute ink script from a story.
Definition runner.h:37
Container for an InkCPP runtime snapshot, which can be migrated.
Definition snapshot.h:59
Pointer wrapper to an object whose lifetime is tied to a story object.
Definition story_ptr.h:85
#define inkAssert(condition, text,...)
Compile argument agnostic assert macro.
Definition system.h:58
Contaning all modules and classes used for the inkles ink runtime.
Definition choice.h:13
story_ptr< globals_interface > globals
alias for an managed ink::runtime::globals_interface pointer
Definition types.h:20
list_interface * list
alias for ink::runtime::list_interface pointer
Definition types.h:24
story_ptr< runner_interface > runner
alias for an managed ink::runtime::runner_interface pointer
Definition types.h:22
whitespace_mode
How runs of whitespace inside a line are treated.
Definition types.h:31
@ collapse
Collapse a run of spaces and tabs inside a line to a single space.
Definition types.h:35
@ keep_runs
Keep runs of spaces and tabs inside a line, for text laid out for a fixed width display.
Definition types.h:41
unsigned int uint32_t
define basic numeric type
Definition system.h:71
A Ink variable.
Definition types.h:54
value(const value &v)
Construct value from corresponding type.
Definition types.h:120
value(bool v)
Construct value from corresponding type.
Definition types.h:84
value(uint32_t v)
Construct value from corresponding type.
Definition types.h:90
Type
Type labels for ink::runtime::value.
Definition types.h:67
@ Float
containing a float
Definition types.h:72
@ String
contaning a const char*
Definition types.h:71
@ Uint32
containing uint32_t
Definition types.h:69
@ List
containing a list_interface
Definition types.h:73
@ Int32
containing int32_t
Definition types.h:70
@ Bool
containing bool
Definition types.h:68
value(list_interface *list)
Construct value from corresponding type.
Definition types.h:114
enum ink::runtime::value::Type type
Label of type currently contained in value.
value(const char *v)
Construct value from corresponding type.
Definition types.h:102
const auto & get() const
Get value to corresponding type.
Definition types.h:159
value(int32_t v)
Construct value from corresponding type.
Definition types.h:96
constexpr value & operator=(const value &v)
Construct value from corresponding type.
Definition types.h:134
value(float v)
Construct value from corresponding type.
Definition types.h:108