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;
35 Loads a statement by calling a function that takes no arguments.
38 class LoaderFunc0: public LoaderAction
41 typedef void (L::*FuncType)();
46 LoaderFunc0(FuncType f): func(f) { }
48 virtual void execute(Loader &l, const Statement &st) const
50 if(st.args.size()!=0) throw TypeError("Wrong number of arguments");
51 (dynamic_cast<L &>(l).*func)();
57 Loads a statement by calling a function that takes one argument.
59 template<typename L, typename A0>
60 class LoaderFunc1: public LoaderAction
63 typedef void (L::*FuncType)(A0);
68 LoaderFunc1(FuncType f): func(f) { }
70 virtual void execute(Loader &l, const Statement &st) const
72 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
73 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>());
79 Loads a statement by calling a function that takes an array of values.
81 template<typename L, typename A0>
82 class LoaderFunc1<L, const std::vector<A0> &>: public LoaderAction
85 typedef void (L::*FuncType)(const std::vector<A0> &);
90 LoaderFunc1(FuncType f): func(f) { }
92 virtual void execute(Loader &l, const Statement &st) const
94 std::vector<A0> values;
95 values.reserve(st.args.size());
96 for(Statement::Arguments::const_iterator i=st.args.begin(); i!=st.args.end(); ++i)
97 values.push_back(i->get<A0>());
98 (dynamic_cast<L &>(l).*func)(values);
104 Loads a statement by calling a function with the statement itself as argument.
107 class LoaderFunc1<L, const Statement &>: public LoaderAction
110 typedef void (L::*FuncType)(const Statement &);
115 LoaderFunc1(FuncType f): func(f) { }
117 virtual void execute(Loader &l, const Statement &st) const
119 (dynamic_cast<L &>(l).*func)(st);
124 template<typename L, typename A0, typename A1>
125 class LoaderFunc2: public LoaderAction
128 typedef void (L::*FuncType)(A0, A1);
133 LoaderFunc2(FuncType f): func(f) { }
135 virtual void execute(Loader &l, const Statement &st) const
137 if(st.args.size()!=2) throw TypeError("Wrong number of arguments");
138 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>());
143 template<typename L, typename A0, typename A1, typename A2>
144 class LoaderFunc3: public LoaderAction
147 typedef void (L::*FuncType)(A0, A1, A2);
152 LoaderFunc3(FuncType f): func(f) { }
154 virtual void execute(Loader &l, const Statement &st) const
156 if(st.args.size()!=3) throw TypeError("Wrong number of arguments");
157 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>());
162 template<typename L, typename A0, typename A1, typename A2, typename A3>
163 class LoaderFunc4: public LoaderAction
166 typedef void (L::*FuncType)(A0, A1, A2, A3);
171 LoaderFunc4(FuncType f): func(f) { }
173 virtual void execute(Loader &l, const Statement &st) const
175 if(st.args.size()!=4) throw TypeError("Wrong number of arguments");
176 (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>());
181 template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
182 class LoaderFunc5: public LoaderAction
185 typedef void (L::*FuncType)(A0, A1, A2, A3, A4);
190 LoaderFunc5(FuncType f): func(f) { }
192 virtual void execute(Loader &l, const Statement &st) const
194 if(st.args.size()!=5) throw TypeError("Wrong number of arguments");
195 (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>());
200 template<typename L, typename T0>
201 class LoadValue1: public LoaderAction
204 typedef T0 L::*Pointer0Type;
209 LoadValue1(Pointer0Type p0): ptr0(p0) { }
211 virtual void execute(Loader &l, const Statement &st) const
213 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
214 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
219 template<typename L, typename T0>
220 class LoadValue1<L, T0 *>: public LoaderAction
223 typedef T0 *L::*Pointer0Type;
228 LoadValue1(Pointer0Type p0): ptr0(p0) { }
230 virtual void execute(Loader &l, const Statement &st) const
232 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
233 typename L::Loader &ldr=dynamic_cast<typename L::Loader &>(l);
234 ldr.get_object().*ptr0=ldr.get_collection().template get<T0>(st.args[0].get<std::string>());
239 template<typename L, typename T0, typename T1>
240 class LoadValue2: public LoaderAction
243 typedef T0 L::*Pointer0Type;
244 typedef T1 L::*Pointer1Type;
250 LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
252 virtual void execute(Loader &l, const Statement &st) const
254 if(st.args.size()!=2) throw TypeError("Wrong number of arguments");
255 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
256 dynamic_cast<typename L::Loader &>(l).get_object().*ptr1=st.args[1].get<T1>();
260 } // namespace DataFile