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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
/*
Ousía
Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _OUSIA_FUNCTION_HPP_
#define _OUSIA_FUNCTION_HPP_
#include <exception>
#include <utility>
#include <vector>
#include "Variant.hpp"
namespace ousia {
namespace script {
/**
* The abstract Function class is most basic version of a function handle,
* maintaining a "call" function and basic virtual functions for lifecyle
* management.
*/
class Function {
public:
/**
* Virtual clone function (e.g. used in the variant class).
*/
virtual Function *clone() const = 0;
/**
* Virtual destructor.
*/
virtual ~Function() {}
/**
* Abstract function which is meant to call the underlying function (be it
* a host or a script function) with the given arguments.
*
* @param args is a vector containing all arguments that shall be passed to
* the function.
* @return a Variant containing the return value.
*/
virtual Variant call(const std::vector<Variant> &args) const = 0;
/**
* Calls the underlying function with no arguments.
*
* @return a Variant containing the return value.
*/
Variant call() const { return call({}); }
// TODO: Use () operator instead of the call function
};
/**
* The Argument class is used to describe the type of a function
* argument.
*/
struct Argument {
const VariantType type;
const bool hasDefault;
const Variant defaultValue;
Argument(VariantType type) : type(type), hasDefault(false){};
Argument(VariantType type, const Variant &defaultValue)
: type(type), hasDefault(true), defaultValue(defaultValue){};
};
/**
* ArgumentValidatorError is an exception type used to represent argument
* validator errors.
*/
class ArgumentValidatorError : public std::exception {
public:
const int index;
const std::string msg;
ArgumentValidatorError(int index, const std::string &msg)
: index(index), msg(msg){};
virtual const char *what() const noexcept override { return msg.c_str(); }
};
/**
* The ArgumentValidator class is responsible for checking whether the given
* arguments passed to a function match the description.
*/
class ArgumentValidator {
private:
/**
* List containing the argument descriptors.
*/
const std::vector<Argument> signature;
/**
* Argument index in the input array, at which the last error occured.
*/
int errorIndex = -1;
/**
* Error message for the last validation error.
*/
std::string errorMessage;
/**
* Used internally to update the errorIndex and the errorMessage fields.
*/
std::pair<bool, std::vector<Variant>> setError(int idx,
const std::string &msg,
std::vector<Variant> &res);
/**
* Resets the error state.
*/
void resetError();
public:
/**
* Constructor of the argument validator class.
*
* @param descriptors is a list of Arguments which should be used
* for the validation.
*/
ArgumentValidator(const std::vector<Argument> &signature)
: signature(signature)
{
}
/**
* Validates and augments the given argument list (e.g. adds the default
* values).
*
* @param args contains the input arguments.
* @return a pair, where the first element specifies whether the arguments
* were validated sucessfully and the second argument contains the augmented
* list of arguments. If false is returned, use the error function to get
* more information about the error.
*/
std::pair<bool, std::vector<Variant>> validate(
const std::vector<Variant> &args);
/**
* Returns an ArgumentValidatorError instance containing the argument index
* in the input array, at which the error occured and an explaining error
* message. As ArgumentValidatorError is derived from std::exception,
* the result of this function is throwable.
*
* @return an ArgumentValidatorError instance containing information about
* the last error. If no error occurred, the message will be empty and
* the argument index will be set to -1.
*/
ArgumentValidatorError error()
{
return ArgumentValidatorError{errorIndex, errorMessage};
}
};
/**
* A validating function
*/
class ValidatingFunction : public Function {
private:
/**
* Specifies whether the validating function should actually run or just
* pass the arguments through.
*/
bool validate;
/**
* Signature for which the function should be validated.
*/
const std::vector<Argument> signature;
protected:
virtual Variant validatedCall(const std::vector<Variant> &args) const = 0;
virtual Variant call(const std::vector<Variant> &args) const override;
using Function::call;
public:
ValidatingFunction() : validate(false) {}
ValidatingFunction(const std::vector<Argument> &signature)
: validate(true), signature(signature)
{
}
};
using HostFunctionCallback = Variant (*)(const std::vector<Variant> &args,
void *data);
using GetterCallback = Variant (*)(void *data);
using SetterCallback = void (*)(Variant arg, void *data);
class HostFunction : public ValidatingFunction {
private:
HostFunctionCallback callback;
void *data;
protected:
virtual Variant validatedCall(
const std::vector<Variant> &args) const override
{
return callback(args, data);
}
public:
HostFunction(HostFunctionCallback callback, std::vector<Argument> signature,
void *data = nullptr)
: ValidatingFunction(signature), callback(callback), data(data)
{
}
HostFunction(HostFunctionCallback callback, void *data = nullptr)
: ValidatingFunction(), callback(callback), data(data)
{
}
Function *clone() const override { return new HostFunction(*this); }
using ValidatingFunction::call;
};
class Getter : public ValidatingFunction {
private:
GetterCallback callback;
void *data;
protected:
virtual Variant validatedCall(
const std::vector<Variant> &args) const override
{
if (!callback) {
// TODO: Use another exception class here
throw "Getter not defined";
}
return callback(data);
}
public:
Getter(GetterCallback callback, void *data = nullptr)
: ValidatingFunction(std::vector<Argument>{}),
callback(callback),
data(data){};
Function *clone() const override { return new Getter(*this); }
Variant call() const { return ValidatingFunction::call(); }
Variant operator()() const { return call(); }
bool exists() const { return callback != nullptr; }
};
class Setter : public ValidatingFunction {
private:
SetterCallback callback;
void *data;
protected:
virtual Variant validatedCall(
const std::vector<Variant> &args) const override
{
if (!callback) {
// TODO: Use another exception class here
throw "Setter not defined";
}
callback(args[0], data);
return Variant::Null;
}
public:
Setter(VariantType type, SetterCallback callback, void *data = nullptr)
: ValidatingFunction({Argument{type}}),
callback(callback),
data(data){};
Function *clone() const override { return new Setter(*this); }
void call(Variant arg) const { ValidatingFunction::call({arg}); }
void operator()(Variant arg) const { return call(arg); }
bool exists() const { return callback != nullptr; }
};
}
}
#endif /* _OUSIA_FUNCTION_HPP_ */
|