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