summaryrefslogtreecommitdiff
path: root/src/core/common/Function.hpp
blob: 0e8af126a49399acab1a1a848c60c186b9bcbaa9 (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
227
/*
    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/>.
*/

/**
 * @file Function.hpp
 *
 * Contains the definition of a Function class used to describe both methods and
 * functions in the host code and functions in the script code.
 *
 * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de)
 */

#ifndef _OUSIA_FUNCTION_HPP_
#define _OUSIA_FUNCTION_HPP_

#include <cassert>

#include "Argument.hpp"
#include "Variant.hpp"

namespace ousia {

/**
 * The Function interface defines all the methods needed to represent a
 * generic function. Function objects can be called using the "call" function in
 * which an array of Variant is supplied to the function and a Variant is
 * returned to the caller. The actual function that is being represented by an
 * instance of the Function class may either be a C++ function or a function
 * residing in some script.
 */
class Function {
protected:
	/**
	 * Protecte default constructor -- prevents the Function class from being
	 * created. Use one of the child classes instead.
	 */
	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 doCall(Variant::arrayType &args, void *thisRef) const = 0;

public:
	// No copy constructor
	Function(const Function &) = delete;

	// No move constructor
	Function(Function &&) = delete;

	/**
	 * Virtual destructor of the Function class.
	 */
	virtual ~Function(){};

	/**
	 * Calls the function.
	 *
	 * @param args is an array of variants that should be passed to the
	 * function. Note that the arguments might be modified, e.g. by a validation
	 * process or the called function itself.
	 * @param thisRef is a user-defined reference which may be pointing at the
	 * object the function should be working on.
	 * @return a Variant containing the result of the function call.
	 */
	Variant call(Variant::arrayType &args, void *thisRef = nullptr) const
	{
		return doCall(args, thisRef);
	}

	/**
	 * Calls the function.
	 *
	 * @param args is an array of variants that should be passed to the
	 * function.
	 * @param thisRef is a user-defined reference which may be pointing at the
	 * object the function should be working on.
	 * @return a Variant containing the result of the function call.
	 */
	Variant call(const Variant::arrayType &args = Variant::arrayType{},
	             void *thisRef = nullptr) const
	{
		Variant::arrayType argsCopy = args;
		return doCall(argsCopy, thisRef);
	}
};

/**
 * Function doing nothing. Instances of this class are used as default values
 * for instances of the Function class.
 */
class FunctionStub : public Function {
protected:
	Variant doCall(Variant::arrayType &, void *) const override
	{
		return nullptr;
	}

public:
	/**
	 * Constructor of the FunctionStub class.
	 */
	FunctionStub() {}
};

/**
 * Function class providing factilities for the validation of arguments.
 */
class ValidatingFunction : public Function {
private:
	/**
	 * List describing a valid set to arguments.
	 */
	Arguments arguments;

	/**
	 * Set to true if any arguments for checking were given in the constructor.
	 * If set to false, no argument checks are performed.
	 */
	bool checkArguments;

protected:
	/**
	 * Default constructor. Disables validation, all arguments are allowed.
	 */
	ValidatingFunction() : checkArguments(false){};

	/**
	 * Default constructor. Disables validation, all arguments are allowed.
	 */
	ValidatingFunction(Arguments arguments)
	    : arguments(std::move(arguments)), checkArguments(true){};

	/**
	 * Function which cares about validating a set of arguments.
	 *
	 * @param args is an array containing the arguments that should be
	 * validated.
	 * @return the reference to the array.
	 */
	Variant::arrayType &validate(Variant::arrayType &args) const;
};

/**
 * The Method class refers to a method in the C++ code, belonging to an object
 * of a certain type T.
 *
 * @tparam T is the type of the method that should be called.
 */
template <class T>
class Method : public ValidatingFunction {
public:
	/**
	 * Type of the Callback function that is being called by the "call"
	 * function.
	 *
	 * @param args contains the input arguments that were passed to the
	 * function.
	 * @param thisRef is a pointer pointing at an instance of type T.
	 * @return the return value of the function as Variant instance.
	 */
	using Callback = Variant (*)(Variant::arrayType &args, T *thisRef);

private:
	/**
	 * Pointer at the actual C++ method being called.
	 */
	const Callback method;

protected:
	/**
	 * Calls the underlying method.
	 *
	 * @param args is a vector containing all arguments that should be passed
	 * to the method.
	 * @return a Variant containing the return value.
	 */
	Variant doCall(Variant::arrayType &args, void *thisRef) const override
	{
		return method(validate(args), static_cast<T *>(thisRef));
	}

public:
	/**
	 * Constructor of the Method class with a description of the arguments that
	 * are to be passed to the callback method.
	 *
	 * @param arguments is a type description restricting the arguments that are
	 * being passed to the callback function.
	 * @param method is the actual callback function that is being called once
	 * the method is executed. The arguments passed to the method are validated
	 * using the given argument descriptor.
	 */
	Method(Arguments arguments, Callback method)
	    : ValidatingFunction(arguments), method(method){};

	/**
	 * Constructor of the Method class.
	 *
	 * @param method is a pointer at the C++ function that should be called.
	 */
	Method(Callback method) : method(method){};
};
}

#endif /* _OUSIA_FUNCTION_HPP_ */