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 : type{v.type}
104 {
105 switch (type) {
106 case Type::Bool: v_bool = v.v_bool; break;
107 case Type::Uint32: v_uint32 = v.v_uint32; break;
108 case Type::Int32: v_int32 = v.v_int32; break;
109 case Type::String: v_string = v.v_string; break;
110 case Type::Float: v_float = v.v_float; break;
111 case Type::List: v_list = v.v_list; break;
112 }
113 }
114
116
121 template<Type Ty>
122 const auto& get() const
123 {
124 static_assert(Ty != Ty, "No value getter for the selected type");
125 }
126};
127
129template<>
130inline const auto& value::get<value::Type::Bool>() const
131{
132 return v_bool;
133}
134
136template<>
137inline const auto& value::get<value::Type::Uint32>() const
138{
139 return v_uint32;
140}
141
143template<>
144inline const auto& value::get<value::Type::Int32>() const
145{
146 return v_int32;
147}
148
150template<>
151inline const auto& value::get<value::Type::String>() const
152{
153 return v_string;
154}
155
157template<>
158inline const auto& value::get<value::Type::Float>() const
159{
160 return v_float;
161}
162
164template<>
165inline const auto& value::get<value::Type::List>() const
166{
167 return v_list;
168}
169} // 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:122
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