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
36struct value {
37private:
38 union {
39 bool v_bool;
40 uint32_t v_uint32;
41 int32_t v_int32;
42 const char* v_string;
43 float v_float;
44 list v_list;
45 };
46
47public:
57
58 value()
59 : v_int32{0}
60 , type{Type::Int32}
61 {
62 }
63
65
66 value(bool v)
67 : v_bool{v}
68 , type{Type::Bool}
69 {
70 }
71
73 : v_uint32{v}
74 , type{Type::Uint32}
75 {
76 }
77
78 value(int32_t v)
79 : v_int32{v}
80 , type{Type::Int32}
81 {
82 }
83
84 value(const char* v)
85 : v_string{v}
86 , type{Type::String}
87 {
88 }
89
90 value(float v)
91 : v_float{v}
92 , type{Type::Float}
93 {
94 }
95
97 : v_list{list}
98 , type{Type::List}
99 {
100 }
101
102 value(const value& v)
103 : v_string{nullptr}
104 , type{v.type}
105 {
106 switch (type) {
107 case Type::Bool: v_bool = v.v_bool; break;
108 case Type::Uint32: v_uint32 = v.v_uint32; break;
109 case Type::Int32: v_int32 = v.v_int32; break;
110 case Type::String: v_string = v.v_string; break;
111 case Type::Float: v_float = v.v_float; break;
112 case Type::List: v_list = v.v_list; break;
113 }
114 }
115
116 constexpr value& operator=(const value& v)
117 {
118 type = v.type;
119 switch (type) {
120 case Type::Bool: v_bool = v.v_bool; break;
121 case Type::Uint32: v_uint32 = v.v_uint32; break;
122 case Type::Int32: v_int32 = v.v_int32; break;
123 case Type::String: v_string = v.v_string; break;
124 case Type::Float: v_float = v.v_float; break;
125 case Type::List: v_list = v.v_list; break;
126 }
127 return *this;
128 }
129
131
136 template<Type Ty>
137 const auto& get() const
138 {
139 static_assert(Ty != Ty, "No value getter for the selected type");
140 }
141};
142
144template<>
145inline const auto& value::get<value::Type::Bool>() const
146{
147 inkAssert(type == value::Type::Bool, "Expected type bool, if a boolean should be readed");
148 return v_bool;
149}
150
152template<>
153inline const auto& value::get<value::Type::Uint32>() const
154{
155 inkAssert(type == value::Type::Uint32, "Expected type uint32, if a boolean should be readed");
156 return v_uint32;
157}
158
160template<>
161inline const auto& value::get<value::Type::Int32>() const
162{
163 inkAssert(type == value::Type::Int32, "Expected type int32, if a boolean should be readed");
164 return v_int32;
165}
166
168template<>
169inline const auto& value::get<value::Type::String>() const
170{
171 inkAssert(type == value::Type::String, "Expected type string, if a boolean should be readed");
172 return v_string;
173}
174
176template<>
177inline const auto& value::get<value::Type::Float>() const
178{
179 inkAssert(type == value::Type::Float, "Expected type float, if a boolean should be readed");
180 return v_float;
181}
182
184template<>
185inline const auto& value::get<value::Type::List>() const
186{
187 inkAssert(type == value::Type::List, "Expected type list, if a boolean should be readed");
188 return v_list;
189}
190} // 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
unsigned int uint32_t
define basic numeric type
Definition system.h:71
A Ink variable.
Definition types.h:36
value(const value &v)
Construct value from corresponding type.
Definition types.h:102
value(bool v)
Construct value from corresponding type.
Definition types.h:66
value(uint32_t v)
Construct value from corresponding type.
Definition types.h:72
Type
Type labels for ink::runtime::value.
Definition types.h:49
@ 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
value(list_interface *list)
Construct value from corresponding type.
Definition types.h:96
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:84
const auto & get() const
Get value to corresponding type.
Definition types.h:137
value(int32_t v)
Construct value from corresponding type.
Definition types.h:78
constexpr value & operator=(const value &v)
Construct value from corresponding type.
Definition types.h:116
value(float v)
Construct value from corresponding type.
Definition types.h:90