]> git.tdb.fi Git - libs/datafile.git/blob - source/loaderaction.h
Require C++11 for building
[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 template<typename T>
12 std::string create_signature(const std::string &prefix = std::string())
13 { return prefix+std::string(1, TypeInfo<T>::signature); }
14
15 template<typename T0, typename T1, typename... Tail>
16 std::string create_signature(const std::string &prefix = std::string())
17 { return create_signature<T1, Tail...>(prefix+std::string(1, TypeInfo<T0>::signature)); }
18
19 class Loader;
20
21 /**
22 Base class for loader actions.
23 */
24 class LoaderAction
25 {
26 protected:
27         LoaderAction() { }
28 public:
29         virtual ~LoaderAction() { }
30
31         /** Called to process a statement. */
32         virtual void execute(Loader &, const Statement &) const = 0;
33
34         virtual void execute(Loader &, const ArgumentStore &) const = 0;
35
36         virtual std::string get_signature() const = 0;
37 };
38
39
40 /**
41 Loads a statement by calling a function that takes no arguments.
42 */
43 template<typename L>
44 class LoaderFunc0: public LoaderAction
45 {
46 private:
47         typedef void (L::*FuncType)();
48
49         FuncType func;
50
51 public:
52         LoaderFunc0(FuncType f): func(f) { }
53
54         virtual void execute(Loader &l, const Statement &) const
55         {
56                 (dynamic_cast<L &>(l).*func)();
57         };
58
59         virtual void execute(Loader &l, const ArgumentStore &) const
60         {
61                 (dynamic_cast<L &>(l).*func)();
62         };
63
64         virtual std::string get_signature() const
65         { return std::string(); }
66 };
67
68
69 /**
70 Loads a statement by calling a function that takes one argument.
71 */
72 template<typename L, typename A0>
73 class LoaderFunc1: public LoaderAction
74 {
75 private:
76         typedef void (L::*FuncType)(A0);
77
78         FuncType func;
79
80 public:
81         LoaderFunc1(FuncType f): func(f) { }
82
83         virtual void execute(Loader &l, const Statement &st) const
84         {
85                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>());
86         }
87
88         virtual void execute(Loader &l, const ArgumentStore &as) const
89         {
90                 (dynamic_cast<L &>(l).*func)(as.get<A0>(0));
91         }
92
93         virtual std::string get_signature() const
94         { return std::string(1, TypeInfo<A0>::signature); }
95 };
96
97
98 /**
99 Loads a statement by calling a function that takes an array of values.
100 */
101 template<typename L, typename A0>
102 class LoaderFunc1<L, const std::vector<A0> &>: public LoaderAction
103 {
104 private:
105         typedef void (L::*FuncType)(const std::vector<A0> &);
106
107         FuncType func;
108
109 public:
110         LoaderFunc1(FuncType f): func(f) { }
111
112         virtual void execute(Loader &l, const Statement &st) const
113         {
114                 std::vector<A0> values;
115                 values.reserve(st.args.size());
116                 for(Statement::Arguments::const_iterator i=st.args.begin(); i!=st.args.end(); ++i)
117                         values.push_back(i->get<A0>());
118                 (dynamic_cast<L &>(l).*func)(values);
119         }
120
121         virtual void execute(Loader &l, const ArgumentStore &as) const
122         {
123                 std::vector<A0> values;
124                 unsigned n_args = as.get_info().key.signature.size();
125                 values.reserve(n_args);
126                 for(unsigned i=0; i<n_args; ++i)
127                         values.push_back(as.get<A0>(i));
128                 (dynamic_cast<L &>(l).*func)(values);
129         }
130
131         virtual std::string get_signature() const
132         {
133                 std::string result;
134                 result += TypeInfo<A0>::signature;
135                 result += '*';
136                 return result;
137         }
138 };
139
140
141 /**
142 Loads a statement by calling a function with the statement itself as argument.
143 */
144 template<typename L>
145 class LoaderFunc1<L, const Statement &>: public LoaderAction
146 {
147 private:
148         typedef void (L::*FuncType)(const Statement &);
149
150         FuncType func;
151
152 public:
153         LoaderFunc1(FuncType f): func(f) { }
154
155         virtual void execute(Loader &l, const Statement &st) const
156         {
157                 (dynamic_cast<L &>(l).*func)(st);
158         }
159
160         virtual void execute(Loader &, const ArgumentStore &) const
161         {
162                 throw std::logic_error("incompatible format");
163         }
164
165         virtual std::string get_signature() const
166         { return "*"; }
167 };
168
169
170 template<unsigned I, typename... Args>
171 struct Apply;
172
173 template<unsigned I>
174 struct Apply<I>
175 {
176         template<typename L, typename F, typename... Args>
177         static void apply(L &l, F func, const Statement &, Args... args)
178         {
179                 (l.*func)(args...);
180         }
181
182         template<typename L, typename F, typename... Args>
183         static void apply(L &l, F func, const ArgumentStore &, Args... args)
184         {
185                 (l.*func)(args...);
186         }
187 };
188
189 template<unsigned I, typename Head, typename... Tail>
190 struct Apply<I, Head, Tail...>
191 {
192         template<typename L, typename F, typename... Args>
193         static void apply(L &l, F func, const Statement &st, Args... args)
194         {
195                 Apply<I+1, Tail...>::apply(l, func, st, args..., st.args[I].get<Head>());
196         }
197
198         template<typename L, typename F, typename... Args>
199         static void apply(L &l, F func, const ArgumentStore &as, Args... args)
200         {
201                 Apply<I+1, Tail...>::apply(l, func, as, args..., as.get<Head>(I));
202         }
203 };
204
205
206 template<typename L, typename... Args>
207 class LoaderFuncN: public LoaderAction
208 {
209 protected:
210         typedef void (L::*FuncType)(Args...);
211
212         FuncType func;
213
214 public:
215         LoaderFuncN(FuncType f): func(f) { }
216
217         virtual void execute(Loader &l, const Statement &st) const
218         {
219                 Apply<0, Args...>::apply(dynamic_cast<L &>(l), func, st);
220         }
221
222         virtual void execute(Loader &l, const ArgumentStore &as) const
223         {
224                 Apply<0, Args...>::apply(dynamic_cast<L &>(l), func, as);
225         }
226
227         virtual std::string get_signature() const
228         { return create_signature<Args...>(); }
229 };
230
231
232 template<typename L, typename B0, typename... Args>
233 class LoaderFuncNBound1: public LoaderAction
234 {
235 protected:
236         typedef void (L::*FuncType)(B0, Args...);
237         typedef typename RemoveReference<B0>::Type Bound0Type;
238
239         FuncType func;
240         Bound0Type bound0;
241
242 public:
243         LoaderFuncNBound1(FuncType f, const Bound0Type &b0): func(f), bound0(b0) { }
244
245         virtual void execute(Loader &l, const Statement &st) const
246         {
247                 Apply<0, Args...>::apply(dynamic_cast<L &>(l), func, st, bound0);
248         }
249
250         virtual void execute(Loader &l, const ArgumentStore &as) const
251         {
252                 Apply<0, Args...>::apply(dynamic_cast<L &>(l), func, as, bound0);
253         }
254
255         virtual std::string get_signature() const
256         { return create_signature<Args...>(); }
257 };
258
259
260 template<typename L, typename T0>
261 class LoadValue1: public LoaderAction
262 {
263 private:
264         typedef T0 L::*Pointer0Type;
265
266         Pointer0Type ptr0;
267
268 public:
269         LoadValue1(Pointer0Type p0): ptr0(p0) { }
270
271         virtual void execute(Loader &l, const Statement &st) const
272         {
273                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0 = st.args[0].get<T0>();
274         }
275
276         virtual void execute(Loader &l, const ArgumentStore &as) const
277         {
278                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0 = as.get<T0>(0);
279         }
280
281         virtual std::string get_signature() const
282         { return std::string(1, TypeInfo<T0>::signature); }
283 };
284
285
286 template<typename L, typename T0>
287 class LoadValue1<L, T0 *>: public LoaderAction
288 {
289 private:
290         typedef T0 *L::*Pointer0Type;
291
292         Pointer0Type ptr0;
293
294 public:
295         LoadValue1(Pointer0Type p0): ptr0(p0) { }
296
297         virtual void execute(Loader &l, const Statement &st) const
298         {
299                 typename L::Loader &ldr = dynamic_cast<typename L::Loader &>(l);
300                 ldr.get_object().*ptr0 = &ldr.get_collection().template get<T0>(st.args[0].get<std::string>());
301         }
302
303         virtual void execute(Loader &l, const ArgumentStore &as) const
304         {
305                 typename L::Loader &ldr = dynamic_cast<typename L::Loader &>(l);
306                 ldr.get_object().*ptr0 = &ldr.get_collection().template get<T0>(as.get<std::string>(0));
307         }
308
309         virtual std::string get_signature() const
310         { return std::string(1, TypeInfo<std::string>::signature); }
311 };
312
313
314 template<typename L, typename T0, typename T1>
315 class LoadValue2: public LoaderAction
316 {
317 private:
318         typedef T0 L::*Pointer0Type;
319         typedef T1 L::*Pointer1Type;
320
321         Pointer0Type ptr0;
322         Pointer1Type ptr1;
323
324 public:
325         LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
326
327         virtual void execute(Loader &l, const Statement &st) const
328         {
329                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0 = st.args[0].get<T0>();
330                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr1 = st.args[1].get<T1>();
331         }
332
333         virtual void execute(Loader &l, const ArgumentStore &as) const
334         {
335                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0 = as.get<T0>(0);
336                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr1 = as.get<T1>(1);
337         }
338
339         virtual std::string get_signature() const
340         {
341                 std::string result;
342                 result += TypeInfo<T0>::signature;
343                 result += TypeInfo<T1>::signature;
344                 return result;
345         }
346 };
347
348 } // namespace DataFile
349 } // namespace Msp
350
351 #endif