]> git.tdb.fi Git - libs/datafile.git/blob - source/loaderaction.h
Some more code reformatting
[libs/datafile.git] / source / loaderaction.h
1 /* $Id$
2
3 This file is part of libmspdatafile
4 Copyright © 2008, 2010  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 protected:
25         LoaderAction() { }
26 public:
27         virtual ~LoaderAction() { }
28
29         /** Called to process a statement. */
30         virtual void execute(Loader &, const Statement &) const = 0;
31 };
32
33
34 /**
35 Loads a statement by calling a function that takes no arguments.
36 */
37 template<typename L>
38 class LoaderFunc0: public LoaderAction
39 {
40 private:
41         typedef void (L::*FuncType)();
42
43         FuncType func;
44
45 public:
46         LoaderFunc0(FuncType f): func(f) { }
47
48         virtual void execute(Loader &l, const Statement &st) const
49         {
50                 if(st.args.size()!=0) throw TypeError("Wrong number of arguments");
51                 (dynamic_cast<L &>(l).*func)();
52         };
53 };
54
55
56 /**
57 Loads a statement by calling a function that takes one argument.
58 */
59 template<typename L, typename A0>
60 class LoaderFunc1: public LoaderAction
61 {
62 private:
63         typedef void (L::*FuncType)(A0);
64
65         FuncType func;
66
67 public:
68         LoaderFunc1(FuncType f): func(f) { }
69
70         virtual void execute(Loader &l, const Statement &st) const
71         {
72                 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
73                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>());
74         }
75 };
76
77
78 /**
79 Loads a statement by calling a function that takes an array of values.
80 */
81 template<typename L, typename A0>
82 class LoaderFunc1<L, const std::vector<A0> &>: public LoaderAction
83 {
84 private:
85         typedef void (L::*FuncType)(const std::vector<A0> &);
86
87         FuncType func;
88
89 public:
90         LoaderFunc1(FuncType f): func(f) { }
91
92         virtual void execute(Loader &l, const Statement &st) const
93         {
94                 std::vector<A0> values;
95                 values.reserve(st.args.size());
96                 for(Statement::Arguments::const_iterator i=st.args.begin(); i!=st.args.end(); ++i)
97                         values.push_back(i->get<A0>());
98                 (dynamic_cast<L &>(l).*func)(values);
99         }
100 };
101
102
103 /**
104 Loads a statement by calling a function with the statement itself as argument.
105 */
106 template<typename L>
107 class LoaderFunc1<L, const Statement &>: public LoaderAction
108 {
109 private:
110         typedef void (L::*FuncType)(const Statement &);
111
112         FuncType func;
113
114 public:
115         LoaderFunc1(FuncType f): func(f) { }
116
117         virtual void execute(Loader &l, const Statement &st) const
118         {
119                 (dynamic_cast<L &>(l).*func)(st);
120         }
121 };
122
123
124 template<typename L, typename A0, typename A1>
125 class LoaderFunc2: public LoaderAction
126 {
127 private:
128         typedef void (L::*FuncType)(A0, A1);
129
130         FuncType func;
131
132 public:
133         LoaderFunc2(FuncType f): func(f) { }
134
135         virtual void execute(Loader &l, const Statement &st) const
136         {
137                 if(st.args.size()!=2) throw TypeError("Wrong number of arguments");
138                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>());
139         }
140 };
141
142
143 template<typename L, typename A0, typename A1, typename A2>
144 class LoaderFunc3: public LoaderAction
145 {
146 private:
147         typedef void (L::*FuncType)(A0, A1, A2);
148
149         FuncType func;
150
151 public:
152         LoaderFunc3(FuncType f): func(f) { }
153
154         virtual void execute(Loader &l, const Statement &st) const
155         {
156                 if(st.args.size()!=3) throw TypeError("Wrong number of arguments");
157                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>());
158         }
159 };
160
161
162 template<typename L, typename A0, typename A1, typename A2, typename A3>
163 class LoaderFunc4: public LoaderAction
164 {
165 private:
166         typedef void (L::*FuncType)(A0, A1, A2, A3);
167
168         FuncType func;
169
170 public:
171         LoaderFunc4(FuncType f): func(f) { }
172
173         virtual void execute(Loader &l, const Statement &st) const
174         {
175                 if(st.args.size()!=4) throw TypeError("Wrong number of arguments");
176                 (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>());
177         }
178 };
179
180
181 template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
182 class LoaderFunc5: public LoaderAction
183 {
184 private:
185         typedef void (L::*FuncType)(A0, A1, A2, A3, A4);
186
187         FuncType func;
188
189 public:
190         LoaderFunc5(FuncType f): func(f) { }
191
192         virtual void execute(Loader &l, const Statement &st) const
193         {
194                 if(st.args.size()!=5) throw TypeError("Wrong number of arguments");
195                 (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>());
196         }
197 };
198
199
200 template<typename L, typename T0>
201 class LoadValue1: public LoaderAction
202 {
203 private:
204         typedef T0 L::*Pointer0Type;
205
206         Pointer0Type ptr0;
207
208 public:
209         LoadValue1(Pointer0Type p0): ptr0(p0) { }
210
211         virtual void execute(Loader &l, const Statement &st) const
212         {
213                 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
214                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
215         }
216 };
217
218
219 template<typename L, typename T0>
220 class LoadValue1<L, T0 *>: public LoaderAction
221 {
222 private:
223         typedef T0 *L::*Pointer0Type;
224
225         Pointer0Type ptr0;
226
227 public:
228         LoadValue1(Pointer0Type p0): ptr0(p0) { }
229
230         virtual void execute(Loader &l, const Statement &st) const
231         {
232                 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
233                 typename L::Loader &ldr=dynamic_cast<typename L::Loader &>(l);
234                 ldr.get_object().*ptr0=ldr.get_collection().template get<T0>(st.args[0].get<std::string>());
235         }
236 };
237
238
239 template<typename L, typename T0, typename T1>
240 class LoadValue2: public LoaderAction
241 {
242 private:
243         typedef T0 L::*Pointer0Type;
244         typedef T1 L::*Pointer1Type;
245
246         Pointer0Type ptr0;
247         Pointer1Type ptr1;
248
249 public:
250         LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
251
252         virtual void execute(Loader &l, const Statement &st) const
253         {
254                 if(st.args.size()!=2) throw TypeError("Wrong number of arguments");
255                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
256                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr1=st.args[1].get<T1>();
257         }
258 };
259
260 } // namespace DataFile
261 } // namespace Msp
262
263 #endif