summaryrefslogtreecommitdiff
path: root/tests/simple/test.cpp
blob: dbae91e67b53f49ed26253b8d829a72cebf89c1c (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
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
#include <iostream>
#include <functional>
#include <sstream>
#include <vector>
#include <map>
#include <stdlib.h>

using namespace std;

/**
 *  Example function that adds numbers
 *  @param	a	A number
 *  @param	b	A different number
 * 	@return		Sum of both parameters
 */
int my_plus(int a, int b)
{
	cout << "my_plus(" << a << ", " << b << ")" << endl;

	return a + b;
}

/**
 *  Another example function that concatenates strings
 *  @param	a	A text
 *  @param	b	A different text
 *  @return		Concattenation
 */
const string my_concat(const string &a, const string &b)
{
	cout << "my_concat(" << a << ", " << b << ")" << endl;
	
	return a + b;
}

/**
 *  Value class that can automatically convert objects between many
 *  different types
 */ 
class Value
{
private:
	/**
	 *  Internal representation
	 *	@var _value
	 */
	string _value;

public:
	/**
	 *  Constructor based on string
	 *  @param	text
	 */
	Value(const string &text) : _value(text) {}

	/**
	 *  Constructor based on string
	 *  @param	text
	 */
	Value(const char *text) : _value(text) {}
	
	/**
	 *  Constructor based on int
	 *  @param	integer
	 */
	Value(int value)
	{
		ostringstream s;
		s << value;
		_value = s.str();
	}
	
	/**
	 *  Destructor
	 */
	virtual ~Value() {}
	
	/**
	 *  Cast to integer
	 *  @return		Numeric representation
	 */
	operator int ()
	{
		return atoi(_value.c_str());
	}

	/**
	 *  Cast to string
	 *  @return		String representation
	 */
	operator const string& ()
	{
		return _value;
	}
};

/**
 *  Simple wrapper around a function
 */
class Function
{
private:
	/**
	 *  The actual function
	 * 
	 * 	Note: 	there must be a much smarter way to achieve the same thing,
	 * 			we just want to have a single member that holds the function
	 * 
	 *  @var	_function
	 */
	union F {
		// union members
		function<Value()> r0;
		function<Value(Value&)> r1;
		function<Value(Value&,Value&)> r2;
		function<void()> v0;
		function<void(Value&)> v1;
		function<void(Value&,Value&)> v2;
		
		// constructors
		F() {}
		F(function<Value()> &f) { r0 = f; }
		F(function<Value(Value&)> &f) { r1 = f; }
		F(function<Value(Value&,Value&)> &f) { r2 = f; }
		F(function<void()> &f) { v0 = f; }
		F(function<void(Value&)> &f) { v1 = f; }
		F(function<void(Value&,Value&)> &f) { v2 = f; }
		
		// destructor
		~F();
		
		// assignment operator
//		F &operator=(function<Value()> &f) { r0 = f; return *this; }
//		F &operator=(function<Value(Value&)> &f) { r1 = f; return *this; }
//		F &operator=(function<Value(Value&,Value&)> &f) { r2 = f; return *this; }
		
	} _function;
	
	/**
	 *  Is the object in a valid state
	 *  @var	bool
	 */
	bool _valid;
	
	/**
	 *  The number of arguments that the function required
	 *  @var	_argc
	 */
	int _argc;
	
	/**
	 *  Does the function return something?
	 *  @var	_return;
	 */
	bool _return;
	
public:
	/**
	 *  Contructor
	 *  @param	f
	 */
	Function(function<Value()> &f) : _function(f)
	{
		_argc = 0;
		_return = true;
		_valid = true;
	}
	
	/**
	 *  Contructor
	 *  @param	f
	 */
	Function(function<Value(Value&)> &f) : _function(f)
	{
		_argc = 1;
		_return = true;
	}

	/**
	 *  Contructor
	 *  @param	f
	 */
	Function(function<Value(Value&,Value&)> &f) : _function(f)
	{
		_argc = 2;
		_return = true;
	}
	
	/**
	 *  Move constructor
	 *  @param	f
	 */
	Function(const Function &&f)
	{
		// copy params
		_argc = f._argc;
		_return = f._return;
		
		switch (_argc) {
		case 0: _function.r0 = f._function.r0; break;
		case 1: _function.r1 = f._function.r1; break;
		case 2: _function.r2 = f._function.r2; break;
		}
	} 
	
	/**
	 *  Destructor
	 */
	virtual ~Function() {}
	
	/**
	 *  Operator to call the function
	 *  @param	args
	 */
	void operator()(vector<Value> args)
	{
		switch (_argc) {
		case 0:	_function.r0();
		case 1: _function.r1(args[0]);
		case 2: _function.r2(args[0], args[1]);
		}
	}
};

/**
 *  Class that stores pointers to functions
 */
class Functions
{
private:
	/**
	 *  Map of functions
	 * 	@var map
	 */
	map<string,Function> _functions;

public:
	/**
	 *  Constructor
	 */
	Functions() {}
	
	/**
	 *  Destructor
	 */
	virtual ~Functions() {}
	
	/**
	 *  Add a function
	 * 	@param	name		The function name
	 * 	@param	func		The function to call
	 */
	void add(const string &name, function<Value(Value&,Value&)> f)
	{
		_functions.insert(pair<string,Function>(name, Function(f)));
	}
	
	/**
	 *  Call the functions
	 *  @param	args
	 */
	Value operator()(vector<Value> args)
	{
		for (auto it = _functions.begin(); it != _functions.end(); it++)
		{
			it->second(args);
		}
		
		return Value(123);
	}

};

/**
 *  Main function
 * 	@param	argc
 *  @param	argv
 */
int main(int argc, char *argv[])
{
	// create a vector of arguments
	vector<Value> arguments;
	
	// fill the arguments
	for (int i=1; i<argc-1; i++) arguments.push_back(argv[i]);
	
	// now register all functions
	Functions functions;
	functions.add("plus", my_plus);
	functions.add("concat", my_concat);
	
	// call all functions
	functions(arguments);
	
	// done
	return 0;
}