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.
26 Called when a statement is to be loaded.
28 virtual void execute(Loader &, const Statement &) const=0;
29 virtual ~LoaderAction() { }
36 Loads a statement by calling a function that takes no arguments.
39 class LoaderFunc0: public LoaderAction
42 typedef void (L::*FuncType)();
44 LoaderFunc0(FuncType f): func(f) { }
45 void execute(Loader &l, const Statement &st) const
47 if(st.args.size()!=0) throw TypeError("Wrong number of arguments");
48 (dynamic_cast<L &>(l).*func)();
56 Loads a statement by calling a function that takes one argument.
58 template<typename L, typename A0>
59 class LoaderFunc1: public LoaderAction
62 typedef void (L::*FuncType)(A0);
64 LoaderFunc1(FuncType f): func(f) { }
65 void execute(Loader &l, const Statement &st) const
67 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
68 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>());
76 Loads a statement by calling a function that takes an array of values.
78 template<typename L, typename A0>
79 class LoaderFunc1<L, const std::vector<A0> &>: public LoaderAction
82 typedef void (L::*FuncType)(const std::vector<A0> &);
84 LoaderFunc1(FuncType f): func(f) { }
85 void execute(Loader &l, const Statement &st) const
87 std::vector<A0> values;
88 values.reserve(st.args.size());
89 for(Statement::Arguments::const_iterator i=st.args.begin(); i!=st.args.end(); ++i)
90 values.push_back(i->get<A0>());
91 (dynamic_cast<L &>(l).*func)(values);
99 Loads a statement by calling a function with the statement itself as argument.
102 class LoaderFunc1<L, const Statement &>: public LoaderAction
105 typedef void (L::*FuncType)(const Statement &);
107 LoaderFunc1(FuncType f): func(f) { }
108 void execute(Loader &l, const Statement &st) const
110 (dynamic_cast<L &>(l).*func)(st);
117 template<typename L, typename A0, typename A1>
118 class LoaderFunc2: public LoaderAction
121 typedef void (L::*FuncType)(A0, A1);
123 LoaderFunc2(FuncType f): func(f) { }
124 void execute(Loader &l, const Statement &st) const
126 if(st.args.size()!=2) throw TypeError("Wrong number of arguments");
127 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>());
134 template<typename L, typename A0, typename A1, typename A2>
135 class LoaderFunc3: public LoaderAction
138 typedef void (L::*FuncType)(A0, A1, A2);
140 LoaderFunc3(FuncType f): func(f) { }
141 void execute(Loader &l, const Statement &st) const
143 if(st.args.size()!=3) throw TypeError("Wrong number of arguments");
144 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>());
151 template<typename L, typename A0, typename A1, typename A2, typename A3>
152 class LoaderFunc4: public LoaderAction
155 typedef void (L::*FuncType)(A0, A1, A2, A3);
157 LoaderFunc4(FuncType f): func(f) { }
158 void execute(Loader &l, const Statement &st) const
160 if(st.args.size()!=4) throw TypeError("Wrong number of arguments");
161 (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>());
168 template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
169 class LoaderFunc5: public LoaderAction
172 typedef void (L::*FuncType)(A0, A1, A2, A3, A4);
174 LoaderFunc5(FuncType f): func(f) { }
175 void execute(Loader &l, const Statement &st) const
177 if(st.args.size()!=5) throw TypeError("Wrong number of arguments");
178 (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>());
185 template<typename L, typename T0>
186 class LoadValue1: public LoaderAction
189 typedef T0 L::*Pointer0Type;
191 LoadValue1(Pointer0Type p0): ptr0(p0) { }
192 void execute(Loader &l, const Statement &st) const
194 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
195 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
202 template<typename L, typename T0>
203 class LoadValue1<L, T0 *>: public LoaderAction
206 typedef T0 *L::*Pointer0Type;
208 LoadValue1(Pointer0Type p0): ptr0(p0) { }
209 void execute(Loader &l, const Statement &st) const
211 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
212 typename L::Loader &ldr=dynamic_cast<typename L::Loader &>(l);
213 ldr.get_object().*ptr0=ldr.get_collection().template get<T0>(st.args[0].get<std::string>());
220 template<typename L, typename T0, typename T1>
221 class LoadValue2: public LoaderAction
224 typedef T0 L::*Pointer0Type;
225 typedef T1 L::*Pointer1Type;
227 LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
228 void execute(Loader &l, const Statement &st) const
230 if(st.args.size()!=2) throw TypeError("Wrong number of arguments");
231 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
232 dynamic_cast<typename L::Loader &>(l).get_object().*ptr1=st.args[1].get<T1>();
239 } // namespace DataFile