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