inkcpp
Loading...
Searching...
No Matches
runner.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 "system.h"
11#include "functional.h"
12#include "types.h"
13
14#ifdef INK_ENABLE_UNREAL
15# include "Containers/UnrealString.h"
16#endif
17
18namespace ink::runtime
19{
20class choice;
21
37{
38public:
39 virtual ~runner_interface(){};
40
42#ifdef INK_ENABLE_STL
43 using line_type = std::string;
44#elif defined(INK_ENABLE_UNREAL)
45 using line_type = FString;
46#endif
47
48#pragma region Interface Methods
54 virtual void set_rng_seed(uint32_t seed) = 0;
55
67 virtual bool move_to(hash_t path) = 0;
68
78 bool move_to(const char* path) { return move_to(ink::hash_string(path)); }
79
90 virtual bool can_continue() const = 0;
91
97 virtual snapshot* create_snapshot() const = 0;
98
107 virtual const char* getline_alloc() = 0;
108
109#if defined(INK_ENABLE_STL) || defined(INK_ENABLE_UNREAL)
118 virtual line_type getline() = 0;
119
128 virtual line_type getall() = 0;
129#endif
130
131#ifdef INK_ENABLE_STL
138 virtual void getline(std::ostream&) = 0;
139
147 virtual void getall(std::ostream&) = 0;
148#endif
149
158 virtual const choice* begin() const = 0;
159
168 virtual const choice* end() const = 0;
169
178 virtual void choose(size_t index) = 0;
179
187 virtual bool has_tags() const = 0;
188
194 virtual size_t num_tags() const = 0;
195
208 virtual const char* get_tag(size_t index) const = 0;
209
217 virtual bool has_global_tags() const = 0;
218
225 virtual size_t num_global_tags() const = 0;
226
239 virtual const char* get_global_tag(size_t index) const = 0;
240
248 virtual bool has_knot_tags() const = 0;
249
256 virtual size_t num_knot_tags() const = 0;
257
267 virtual const char* get_knot_tag(size_t index) const = 0;
268
274 virtual hash_t get_current_knot() const = 0;
275
278
279protected:
282 virtual void internal_bind(hash_t name, internal::function_base* function) = 0;
283
284public:
301 template<typename F>
302 inline void bind(hash_t name, F function, bool lookaheadSafe = false)
303 {
304 internal_bind(name, new internal::function(function, lookaheadSafe));
305 }
306
319 template<typename F>
320 inline void bind(const char* name, F function, bool lookaheadSafe = false)
321 {
322 bind(ink::hash_string(name), function, lookaheadSafe);
323 }
324
325#ifdef INK_ENABLE_UNREAL
331 template<typename D>
332 void bind_delegate(hash_t name, D functionDelegate, bool lookaheadSafe)
333 {
334 internal_bind(name, new internal::function_array_delegate(functionDelegate, lookaheadSafe));
335 }
336#endif
337
338#pragma endregion
339
340#pragma region Convenience Methods
341
347 inline operator bool() const { return can_continue(); }
348
354 inline bool has_choices() const { return begin() != end(); }
355
361 size_t num_choices() const;
362
372 const choice* get_choice(size_t index) const;
373
381 inline const choice* operator[](size_t index) { return get_choice(index); }
382
383#pragma endregion
384};
385} // namespace ink::runtime
An ink choice that is being presented to the user.
Definition choice.h:33
A runner to execute ink script from a story.
Definition runner.h:37
virtual const choice * begin() const =0
Choice iterator.
virtual bool has_tags() const =0
Check if the current line has any tags.
virtual void set_rng_seed(uint32_t seed)=0
Sets seed for PRNG used in runner.
virtual size_t num_knot_tags() const =0
Get Number of knot/stitch tags.
void bind(hash_t name, F function, bool lookaheadSafe=false)
Binds an external callable to the runtime.
Definition runner.h:302
virtual void choose(size_t index)=0
Make a choice.
std::string line_type
String type to simplify interfaces working with strings.
Definition runner.h:43
virtual bool has_global_tags() const =0
Check if the there are global tags.
virtual snapshot * create_snapshot() const =0
creates a snapshot containing the runner, globals and all other runners connected to the globals.
virtual bool has_knot_tags() const =0
Check if there are knot/stitch tags.
virtual const char * get_global_tag(size_t index) const =0
Access a global tag by index.
void bind(const char *name, F function, bool lookaheadSafe=false)
Binds an external callable to the runtime.
Definition runner.h:320
virtual config::statistics::runner statistics() const =0
Get usage statistics for the runner.
virtual void getall(std::ostream &)=0
Gets all the text until the next choice or end.
virtual size_t num_global_tags() const =0
Get Number of global tags.
virtual size_t num_tags() const =0
Returns the number of tags on the current line.
virtual const char * get_knot_tag(size_t index) const =0
Access a knot/stitch tag by index.
virtual bool can_continue() const =0
Can the runner continue?
virtual const choice * end() const =0
Terminal choice iterator.
bool move_to(const char *path)
Moves the runner to the specified path.
Definition runner.h:78
virtual void getline(std::ostream &)=0
Gets the next line of output using C++ STL string.
virtual const char * getline_alloc()=0
Continue execution until the next newline, then allocate a c-style string with the output.
virtual line_type getall()=0
Execute the script until the next choice or the end of the script.
const choice * get_choice(size_t index) const
Gets a choice.
virtual line_type getline()=0
Execute the next line of the script.
virtual bool move_to(hash_t path)=0
Moves the runner to the specified path.
bool has_choices() const
Checks if we're currently facing any choices.
Definition runner.h:354
void bind_delegate(hash_t name, D functionDelegate, bool lookaheadSafe)
bind and unreal delegate
Definition runner.h:332
virtual const char * get_tag(size_t index) const =0
Access a tag by index.
virtual hash_t get_current_knot() const =0
Get hash of current knot/stitch name.
const choice * operator[](size_t index)
Shorcut for accessing a choice.
Definition runner.h:381
size_t num_choices() const
Returns the number of choices currently available.
Container for an InkCPP runtime snapshot, which can be migrated.
Definition snapshot.h:59
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:71
hash_t hash_string(const char *string)
Simple hash for serialization of strings.
Definition system.h:138
uint32_t hash_t
Name hash (used for temporary variables).
Definition system.h:85
Stastics for state managed for one thread inside a runtime.
Definition config.h:117