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
68 virtual void set_whitespace_mode(whitespace_mode mode) = 0;
69
81 virtual bool move_to(hash_t path) = 0;
82
92 bool move_to(const char* path) { return move_to(ink::hash_string(path)); }
93
104 virtual bool can_continue() const = 0;
105
111 virtual snapshot* create_snapshot() const = 0;
112
121 virtual const char* getline_alloc() = 0;
122
123#if defined(INK_ENABLE_STL) || defined(INK_ENABLE_UNREAL)
132 virtual line_type getline() = 0;
133
142 virtual line_type getall() = 0;
143#endif
144
145#ifdef INK_ENABLE_STL
152 virtual void getline(std::ostream&) = 0;
153
161 virtual void getall(std::ostream&) = 0;
162#endif
163
172 virtual const choice* begin() const = 0;
173
182 virtual const choice* end() const = 0;
183
192 virtual void choose(size_t index) = 0;
193
201 virtual bool has_tags() const = 0;
202
208 virtual size_t num_tags() const = 0;
209
222 virtual const char* get_tag(size_t index) const = 0;
223
231 virtual bool has_global_tags() const = 0;
232
239 virtual size_t num_global_tags() const = 0;
240
253 virtual const char* get_global_tag(size_t index) const = 0;
254
262 virtual bool has_knot_tags() const = 0;
263
270 virtual size_t num_knot_tags() const = 0;
271
281 virtual const char* get_knot_tag(size_t index) const = 0;
282
288 virtual hash_t get_current_knot() const = 0;
289
292
293protected:
296 virtual void internal_bind(hash_t name, internal::function_base* function) = 0;
297
298public:
315 template<typename F>
316 inline void bind(hash_t name, F function, bool lookaheadSafe = false)
317 {
318 internal_bind(name, new internal::function(function, lookaheadSafe));
319 }
320
333 template<typename F>
334 inline void bind(const char* name, F function, bool lookaheadSafe = false)
335 {
336 bind(ink::hash_string(name), function, lookaheadSafe);
337 }
338
339#ifdef INK_ENABLE_UNREAL
345 template<typename D>
346 void bind_delegate(hash_t name, D functionDelegate, bool lookaheadSafe)
347 {
348 internal_bind(name, new internal::function_array_delegate(functionDelegate, lookaheadSafe));
349 }
350#endif
351
352#pragma endregion
353
354#pragma region Convenience Methods
355
361 inline operator bool() const { return can_continue(); }
362
368 inline bool has_choices() const { return begin() != end(); }
369
375 size_t num_choices() const;
376
388 const choice* get_choice(size_t index) const;
389
399 inline const choice* operator[](size_t index) { return get_choice(index); }
400
401#pragma endregion
402};
403} // namespace ink::runtime
An ink choice that is being presented to the user.
Definition choice.h:32
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:316
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:334
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:92
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:368
void bind_delegate(hash_t name, D functionDelegate, bool lookaheadSafe)
bind and unreal delegate
Definition runner.h:346
virtual const char * get_tag(size_t index) const =0
Access a tag by index.
virtual void set_whitespace_mode(whitespace_mode mode)=0
Sets how runs of whitespace inside a line are treated.
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:399
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
whitespace_mode
How runs of whitespace inside a line are treated.
Definition types.h:31
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