3 This file is part of libmspdatafile
4 Copyright © 2006-2008 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(ValueArray::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);
98 template<typename L, typename A0, typename A1>
99 class LoaderFunc2: public LoaderAction
102 typedef void (L::*FuncType)(A0, A1);
104 LoaderFunc2(FuncType f): func(f) { }
105 void execute(Loader &l, const Statement &st) const
107 if(st.args.size()!=2) throw TypeError("Wrong number of arguments");
108 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>());
115 template<typename L, typename A0, typename A1, typename A2>
116 class LoaderFunc3: public LoaderAction
119 typedef void (L::*FuncType)(A0, A1, A2);
121 LoaderFunc3(FuncType f): func(f) { }
122 void execute(Loader &l, const Statement &st) const
124 if(st.args.size()!=3) throw TypeError("Wrong number of arguments");
125 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>());
132 template<typename L, typename A0, typename A1, typename A2, typename A3>
133 class LoaderFunc4: public LoaderAction
136 typedef void (L::*FuncType)(A0, A1, A2, A3);
138 LoaderFunc4(FuncType f): func(f) { }
139 void execute(Loader &l, const Statement &st) const
141 if(st.args.size()!=4) throw TypeError("Wrong number of arguments");
142 (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>());
149 template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
150 class LoaderFunc5: public LoaderAction
153 typedef void (L::*FuncType)(A0, A1, A2, A3, A4);
155 LoaderFunc5(FuncType f): func(f) { }
156 void execute(Loader &l, const Statement &st) const
158 if(st.args.size()!=5) throw TypeError("Wrong number of arguments");
159 (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>());
166 template<typename L, typename T0>
167 class LoadValue1: public LoaderAction
170 typedef T0 L::*Pointer0Type;
172 LoadValue1(Pointer0Type p0): ptr0(p0) { }
173 void execute(Loader &l, const Statement &st) const
175 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
176 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
183 template<typename L, typename T0>
184 class LoadValue1<L, T0 *>: public LoaderAction
187 typedef T0 *L::*Pointer0Type;
189 LoadValue1(Pointer0Type p0): ptr0(p0) { }
190 void execute(Loader &l, const Statement &st) const
192 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
193 typename L::Loader &ldr=dynamic_cast<typename L::Loader &>(l);
194 ldr.get_object().*ptr0=ldr.get_collection().template get<T0>(st.args[0].get<std::string>());
201 template<typename L, typename T0, typename T1>
202 class LoadValue2: public LoaderAction
205 typedef T0 L::*Pointer0Type;
206 typedef T1 L::*Pointer1Type;
208 LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
209 void execute(Loader &l, const Statement &st) const
211 if(st.args.size()!=2) throw TypeError("Wrong number of arguments");
212 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
213 dynamic_cast<typename L::Loader &>(l).get_object().*ptr1=st.args[1].get<T1>();
220 } // namespace DataFile