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
41 // String type to simplify interfaces working with strings
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
65 virtual bool move_to(hash_t path) = 0;
66
77 virtual bool can_continue() const = 0;
78
79#ifdef INK_ENABLE_CSTD
88 virtual const char* getline_alloc() = 0;
89#endif
90
96 virtual snapshot* create_snapshot() const = 0;
97
106 virtual line_type getline() = 0;
107
116 virtual line_type getall() = 0;
117
118#ifdef INK_ENABLE_STL
125 virtual void getline(std::ostream&) = 0;
126
134 virtual void getall(std::ostream&) = 0;
135#endif
136
145 virtual const choice* begin() const = 0;
146
155 virtual const choice* end() const = 0;
156
165 virtual void choose(size_t index) = 0;
166
174 virtual bool has_tags() const = 0;
175
181 virtual size_t num_tags() const = 0;
182
195 virtual const char* get_tag(size_t index) const = 0;
196
204 virtual bool has_global_tags() const = 0;
205
212 virtual size_t num_global_tags() const = 0;
213
226 virtual const char* get_global_tag(size_t index) const = 0;
227
235 virtual bool has_knot_tags() const = 0;
236
243 virtual size_t num_knot_tags() const = 0;
244
254 virtual const char* get_knot_tag(size_t index) const = 0;
255
261 virtual hash_t get_current_knot() const = 0;
262
263
264protected:
267 virtual void internal_bind(hash_t name, internal::function_base* function) = 0;
268
269public:
286 template<typename F>
287 inline void bind(hash_t name, F function, bool lookaheadSafe = false)
288 {
289 internal_bind(name, new internal::function(function, lookaheadSafe));
290 }
291
304 template<typename F>
305 inline void bind(const char* name, F function, bool lookaheadSafe = false)
306 {
307 bind(ink::hash_string(name), function, lookaheadSafe);
308 }
309
310#ifdef INK_ENABLE_UNREAL
316 template<typename D>
317 void bind_delegate(hash_t name, D functionDelegate, bool lookaheadSafe)
318 {
319 internal_bind(name, new internal::function_array_delegate(functionDelegate, lookaheadSafe));
320 }
321#endif
322
323#pragma endregion
324
325#pragma region Convenience Methods
326
332 inline operator bool() const { return can_continue(); }
333
339 inline bool has_choices() const { return begin() != end(); }
340
346 size_t num_choices() const;
347
357 const choice* get_choice(size_t index) const;
358
366 inline const choice* operator[](size_t index) { return get_choice(index); }
367
368#pragma endregion
369};
370} // 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:287
virtual void choose(size_t index)=0
Make a choice.
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:305
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.
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:339
void bind_delegate(hash_t name, D functionDelegate, bool lookaheadSafe)
bind and unreal delegate
Definition runner.h:317
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:366
size_t num_choices() const
Returns the number of choices currently available.
Container for an InkCPP runtime snapshot.
Definition snapshot.h:27
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
hash_t hash_string(const char *string)
Simple hash for serialization of strings.
Definition system.h:57
uint32_t hash_t
Name hash (used for temporary variables)
Definition system.h:50