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{
15class globals_interface;
16class runner_interface;
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:
49 enum class Type {
50 Bool,
51 Uint32,
52 Int32,
53 String,
54 Float,
55 List
56 } type;
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
117
122 template<Type Ty>
123 const auto& get() const
124 {
125 static_assert(Ty != Ty, "No value getter for the selected type");
126 }
127};
128
130template<>
131inline const auto& value::get<value::Type::Bool>() const
132{
133 return v_bool;
134}
135
137template<>
138inline const auto& value::get<value::Type::Uint32>() const
139{
140 return v_uint32;
141}
142
144template<>
145inline const auto& value::get<value::Type::Int32>() const
146{
147 return v_int32;
148}
149
151template<>
152inline const auto& value::get<value::Type::String>() const
153{
154 return v_string;
155}
156
158template<>
159inline const auto& value::get<value::Type::Float>() const
160{
161 return v_float;
162}
163
165template<>
166inline const auto& value::get<value::Type::List>() const
167{
168 return v_list;
169}
170} // namespace ink::runtime
Interface for accessing a Ink list.
Definition list.h:43
Pointer wrapper to an object whose lifetime is tied to a story object.
Definition story_ptr.h:85
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
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
@ String
contaning a const char*
@ Uint32
containing uint32_t
@ List
containing a list_interface
@ Int32
containing int32_t
@ Bool
containing bool
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:123
value(int32_t v)
Construct value from corresponding type.
Definition types.h:78
value(float v)
Construct value from corresponding type.
Definition types.h:90