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#pragma region Interface Methods
47 virtual void set_rng_seed(uint32_t seed) = 0;
48
58 virtual bool move_to(hash_t path) = 0;
59
70 virtual bool can_continue() const = 0;
71
72#ifdef INK_ENABLE_CSTD
81 virtual const char* getline_alloc() = 0;
82#endif
83
89 virtual snapshot* create_snapshot() const = 0;
90
91#ifdef INK_ENABLE_STL
100 virtual std::string getline() = 0;
101
108 virtual void getline(std::ostream&) = 0;
109
119 virtual std::string getall() = 0;
120
128 virtual void getall(std::ostream&) = 0;
129#endif
130
131#ifdef INK_ENABLE_UNREAL
140 virtual FString getline() = 0;
141#endif
142
151 virtual const choice* begin() const = 0;
152
161 virtual const choice* end() const = 0;
162
171 virtual void choose(size_t index) = 0;
172
174 virtual bool has_tags() const = 0;
179 virtual size_t num_tags() const = 0;
183 virtual const char* get_tag(size_t index) const = 0;
184
185protected:
188 virtual void internal_bind(hash_t name, internal::function_base* function) = 0;
189
190public:
207 template<typename F>
208 inline void bind(hash_t name, F function, bool lookaheadSafe = false)
209 {
210 internal_bind(name, new internal::function(function, lookaheadSafe));
211 }
212
225 template<typename F>
226 inline void bind(const char* name, F function, bool lookaheadSafe = false)
227 {
228 bind(ink::hash_string(name), function, lookaheadSafe);
229 }
230
231#ifdef INK_ENABLE_UNREAL
237 template<typename D>
238 void bind_delegate(hash_t name, D functionDelegate, bool lookaheadSafe)
239 {
240 internal_bind(name, new internal::function_array_delegate(functionDelegate, lookaheadSafe));
241 }
242#endif
243
244#pragma endregion
245
246#pragma region Convenience Methods
247
253 inline operator bool() const { return can_continue(); }
254
260 inline bool has_choices() const { return begin() != end(); }
261
267 size_t num_choices() const;
268
278 const choice* get_choice(size_t index) const;
279
287 inline const choice* operator[](size_t index) { return get_choice(index); }
288
289#pragma endregion
290};
291} // 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 std::string getall()=0
Gets all the text until the next choice or end.
virtual const choice * begin() const =0
Choice iterator.
virtual bool has_tags() const =0
check if since last choice selection tags have been added
virtual void set_rng_seed(uint32_t seed)=0
Sets seed for PRNG used in runner.
virtual std::string getline()=0
Gets the next line of output using C++ STL string.
void bind(hash_t name, F function, bool lookaheadSafe=false)
Binds an external callable to the runtime.
Definition runner.h:208
virtual void choose(size_t index)=0
Make a choice.
virtual snapshot * create_snapshot() const =0
creates a snapshot containing the runner, globals and all other runners connected to the globals.
void bind(const char *name, F function, bool lookaheadSafe=false)
Binds an external callable to the runtime.
Definition runner.h:226
virtual void getall(std::ostream &)=0
Gets all the text until the next choice or end.
virtual size_t num_tags() const =0
return the number of current.
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.
const choice * get_choice(size_t index) const
Gets a choice.
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:260
void bind_delegate(hash_t name, D functionDelegate, bool lookaheadSafe)
bind and unreal delegate
Definition runner.h:238
virtual const char * get_tag(size_t index) const =0
access tag.
const choice * operator[](size_t index)
Shorcut for accessing a choice.
Definition runner.h:287
virtual FString getline()=0
Gets the next line of output using unreal string allocation.
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