-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathscript_runner.h
More file actions
213 lines (187 loc) · 5.03 KB
/
Copy pathscript_runner.h
File metadata and controls
213 lines (187 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
////////////////////////////////////////////////////////////////////////////////
// Distributed under the Boost Software License, Version 1.0. //
// (See accompanying file LICENSE or copy at //
// https://www.boost.org/LICENSE_1_0.txt) //
////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <memory>
#include <tuple>
#include <type_traits>
#include "scripting/script.h"
namespace script_runner_impl
{
/**
* Helper function to get results of script.
*
* @param script
* Script to get results from.
*
* @param result
* Tuple to store results in.
*/
template <int I, int M, class T>
inline void get_results(iris::Script *script, T &result)
{
script->get_result(std::get<M - I - 1>(result));
if constexpr (I != M - 1)
{
get_results<I + 1, M>(script, result);
}
}
/**
* Base case no arguments specialisation.
*/
inline void handle_args(iris::Script *)
{
}
/**
* Base case single arguments specialisation.
*
* @param script
* Script to set arguments for.
*
* @param head
* Head of variadic list.
*
*/
template <class Head>
inline void handle_args(iris::Script *script, Head head)
{
script->set_argument(head);
}
/**
* Template function to recursively set each argument in a variadic list.
*
* @param script
* Script to set arguments for.
*
* @param head
* Head of variadic list.
*
* @param tail
* Rest of variadic list.
*/
template <class Head, class... Tail>
inline void handle_args(iris::Script *script, Head head, Tail... tail)
{
handle_args(script, head);
handle_args(script, tail...);
}
}
namespace iris
{
// Base struct for deducing return type of ScriptRunner::execute.
template <class... R>
struct ReturnHelper;
// Specialisation - no arguments then type is void.
template <>
struct ReturnHelper<>
{
using type = void;
};
// Specialisation - single argument then type is that argument
template <class R>
struct ReturnHelper<R>
{
using type = R;
};
// Specialisation - multiple argumetns then type is tuple of all arguments
template <class... R>
struct ReturnHelper
{
using type = std::tuple<R...>;
};
// convenience alias
template <class... R>
using return_type_t = typename ReturnHelper<R...>::type;
/**
* This is a convenience class for executing a Script object. It abstracts away the need for calling all the individual
* functions in Script.
*/
class ScriptRunner
{
public:
/**
* Construct a new ScriptRunner.
*
* @param script
* Script to run.
*/
ScriptRunner(std::unique_ptr<Script> script)
: script_(std::move(script))
{
}
ScriptRunner(const ScriptRunner &) = delete;
ScriptRunner &operator=(const ScriptRunner &) = delete;
/**
* Execute a function in the script with the supplied arguments.
*
* The supplied template parameters should be the expected result types.
*
* @param function
* Name of function to execute.
*
* @param args
* Arguments to function.
*
* @return
* Either void, a single type or tuple of multiple types depending on supplied template parameters.
*/
template <class... R, class... Args>
return_type_t<R...> execute(const std::string &function, Args &&...args)
{
if constexpr (sizeof...(R) == 0)
{
// no return type (a void function)
std::tuple<> result;
do_execute(function, result, std::forward<Args>(args)...);
}
else if constexpr (sizeof...(R) == 1)
{
// single return type
// create a result tuple of just the single type and we will extract it at the end
std::tuple<return_type_t<R...>> result;
do_execute(function, result, std::forward<Args>(args)...);
return std::get<0>(result);
}
else
{
// multiple return types
return_type_t<R...> result;
do_execute(function, result, std::forward<Args>(args)...);
return result;
}
}
private:
/**
* Do the execution of the script.
*
* @param function
* Name of function to execute.
*
* @param result
* Out parameter for script results. Must be a tuple.
*
* @param args
* Arguments to function.
*/
template <class... Args, class T>
void do_execute(const std::string &function, T &result, Args &&...args)
{
// set function name
script_->set_function(function);
// set function arguments
script_runner_impl::handle_args(script_.get(), std::forward<Args>(args)...);
// execute function
script_->execute(sizeof...(Args), std::tuple_size_v<T>);
// get results (if any)
if constexpr (std::tuple_size_v<T> != 0)
{
script_runner_impl::get_results<0, std::tuple_size_v<T>>(script_.get(), result);
}
}
private:
/** Script object to run. */
std::unique_ptr<Script> script_;
};
}