]> git.tdb.fi Git - libs/datafile.git/blob - source/loaderaction.h
ff478ec2df7aa8ed651914922516781e3f519278
[libs/datafile.git] / source / loaderaction.h
1 /* $Id$
2
3 This file is part of libmspdatafile
4 Copyright © 2006-2008  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_DATAFILE_LOADERACTION_H_
9 #define MSP_DATAFILE_LOADERACTION_H_
10
11 #include "except.h"
12 #include "statement.h"
13
14 namespace Msp {
15 namespace DataFile {
16
17 class Loader;
18
19 /**
20 Base class for loader actions.
21 */
22 class LoaderAction
23 {
24 public:
25         /**
26         Called when a statement is to be loaded.
27         */
28         virtual void execute(Loader &, const Statement &) const=0;
29         virtual ~LoaderAction() { }
30 protected:
31         LoaderAction() { }
32 };
33
34
35 /**
36 Loads a statement by calling a function that takes no arguments.
37 */
38 template<typename L>
39 class LoaderFunc0: public LoaderAction
40 {
41 public:
42         typedef void (L::*FuncType)();
43
44         LoaderFunc0(FuncType f): func(f) { }
45         void execute(Loader &l, const Statement &st) const
46         {
47                 if(st.args.size()!=0) throw TypeError("Wrong number of arguments");
48                 (dynamic_cast<L &>(l).*func)();
49         };
50 private:
51         FuncType func;
52 };
53
54
55 /**
56 Loads a statement by calling a function that takes one argument.
57 */
58 template<typename L, typename A0>
59 class LoaderFunc1: public LoaderAction
60 {
61 public:
62         typedef void (L::*FuncType)(A0);
63
64         LoaderFunc1(FuncType f): func(f) { }
65         void execute(Loader &l, const Statement &st) const
66         {
67                 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
68                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>());
69         }
70 private:
71         FuncType func;
72 };
73
74
75 /**
76 Loads a statement by calling a function that takes an array of values.
77 */
78 template<typename L, typename A0>
79 class LoaderFunc1<L, const std::vector<A0> &>: public LoaderAction
80 {
81 public:
82         typedef void (L::*FuncType)(const std::vector<A0> &);
83
84         LoaderFunc1(FuncType f): func(f) { }
85         void execute(Loader &l, const Statement &st) const
86         {
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);
92         }
93 private:
94         FuncType func;
95 };
96
97
98 /**
99 Loads a statement by calling a function with the statement itself as argument.
100 */
101 template<typename L>
102 class LoaderFunc1<L, const Statement &>: public LoaderAction
103 {
104 public:
105         typedef void (L::*FuncType)(const Statement &);
106
107         LoaderFunc1(FuncType f): func(f) { }
108         void execute(Loader &l, const Statement &st) const
109         {
110                 (dynamic_cast<L &>(l).*func)(st);
111         }
112 private:
113         FuncType func;
114 };
115
116
117 template<typename L, typename A0, typename A1>
118 class LoaderFunc2: public LoaderAction
119 {
120 public:
121         typedef void (L::*FuncType)(A0, A1);
122
123         LoaderFunc2(FuncType f): func(f) { }
124         void execute(Loader &l, const Statement &st) const
125         {
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>());
128         }
129 private:
130         FuncType func;
131 };
132
133
134 template<typename L, typename A0, typename A1, typename A2>
135 class LoaderFunc3: public LoaderAction
136 {
137 public:
138         typedef void (L::*FuncType)(A0, A1, A2);
139
140         LoaderFunc3(FuncType f): func(f) { }
141         void execute(Loader &l, const Statement &st) const
142         {
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>());
145         }
146 private:
147         FuncType func;
148 };
149
150
151 template<typename L, typename A0, typename A1, typename A2, typename A3>
152 class LoaderFunc4: public LoaderAction
153 {
154 public:
155         typedef void (L::*FuncType)(A0, A1, A2, A3);
156
157         LoaderFunc4(FuncType f): func(f) { }
158         void execute(Loader &l, const Statement &st) const
159         {
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>());
162         }
163 private:
164         FuncType func;
165 };
166
167
168 template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
169 class LoaderFunc5: public LoaderAction
170 {
171 public:
172         typedef void (L::*FuncType)(A0, A1, A2, A3, A4);
173
174         LoaderFunc5(FuncType f): func(f) { }
175         void execute(Loader &l, const Statement &st) const
176         {
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>());
179         }
180 private:
181         FuncType func;
182 };
183
184
185 template<typename L, typename T0>
186 class LoadValue1: public LoaderAction
187 {
188 public:
189         typedef T0 L::*Pointer0Type;
190
191         LoadValue1(Pointer0Type p0): ptr0(p0) { }
192         void execute(Loader &l, const Statement &st) const
193         {
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>();
196         }
197 private:
198         Pointer0Type ptr0;
199 };
200
201
202 template<typename L, typename T0>
203 class LoadValue1<L, T0 *>: public LoaderAction
204 {
205 public:
206         typedef T0 *L::*Pointer0Type;
207
208         LoadValue1(Pointer0Type p0): ptr0(p0) { }
209         void execute(Loader &l, const Statement &st) const
210         {
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>());
214         }
215 private:
216         Pointer0Type ptr0;
217 };
218
219
220 template<typename L, typename T0, typename T1>
221 class LoadValue2: public LoaderAction
222 {
223 public:
224         typedef T0 L::*Pointer0Type;
225         typedef T1 L::*Pointer1Type;
226
227         LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
228         void execute(Loader &l, const Statement &st) const
229         {
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>();
233         }
234 private:
235         Pointer0Type ptr0;
236         Pointer1Type ptr1;
237 };
238
239 } // namespace DataFile
240 } // namespace Msp
241
242 #endif