1 #ifndef MSP_DATAFILE_LOADERACTION_H_
2 #define MSP_DATAFILE_LOADERACTION_H_
4 #include <msp/core/meta.h>
5 #include "argumentstore.h"
12 std::string create_signature(const std::string &prefix = std::string())
13 { return prefix+std::string(1, TypeInfo<T>::signature); }
15 template<typename T0, typename T1, typename... Tail>
16 std::string create_signature(const std::string &prefix = std::string())
17 { return create_signature<T1, Tail...>(prefix+std::string(1, TypeInfo<T0>::signature)); }
22 Base class for loader actions.
29 virtual ~LoaderAction() { }
31 /** Called to process a statement. */
32 virtual void execute(Loader &, const Statement &) const = 0;
34 virtual void execute(Loader &, const ArgumentStore &) const = 0;
36 virtual std::string get_signature() const = 0;
41 Loads a statement by calling a function that takes no arguments.
44 class LoaderFunc0: public LoaderAction
47 typedef void (L::*FuncType)();
52 LoaderFunc0(FuncType f): func(f) { }
54 virtual void execute(Loader &l, const Statement &) const
56 (dynamic_cast<L &>(l).*func)();
59 virtual void execute(Loader &l, const ArgumentStore &) const
61 (dynamic_cast<L &>(l).*func)();
64 virtual std::string get_signature() const
65 { return std::string(); }
70 Loads a statement by calling a function that takes one argument.
72 template<typename L, typename A0>
73 class LoaderFunc1: public LoaderAction
76 typedef void (L::*FuncType)(A0);
81 LoaderFunc1(FuncType f): func(f) { }
83 virtual void execute(Loader &l, const Statement &st) const
85 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>());
88 virtual void execute(Loader &l, const ArgumentStore &as) const
90 (dynamic_cast<L &>(l).*func)(as.get<A0>(0));
93 virtual std::string get_signature() const
94 { return std::string(1, TypeInfo<A0>::signature); }
99 Loads a statement by calling a function that takes an array of values.
101 template<typename L, typename A0>
102 class LoaderFunc1<L, const std::vector<A0> &>: public LoaderAction
105 typedef void (L::*FuncType)(const std::vector<A0> &);
110 LoaderFunc1(FuncType f): func(f) { }
112 virtual void execute(Loader &l, const Statement &st) const
114 std::vector<A0> values;
115 values.reserve(st.args.size());
116 for(const Value &a: st.args)
117 values.push_back(a.get<A0>());
118 (dynamic_cast<L &>(l).*func)(values);
121 virtual void execute(Loader &l, const ArgumentStore &as) const
123 std::vector<A0> values;
124 unsigned n_args = as.get_info().key.signature.size();
125 values.reserve(n_args);
126 for(unsigned i=0; i<n_args; ++i)
127 values.push_back(as.get<A0>(i));
128 (dynamic_cast<L &>(l).*func)(values);
131 virtual std::string get_signature() const
134 result += TypeInfo<A0>::signature;
142 Loads a statement by calling a function with the statement itself as argument.
145 class LoaderFunc1<L, const Statement &>: public LoaderAction
148 typedef void (L::*FuncType)(const Statement &);
153 LoaderFunc1(FuncType f): func(f) { }
155 virtual void execute(Loader &l, const Statement &st) const
157 (dynamic_cast<L &>(l).*func)(st);
160 virtual void execute(Loader &, const ArgumentStore &) const
162 throw std::logic_error("incompatible format");
165 virtual std::string get_signature() const
170 template<unsigned I, typename... Args>
176 template<typename L, typename F, typename... Args>
177 static void apply(L &l, F func, const Statement &, Args... args)
182 template<typename L, typename F, typename... Args>
183 static void apply(L &l, F func, const ArgumentStore &, Args... args)
189 template<unsigned I, typename Head, typename... Tail>
190 struct Apply<I, Head, Tail...>
192 template<typename L, typename F, typename... Args>
193 static void apply(L &l, F func, const Statement &st, Args... args)
195 Apply<I+1, Tail...>::apply(l, func, st, args..., st.args[I].get<Head>());
198 template<typename L, typename F, typename... Args>
199 static void apply(L &l, F func, const ArgumentStore &as, Args... args)
201 Apply<I+1, Tail...>::apply(l, func, as, args..., as.get<Head>(I));
206 template<typename L, typename... Args>
207 class LoaderFuncN: public LoaderAction
210 typedef void (L::*FuncType)(Args...);
215 LoaderFuncN(FuncType f): func(f) { }
217 virtual void execute(Loader &l, const Statement &st) const
219 Apply<0, Args...>::apply(dynamic_cast<L &>(l), func, st);
222 virtual void execute(Loader &l, const ArgumentStore &as) const
224 Apply<0, Args...>::apply(dynamic_cast<L &>(l), func, as);
227 virtual std::string get_signature() const
228 { return create_signature<Args...>(); }
232 template<typename L, typename B0, typename... Args>
233 class LoaderFuncNBound1: public LoaderAction
236 typedef void (L::*FuncType)(B0, Args...);
237 typedef typename std::remove_reference<B0>::type Bound0Type;
243 LoaderFuncNBound1(FuncType f, const Bound0Type &b0): func(f), bound0(b0) { }
245 virtual void execute(Loader &l, const Statement &st) const
247 Apply<0, Args...>::apply(dynamic_cast<L &>(l), func, st, bound0);
250 virtual void execute(Loader &l, const ArgumentStore &as) const
252 Apply<0, Args...>::apply(dynamic_cast<L &>(l), func, as, bound0);
255 virtual std::string get_signature() const
256 { return create_signature<Args...>(); }
260 template<typename L, typename T0>
261 class LoadValue1: public LoaderAction
264 typedef T0 L::*Pointer0Type;
269 LoadValue1(Pointer0Type p0): ptr0(p0) { }
271 virtual void execute(Loader &l, const Statement &st) const
273 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0 = st.args[0].get<T0>();
276 virtual void execute(Loader &l, const ArgumentStore &as) const
278 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0 = as.get<T0>(0);
281 virtual std::string get_signature() const
282 { return std::string(1, TypeInfo<T0>::signature); }
286 template<typename L, typename T0>
287 class LoadValue1<L, T0 *>: public LoaderAction
290 typedef T0 *L::*Pointer0Type;
295 LoadValue1(Pointer0Type p0): ptr0(p0) { }
297 virtual void execute(Loader &l, const Statement &st) const
299 typename L::Loader &ldr = dynamic_cast<typename L::Loader &>(l);
300 ldr.get_object().*ptr0 = &ldr.get_collection().template get<T0>(st.args[0].get<std::string>());
303 virtual void execute(Loader &l, const ArgumentStore &as) const
305 typename L::Loader &ldr = dynamic_cast<typename L::Loader &>(l);
306 ldr.get_object().*ptr0 = &ldr.get_collection().template get<T0>(as.get<std::string>(0));
309 virtual std::string get_signature() const
310 { return std::string(1, TypeInfo<std::string>::signature); }
314 template<typename L, typename T0, typename T1>
315 class LoadValue2: public LoaderAction
318 typedef T0 L::*Pointer0Type;
319 typedef T1 L::*Pointer1Type;
325 LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
327 virtual void execute(Loader &l, const Statement &st) const
329 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0 = st.args[0].get<T0>();
330 dynamic_cast<typename L::Loader &>(l).get_object().*ptr1 = st.args[1].get<T1>();
333 virtual void execute(Loader &l, const ArgumentStore &as) const
335 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0 = as.get<T0>(0);
336 dynamic_cast<typename L::Loader &>(l).get_object().*ptr1 = as.get<T1>(1);
339 virtual std::string get_signature() const
342 result += TypeInfo<T0>::signature;
343 result += TypeInfo<T1>::signature;
348 } // namespace DataFile