1 #ifndef MSP_DATAFILE_LOADERACTION_H_
2 #define MSP_DATAFILE_LOADERACTION_H_
4 #include "argumentstore.h"
13 Base class for loader actions.
20 virtual ~LoaderAction() { }
22 /** Called to process a statement. */
23 virtual void execute(Loader &, const Statement &) const = 0;
25 virtual void execute(Loader &, const ArgumentStore &) const = 0;
27 virtual std::string get_signature() const = 0;
32 Loads a statement by calling a function that takes no arguments.
35 class LoaderFunc0: public LoaderAction
38 typedef void (L::*FuncType)();
43 LoaderFunc0(FuncType f): func(f) { }
45 virtual void execute(Loader &l, const Statement &) const
47 (dynamic_cast<L &>(l).*func)();
50 virtual void execute(Loader &l, const ArgumentStore &) 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 void execute(Loader &l, const ArgumentStore &as) const
81 (dynamic_cast<L &>(l).*func)(as.get<A0>(0));
84 virtual std::string get_signature() const
85 { return std::string(1, TypeInfo<A0>::signature); }
90 Loads a statement by calling a function that takes an array of values.
92 template<typename L, typename A0>
93 class LoaderFunc1<L, const std::vector<A0> &>: public LoaderAction
96 typedef void (L::*FuncType)(const std::vector<A0> &);
101 LoaderFunc1(FuncType f): func(f) { }
103 virtual void execute(Loader &l, const Statement &st) const
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);
112 virtual void execute(Loader &l, const ArgumentStore &as) const
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);
122 virtual std::string get_signature() const
125 result += TypeInfo<A0>::signature;
133 Loads a statement by calling a function with the statement itself as argument.
136 class LoaderFunc1<L, const Statement &>: public LoaderAction
139 typedef void (L::*FuncType)(const Statement &);
144 LoaderFunc1(FuncType f): func(f) { }
146 virtual void execute(Loader &l, const Statement &st) const
148 (dynamic_cast<L &>(l).*func)(st);
151 virtual void execute(Loader &, const ArgumentStore &) const
153 throw std::logic_error("incompatible format");
156 virtual std::string get_signature() const
161 template<typename L, typename A0, typename A1>
162 class LoaderFunc2: public LoaderAction
165 typedef void (L::*FuncType)(A0, A1);
170 LoaderFunc2(FuncType f): func(f) { }
172 virtual void execute(Loader &l, const Statement &st) const
174 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>());
177 virtual void execute(Loader &l, const ArgumentStore &as) const
179 (dynamic_cast<L &>(l).*func)(as.get<A0>(0), as.get<A1>(1));
182 virtual std::string get_signature() const
185 result += TypeInfo<A0>::signature;
186 result += TypeInfo<A1>::signature;
192 template<typename L, typename A0, typename A1, typename A2>
193 class LoaderFunc3: public LoaderAction
196 typedef void (L::*FuncType)(A0, A1, A2);
201 LoaderFunc3(FuncType f): func(f) { }
203 virtual void execute(Loader &l, const Statement &st) const
205 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>());
208 virtual void execute(Loader &l, const ArgumentStore &as) const
210 (dynamic_cast<L &>(l).*func)(as.get<A0>(0), as.get<A1>(1), as.get<A2>(2));
213 virtual std::string get_signature() const
216 result += TypeInfo<A0>::signature;
217 result += TypeInfo<A1>::signature;
218 result += TypeInfo<A2>::signature;
224 template<typename L, typename A0, typename A1, typename A2, typename A3>
225 class LoaderFunc4: public LoaderAction
228 typedef void (L::*FuncType)(A0, A1, A2, A3);
233 LoaderFunc4(FuncType f): func(f) { }
235 virtual void execute(Loader &l, const Statement &st) const
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>());
240 virtual void execute(Loader &l, const ArgumentStore &as) const
242 (dynamic_cast<L &>(l).*func)(as.get<A0>(0), as.get<A1>(1), as.get<A2>(2), as.get<A3>(3));
245 virtual std::string get_signature() const
248 result += TypeInfo<A0>::signature;
249 result += TypeInfo<A1>::signature;
250 result += TypeInfo<A2>::signature;
251 result += TypeInfo<A3>::signature;
257 template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
258 class LoaderFunc5: public LoaderAction
261 typedef void (L::*FuncType)(A0, A1, A2, A3, A4);
266 LoaderFunc5(FuncType f): func(f) { }
268 virtual void execute(Loader &l, const Statement &st) const
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>());
273 virtual void execute(Loader &l, const ArgumentStore &as) const
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));
278 virtual std::string get_signature() const
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;
291 template<typename L, typename T0>
292 class LoadValue1: public LoaderAction
295 typedef T0 L::*Pointer0Type;
300 LoadValue1(Pointer0Type p0): ptr0(p0) { }
302 virtual void execute(Loader &l, const Statement &st) const
304 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0 = st.args[0].get<T0>();
307 virtual void execute(Loader &l, const ArgumentStore &as) const
309 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0 = as.get<T0>(0);
312 virtual std::string get_signature() const
313 { return std::string(1, TypeInfo<T0>::signature); }
317 template<typename L, typename T0>
318 class LoadValue1<L, T0 *>: public LoaderAction
321 typedef T0 *L::*Pointer0Type;
326 LoadValue1(Pointer0Type p0): ptr0(p0) { }
328 virtual void execute(Loader &l, const Statement &st) const
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>());
334 virtual void execute(Loader &l, const ArgumentStore &as) const
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));
340 virtual std::string get_signature() const
341 { return std::string(1, TypeInfo<std::string>::signature); }
345 template<typename L, typename T0, typename T1>
346 class LoadValue2: public LoaderAction
349 typedef T0 L::*Pointer0Type;
350 typedef T1 L::*Pointer1Type;
356 LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
358 virtual void execute(Loader &l, const Statement &st) const
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>();
364 virtual void execute(Loader &l, const ArgumentStore &as) const
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);
370 virtual std::string get_signature() const
373 result += TypeInfo<T0>::signature;
374 result += TypeInfo<T1>::signature;
379 } // namespace DataFile