]> git.tdb.fi Git - libs/datafile.git/blob - source/loaderaction.h
Cosmetic changes
[libs/datafile.git] / source / loaderaction.h
1 #ifndef MSP_DATAFILE_LOADERACTION_H_
2 #define MSP_DATAFILE_LOADERACTION_H_
3
4 #include <msp/core/meta.h>
5 #include "argumentstore.h"
6 #include "statement.h"
7
8 namespace Msp {
9 namespace DataFile {
10
11 #if __cplusplus>=201103L
12 template<typename T>
13 std::string create_signature(const std::string &prefix = std::string())
14 { return prefix+std::string(1, TypeInfo<T>::signature); }
15
16 template<typename T0, typename T1, typename... Tail>
17 std::string create_signature(const std::string &prefix = std::string())
18 { return create_signature<T1, Tail...>(prefix+std::string(1, TypeInfo<T0>::signature)); }
19 #endif
20
21 class Loader;
22
23 /**
24 Base class for loader actions.
25 */
26 class LoaderAction
27 {
28 protected:
29         LoaderAction() { }
30 public:
31         virtual ~LoaderAction() { }
32
33         /** Called to process a statement. */
34         virtual void execute(Loader &, const Statement &) const = 0;
35
36         virtual void execute(Loader &, const ArgumentStore &) const = 0;
37
38         virtual std::string get_signature() const = 0;
39 };
40
41
42 /**
43 Loads a statement by calling a function that takes no arguments.
44 */
45 template<typename L>
46 class LoaderFunc0: public LoaderAction
47 {
48 private:
49         typedef void (L::*FuncType)();
50
51         FuncType func;
52
53 public:
54         LoaderFunc0(FuncType f): func(f) { }
55
56         virtual void execute(Loader &l, const Statement &) const
57         {
58                 (dynamic_cast<L &>(l).*func)();
59         };
60
61         virtual void execute(Loader &l, const ArgumentStore &) const
62         {
63                 (dynamic_cast<L &>(l).*func)();
64         };
65
66         virtual std::string get_signature() const
67         { return std::string(); }
68 };
69
70
71 /**
72 Loads a statement by calling a function that takes one argument.
73 */
74 template<typename L, typename A0>
75 class LoaderFunc1: public LoaderAction
76 {
77 private:
78         typedef void (L::*FuncType)(A0);
79
80         FuncType func;
81
82 public:
83         LoaderFunc1(FuncType f): func(f) { }
84
85         virtual void execute(Loader &l, const Statement &st) const
86         {
87                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>());
88         }
89
90         virtual void execute(Loader &l, const ArgumentStore &as) const
91         {
92                 (dynamic_cast<L &>(l).*func)(as.get<A0>(0));
93         }
94
95         virtual std::string get_signature() const
96         { return std::string(1, TypeInfo<A0>::signature); }
97 };
98
99
100 /**
101 Loads a statement by calling a function that takes an array of values.
102 */
103 template<typename L, typename A0>
104 class LoaderFunc1<L, const std::vector<A0> &>: public LoaderAction
105 {
106 private:
107         typedef void (L::*FuncType)(const std::vector<A0> &);
108
109         FuncType func;
110
111 public:
112         LoaderFunc1(FuncType f): func(f) { }
113
114         virtual void execute(Loader &l, const Statement &st) const
115         {
116                 std::vector<A0> values;
117                 values.reserve(st.args.size());
118                 for(Statement::Arguments::const_iterator i=st.args.begin(); i!=st.args.end(); ++i)
119                         values.push_back(i->get<A0>());
120                 (dynamic_cast<L &>(l).*func)(values);
121         }
122
123         virtual void execute(Loader &l, const ArgumentStore &as) const
124         {
125                 std::vector<A0> values;
126                 unsigned n_args = as.get_info().key.signature.size();
127                 values.reserve(n_args);
128                 for(unsigned i=0; i<n_args; ++i)
129                         values.push_back(as.get<A0>(i));
130                 (dynamic_cast<L &>(l).*func)(values);
131         }
132
133         virtual std::string get_signature() const
134         {
135                 std::string result;
136                 result += TypeInfo<A0>::signature;
137                 result += '*';
138                 return result;
139         }
140 };
141
142
143 /**
144 Loads a statement by calling a function with the statement itself as argument.
145 */
146 template<typename L>
147 class LoaderFunc1<L, const Statement &>: public LoaderAction
148 {
149 private:
150         typedef void (L::*FuncType)(const Statement &);
151
152         FuncType func;
153
154 public:
155         LoaderFunc1(FuncType f): func(f) { }
156
157         virtual void execute(Loader &l, const Statement &st) const
158         {
159                 (dynamic_cast<L &>(l).*func)(st);
160         }
161
162         virtual void execute(Loader &, const ArgumentStore &) const
163         {
164                 throw std::logic_error("incompatible format");
165         }
166
167         virtual std::string get_signature() const
168         { return "*"; }
169 };
170
171
172 template<typename L, typename A0, typename A1>
173 class LoaderFunc2: public LoaderAction
174 {
175 private:
176         typedef void (L::*FuncType)(A0, A1);
177
178         FuncType func;
179
180 public:
181         LoaderFunc2(FuncType f): func(f) { }
182
183         virtual void execute(Loader &l, const Statement &st) const
184         {
185                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>());
186         }
187
188         virtual void execute(Loader &l, const ArgumentStore &as) const
189         {
190                 (dynamic_cast<L &>(l).*func)(as.get<A0>(0), as.get<A1>(1));
191         }
192
193         virtual std::string get_signature() const
194         {
195                 std::string result;
196                 result += TypeInfo<A0>::signature;
197                 result += TypeInfo<A1>::signature;
198                 return result;
199         }
200 };
201
202
203 template<typename L, typename A0, typename A1, typename A2>
204 class LoaderFunc3: public LoaderAction
205 {
206 private:
207         typedef void (L::*FuncType)(A0, A1, A2);
208
209         FuncType func;
210
211 public:
212         LoaderFunc3(FuncType f): func(f) { }
213
214         virtual void execute(Loader &l, const Statement &st) const
215         {
216                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>());
217         }
218
219         virtual void execute(Loader &l, const ArgumentStore &as) const
220         {
221                 (dynamic_cast<L &>(l).*func)(as.get<A0>(0), as.get<A1>(1), as.get<A2>(2));
222         }
223
224         virtual std::string get_signature() const
225         {
226                 std::string result;
227                 result += TypeInfo<A0>::signature;
228                 result += TypeInfo<A1>::signature;
229                 result += TypeInfo<A2>::signature;
230                 return result;
231         }
232 };
233
234
235 template<typename L, typename A0, typename A1, typename A2, typename A3>
236 class LoaderFunc4: public LoaderAction
237 {
238 private:
239         typedef void (L::*FuncType)(A0, A1, A2, A3);
240
241         FuncType func;
242
243 public:
244         LoaderFunc4(FuncType f): func(f) { }
245
246         virtual void execute(Loader &l, const Statement &st) const
247         {
248                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>(), st.args[3].get<A3>());
249         }
250
251         virtual void execute(Loader &l, const ArgumentStore &as) const
252         {
253                 (dynamic_cast<L &>(l).*func)(as.get<A0>(0), as.get<A1>(1), as.get<A2>(2), as.get<A3>(3));
254         }
255
256         virtual std::string get_signature() const
257         {
258                 std::string result;
259                 result += TypeInfo<A0>::signature;
260                 result += TypeInfo<A1>::signature;
261                 result += TypeInfo<A2>::signature;
262                 result += TypeInfo<A3>::signature;
263                 return result;
264         }
265 };
266
267
268 template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
269 class LoaderFunc5: public LoaderAction
270 {
271 private:
272         typedef void (L::*FuncType)(A0, A1, A2, A3, A4);
273
274         FuncType func;
275
276 public:
277         LoaderFunc5(FuncType f): func(f) { }
278
279         virtual void execute(Loader &l, const Statement &st) const
280         {
281                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>(), st.args[3].get<A3>(), st.args[4].get<A4>());
282         }
283
284         virtual void execute(Loader &l, const ArgumentStore &as) const
285         {
286                 (dynamic_cast<L &>(l).*func)(as.get<A0>(0), as.get<A1>(1), as.get<A2>(2), as.get<A3>(3), as.get<A4>(4));
287         }
288
289         virtual std::string get_signature() const
290         {
291                 std::string result;
292                 result += TypeInfo<A0>::signature;
293                 result += TypeInfo<A1>::signature;
294                 result += TypeInfo<A2>::signature;
295                 result += TypeInfo<A3>::signature;
296                 result += TypeInfo<A4>::signature;
297                 return result;
298         }
299 };
300
301
302 #if __cplusplus>=201103L
303 template<unsigned I, typename... Args>
304 struct Apply;
305
306 template<unsigned I>
307 struct Apply<I>
308 {
309         template<typename L, typename F, typename... Args>
310         static void apply(L &l, F func, const Statement &, Args... args)
311         {
312                 (l.*func)(args...);
313         }
314
315         template<typename L, typename F, typename... Args>
316         static void apply(L &l, F func, const ArgumentStore &, Args... args)
317         {
318                 (l.*func)(args...);
319         }
320 };
321
322 template<unsigned I, typename Head, typename... Tail>
323 struct Apply<I, Head, Tail...>
324 {
325         template<typename L, typename F, typename... Args>
326         static void apply(L &l, F func, const Statement &st, Args... args)
327         {
328                 Apply<I+1, Tail...>::apply(l, func, st, args..., st.args[I].get<Head>());
329         }
330
331         template<typename L, typename F, typename... Args>
332         static void apply(L &l, F func, const ArgumentStore &as, Args... args)
333         {
334                 Apply<I+1, Tail...>::apply(l, func, as, args..., as.get<Head>(I));
335         }
336 };
337
338
339 template<typename L, typename... Args>
340 class LoaderFuncN: public LoaderAction
341 {
342 protected:
343         typedef void (L::*FuncType)(Args...);
344
345         FuncType func;
346
347 public:
348         LoaderFuncN(FuncType f): func(f) { }
349
350         virtual void execute(Loader &l, const Statement &st) const
351         {
352                 Apply<0, Args...>::apply(dynamic_cast<L &>(l), func, st);
353         }
354
355         virtual void execute(Loader &l, const ArgumentStore &as) const
356         {
357                 Apply<0, Args...>::apply(dynamic_cast<L &>(l), func, as);
358         }
359
360         virtual std::string get_signature() const
361         { return create_signature<Args...>(); }
362 };
363
364
365 template<typename L, typename B0, typename... Args>
366 class LoaderFuncNBound1: public LoaderAction
367 {
368 protected:
369         typedef void (L::*FuncType)(B0, Args...);
370         typedef typename RemoveReference<B0>::Type Bound0Type;
371
372         FuncType func;
373         Bound0Type bound0;
374
375 public:
376         LoaderFuncNBound1(FuncType f, const Bound0Type &b0): func(f), bound0(b0) { }
377
378         virtual void execute(Loader &l, const Statement &st) const
379         {
380                 Apply<0, Args...>::apply(dynamic_cast<L &>(l), func, st, bound0);
381         }
382
383         virtual void execute(Loader &l, const ArgumentStore &as) const
384         {
385                 Apply<0, Args...>::apply(dynamic_cast<L &>(l), func, as, bound0);
386         }
387
388         virtual std::string get_signature() const
389         { return create_signature<Args...>(); }
390 };
391 #endif
392
393
394 template<typename L, typename T0>
395 class LoadValue1: public LoaderAction
396 {
397 private:
398         typedef T0 L::*Pointer0Type;
399
400         Pointer0Type ptr0;
401
402 public:
403         LoadValue1(Pointer0Type p0): ptr0(p0) { }
404
405         virtual void execute(Loader &l, const Statement &st) const
406         {
407                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0 = st.args[0].get<T0>();
408         }
409
410         virtual void execute(Loader &l, const ArgumentStore &as) const
411         {
412                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0 = as.get<T0>(0);
413         }
414
415         virtual std::string get_signature() const
416         { return std::string(1, TypeInfo<T0>::signature); }
417 };
418
419
420 template<typename L, typename T0>
421 class LoadValue1<L, T0 *>: public LoaderAction
422 {
423 private:
424         typedef T0 *L::*Pointer0Type;
425
426         Pointer0Type ptr0;
427
428 public:
429         LoadValue1(Pointer0Type p0): ptr0(p0) { }
430
431         virtual void execute(Loader &l, const Statement &st) const
432         {
433                 typename L::Loader &ldr = dynamic_cast<typename L::Loader &>(l);
434                 ldr.get_object().*ptr0 = &ldr.get_collection().template get<T0>(st.args[0].get<std::string>());
435         }
436
437         virtual void execute(Loader &l, const ArgumentStore &as) const
438         {
439                 typename L::Loader &ldr = dynamic_cast<typename L::Loader &>(l);
440                 ldr.get_object().*ptr0 = &ldr.get_collection().template get<T0>(as.get<std::string>(0));
441         }
442
443         virtual std::string get_signature() const
444         { return std::string(1, TypeInfo<std::string>::signature); }
445 };
446
447
448 template<typename L, typename T0, typename T1>
449 class LoadValue2: public LoaderAction
450 {
451 private:
452         typedef T0 L::*Pointer0Type;
453         typedef T1 L::*Pointer1Type;
454
455         Pointer0Type ptr0;
456         Pointer1Type ptr1;
457
458 public:
459         LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
460
461         virtual void execute(Loader &l, const Statement &st) const
462         {
463                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0 = st.args[0].get<T0>();
464                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr1 = st.args[1].get<T1>();
465         }
466
467         virtual void execute(Loader &l, const ArgumentStore &as) const
468         {
469                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0 = as.get<T0>(0);
470                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr1 = as.get<T1>(1);
471         }
472
473         virtual std::string get_signature() const
474         {
475                 std::string result;
476                 result += TypeInfo<T0>::signature;
477                 result += TypeInfo<T1>::signature;
478                 return result;
479         }
480 };
481
482 } // namespace DataFile
483 } // namespace Msp
484
485 #endif