3 This file is part of libmspdatafile
4 Copyright © 2008, 2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #ifndef MSP_DATAFILE_LOADERACTION_H_
9 #define MSP_DATAFILE_LOADERACTION_H_
12 #include "statement.h"
20 Base class for loader actions.
27 virtual ~LoaderAction() { }
29 /** Called to process a statement. */
30 virtual void execute(Loader &, const Statement &) const = 0;
32 virtual std::string get_signature() const = 0;
37 Loads a statement by calling a function that takes no arguments.
40 class LoaderFunc0: public LoaderAction
43 typedef void (L::*FuncType)();
48 LoaderFunc0(FuncType f): func(f) { }
50 virtual void execute(Loader &l, const Statement &st) const
52 if(st.args.size()!=0) throw TypeError("Wrong number of arguments");
53 (dynamic_cast<L &>(l).*func)();
56 virtual std::string get_signature() const
57 { return std::string(); }
62 Loads a statement by calling a function that takes one argument.
64 template<typename L, typename A0>
65 class LoaderFunc1: public LoaderAction
68 typedef void (L::*FuncType)(A0);
73 LoaderFunc1(FuncType f): func(f) { }
75 virtual void execute(Loader &l, const Statement &st) const
77 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
78 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>());
81 virtual std::string get_signature() const
82 { return std::string(1, TypeInfo<A0>::signature); }
87 Loads a statement by calling a function that takes an array of values.
89 template<typename L, typename A0>
90 class LoaderFunc1<L, const std::vector<A0> &>: public LoaderAction
93 typedef void (L::*FuncType)(const std::vector<A0> &);
98 LoaderFunc1(FuncType f): func(f) { }
100 virtual void execute(Loader &l, const Statement &st) const
102 std::vector<A0> values;
103 values.reserve(st.args.size());
104 for(Statement::Arguments::const_iterator i=st.args.begin(); i!=st.args.end(); ++i)
105 values.push_back(i->get<A0>());
106 (dynamic_cast<L &>(l).*func)(values);
109 virtual std::string get_signature() const
112 result += TypeInfo<A0>::signature;
120 Loads a statement by calling a function with the statement itself as argument.
123 class LoaderFunc1<L, const Statement &>: public LoaderAction
126 typedef void (L::*FuncType)(const Statement &);
131 LoaderFunc1(FuncType f): func(f) { }
133 virtual void execute(Loader &l, const Statement &st) const
135 (dynamic_cast<L &>(l).*func)(st);
138 virtual std::string get_signature() const
143 template<typename L, typename A0, typename A1>
144 class LoaderFunc2: public LoaderAction
147 typedef void (L::*FuncType)(A0, A1);
152 LoaderFunc2(FuncType f): func(f) { }
154 virtual void execute(Loader &l, const Statement &st) const
156 if(st.args.size()!=2) throw TypeError("Wrong number of arguments");
157 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>());
160 virtual std::string get_signature() const
163 result += TypeInfo<A0>::signature;
164 result += TypeInfo<A1>::signature;
170 template<typename L, typename A0, typename A1, typename A2>
171 class LoaderFunc3: public LoaderAction
174 typedef void (L::*FuncType)(A0, A1, A2);
179 LoaderFunc3(FuncType f): func(f) { }
181 virtual void execute(Loader &l, const Statement &st) const
183 if(st.args.size()!=3) throw TypeError("Wrong number of arguments");
184 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>());
187 virtual std::string get_signature() const
190 result += TypeInfo<A0>::signature;
191 result += TypeInfo<A1>::signature;
192 result += TypeInfo<A2>::signature;
198 template<typename L, typename A0, typename A1, typename A2, typename A3>
199 class LoaderFunc4: public LoaderAction
202 typedef void (L::*FuncType)(A0, A1, A2, A3);
207 LoaderFunc4(FuncType f): func(f) { }
209 virtual void execute(Loader &l, const Statement &st) const
211 if(st.args.size()!=4) throw TypeError("Wrong number of arguments");
212 (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>());
215 virtual std::string get_signature() const
218 result += TypeInfo<A0>::signature;
219 result += TypeInfo<A1>::signature;
220 result += TypeInfo<A2>::signature;
221 result += TypeInfo<A3>::signature;
227 template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
228 class LoaderFunc5: public LoaderAction
231 typedef void (L::*FuncType)(A0, A1, A2, A3, A4);
236 LoaderFunc5(FuncType f): func(f) { }
238 virtual void execute(Loader &l, const Statement &st) const
240 if(st.args.size()!=5) throw TypeError("Wrong number of arguments");
241 (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>());
244 virtual std::string get_signature() const
247 result += TypeInfo<A0>::signature;
248 result += TypeInfo<A1>::signature;
249 result += TypeInfo<A2>::signature;
250 result += TypeInfo<A3>::signature;
251 result += TypeInfo<A4>::signature;
257 template<typename L, typename T0>
258 class LoadValue1: public LoaderAction
261 typedef T0 L::*Pointer0Type;
266 LoadValue1(Pointer0Type p0): ptr0(p0) { }
268 virtual void execute(Loader &l, const Statement &st) const
270 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
271 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
274 virtual std::string get_signature() const
275 { return std::string(1, TypeInfo<T0>::signature); }
279 template<typename L, typename T0>
280 class LoadValue1<L, T0 *>: public LoaderAction
283 typedef T0 *L::*Pointer0Type;
288 LoadValue1(Pointer0Type p0): ptr0(p0) { }
290 virtual void execute(Loader &l, const Statement &st) const
292 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
293 typename L::Loader &ldr=dynamic_cast<typename L::Loader &>(l);
294 ldr.get_object().*ptr0=ldr.get_collection().template get<T0>(st.args[0].get<std::string>());
297 virtual std::string get_signature() const
298 { return std::string(1, TypeInfo<std::string>::signature); }
302 template<typename L, typename T0, typename T1>
303 class LoadValue2: public LoaderAction
306 typedef T0 L::*Pointer0Type;
307 typedef T1 L::*Pointer1Type;
313 LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
315 virtual void execute(Loader &l, const Statement &st) const
317 if(st.args.size()!=2) throw TypeError("Wrong number of arguments");
318 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
319 dynamic_cast<typename L::Loader &>(l).get_object().*ptr1=st.args[1].get<T1>();
322 virtual std::string get_signature() const
325 result += TypeInfo<T0>::signature;
326 result += TypeInfo<T1>::signature;
331 } // namespace DataFile