summaryrefslogtreecommitdiff
path: root/src/core/script/Function.hpp
blob: 4c13dbc7da0276229023966217e12e1e67b7880a (plain)
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
/*
    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 --
 * just a virtual "call" function which calls the underlying code.
 */
class Function {

public:

	/**
	 * 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({}); }

};

/**
 * The ArgumentDescriptor class is used to describe the type of a function
 * argument.
 */
struct ArgumentDescriptor {

	const VariantType type;
	const bool hasDefault;
	const Variant defaultValue;

	ArgumentDescriptor(VariantType type) :
		type(type), hasDefault(false) {};

	ArgumentDescriptor(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<ArgumentDescriptor> descriptors;

	/**
	 * 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;

	std::pair<bool, std::vector<Variant>> setError(int idx,
			const std::string &msg, std::vector<Variant> &res);

	void resetError();

public:

	/**
	 * Constructor of the argument validator class.
	 *
	 * @param descriptors is a list of ArgumentDescriptors which should be used
	 * for the validation.
	 */
	ArgumentValidator(const std::vector<ArgumentDescriptor> &descriptors) :
		descriptors(descriptors) {}

	/**
	 * 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);
	}

};

/**
 * The HostFunction class represents a function that resides in the script host.
 */
template<class T>
class HostFunction : public Function {

private:
	T callback;
	ArgumentValidator *validator;
	void *data;

public:

	HostFunction(T callback, std::vector<ArgumentDescriptor> signature,
			void *data = nullptr) :
		callback(callback), validator(new ArgumentValidator(signature)),
		data(data) {}

	HostFunction(T callback, void *data = nullptr) :
		callback(callback), validator(nullptr), data(data) {}

	~HostFunction()
	{
		delete validator;
	}

	virtual Variant call(const std::vector<Variant> &args) const override
	{
		if (validator) {
			std::pair<bool, std::vector<Variant>> res = validator->validate(args);
			if (!res.first) {
				throw validator->error();
			}
			return callback(res.second, data);
		} else {
			return callback(args, data);
		}
	}

	using Function::call;

};

template<class T>
static HostFunction<T> createHostFunction(T callback,
		std::vector<ArgumentDescriptor> signature, void *data = nullptr)
{
	return HostFunction<T>(callback, signature, data);
}

template<class T>
static HostFunction<T> createHostFunction(T callback, void *data = nullptr)
{
	return HostFunction<T>(callback, data);
}


}
}

#endif /* _OUSIA_FUNCTION_HPP_ */