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 &) const
52 (dynamic_cast<L &>(l).*func)();
55 virtual std::string get_signature() const
56 { return std::string(); }
61 Loads a statement by calling a function that takes one argument.
63 template<typename L, typename A0>
64 class LoaderFunc1: public LoaderAction
67 typedef void (L::*FuncType)(A0);
72 LoaderFunc1(FuncType f): func(f) { }
74 virtual void execute(Loader &l, const Statement &st) const
76 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>());
79 virtual std::string get_signature() const
80 { return std::string(1, TypeInfo<A0>::signature); }
85 Loads a statement by calling a function that takes an array of values.
87 template<typename L, typename A0>
88 class LoaderFunc1<L, const std::vector<A0> &>: public LoaderAction
91 typedef void (L::*FuncType)(const std::vector<A0> &);
96 LoaderFunc1(FuncType f): func(f) { }
98 virtual void execute(Loader &l, const Statement &st) const
100 std::vector<A0> values;
101 values.reserve(st.args.size());
102 for(Statement::Arguments::const_iterator i=st.args.begin(); i!=st.args.end(); ++i)
103 values.push_back(i->get<A0>());
104 (dynamic_cast<L &>(l).*func)(values);
107 virtual std::string get_signature() const
110 result += TypeInfo<A0>::signature;
118 Loads a statement by calling a function with the statement itself as argument.
121 class LoaderFunc1<L, const Statement &>: public LoaderAction
124 typedef void (L::*FuncType)(const Statement &);
129 LoaderFunc1(FuncType f): func(f) { }
131 virtual void execute(Loader &l, const Statement &st) const
133 (dynamic_cast<L &>(l).*func)(st);
136 virtual std::string get_signature() const
141 template<typename L, typename A0, typename A1>
142 class LoaderFunc2: public LoaderAction
145 typedef void (L::*FuncType)(A0, A1);
150 LoaderFunc2(FuncType f): func(f) { }
152 virtual void execute(Loader &l, const Statement &st) const
154 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>());
157 virtual std::string get_signature() const
160 result += TypeInfo<A0>::signature;
161 result += TypeInfo<A1>::signature;
167 template<typename L, typename A0, typename A1, typename A2>
168 class LoaderFunc3: public LoaderAction
171 typedef void (L::*FuncType)(A0, A1, A2);
176 LoaderFunc3(FuncType f): func(f) { }
178 virtual void execute(Loader &l, const Statement &st) const
180 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>());
183 virtual std::string get_signature() const
186 result += TypeInfo<A0>::signature;
187 result += TypeInfo<A1>::signature;
188 result += TypeInfo<A2>::signature;
194 template<typename L, typename A0, typename A1, typename A2, typename A3>
195 class LoaderFunc4: public LoaderAction
198 typedef void (L::*FuncType)(A0, A1, A2, A3);
203 LoaderFunc4(FuncType f): func(f) { }
205 virtual void execute(Loader &l, const Statement &st) const
207 (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>());
210 virtual std::string get_signature() const
213 result += TypeInfo<A0>::signature;
214 result += TypeInfo<A1>::signature;
215 result += TypeInfo<A2>::signature;
216 result += TypeInfo<A3>::signature;
222 template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
223 class LoaderFunc5: public LoaderAction
226 typedef void (L::*FuncType)(A0, A1, A2, A3, A4);
231 LoaderFunc5(FuncType f): func(f) { }
233 virtual void execute(Loader &l, const Statement &st) const
235 (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>());
238 virtual std::string get_signature() const
241 result += TypeInfo<A0>::signature;
242 result += TypeInfo<A1>::signature;
243 result += TypeInfo<A2>::signature;
244 result += TypeInfo<A3>::signature;
245 result += TypeInfo<A4>::signature;
251 template<typename L, typename T0>
252 class LoadValue1: public LoaderAction
255 typedef T0 L::*Pointer0Type;
260 LoadValue1(Pointer0Type p0): ptr0(p0) { }
262 virtual void execute(Loader &l, const Statement &st) const
264 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0 = st.args[0].get<T0>();
267 virtual std::string get_signature() const
268 { return std::string(1, TypeInfo<T0>::signature); }
272 template<typename L, typename T0>
273 class LoadValue1<L, T0 *>: public LoaderAction
276 typedef T0 *L::*Pointer0Type;
281 LoadValue1(Pointer0Type p0): ptr0(p0) { }
283 virtual void execute(Loader &l, const Statement &st) const
285 typename L::Loader &ldr = dynamic_cast<typename L::Loader &>(l);
286 if(!ldr.is_pointer_reload_allowed() && ldr.get_object().*ptr0)
287 throw InvalidState("The pointer has already been loaded");
288 ldr.get_object().*ptr0 = ldr.get_collection().template get<T0>(st.args[0].get<std::string>());
291 virtual std::string get_signature() const
292 { return std::string(1, TypeInfo<std::string>::signature); }
296 template<typename L, typename T0, typename T1>
297 class LoadValue2: public LoaderAction
300 typedef T0 L::*Pointer0Type;
301 typedef T1 L::*Pointer1Type;
307 LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
309 virtual void execute(Loader &l, const Statement &st) const
311 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0 = st.args[0].get<T0>();
312 dynamic_cast<typename L::Loader &>(l).get_object().*ptr1 = st.args[1].get<T1>();
315 virtual std::string get_signature() const
318 result += TypeInfo<T0>::signature;
319 result += TypeInfo<T1>::signature;
324 } // namespace DataFile