]> git.tdb.fi Git - libs/datafile.git/blob - source/loaderaction.h
Remove pointer reload prevention feature; the purpose it was added for is now gone
[libs/datafile.git] / source / loaderaction.h
1 #ifndef MSP_DATAFILE_LOADERACTION_H_
2 #define MSP_DATAFILE_LOADERACTION_H_
3
4 #include "except.h"
5 #include "statement.h"
6
7 namespace Msp {
8 namespace DataFile {
9
10 class Loader;
11
12 /**
13 Base class for loader actions.
14 */
15 class LoaderAction
16 {
17 protected:
18         LoaderAction() { }
19 public:
20         virtual ~LoaderAction() { }
21
22         /** Called to process a statement. */
23         virtual void execute(Loader &, const Statement &) const = 0;
24
25         virtual std::string get_signature() const = 0;
26 };
27
28
29 /**
30 Loads a statement by calling a function that takes no arguments.
31 */
32 template<typename L>
33 class LoaderFunc0: public LoaderAction
34 {
35 private:
36         typedef void (L::*FuncType)();
37
38         FuncType func;
39
40 public:
41         LoaderFunc0(FuncType f): func(f) { }
42
43         virtual void execute(Loader &l, const Statement &) const
44         {
45                 (dynamic_cast<L &>(l).*func)();
46         };
47
48         virtual std::string get_signature() const
49         { return std::string(); }
50 };
51
52
53 /**
54 Loads a statement by calling a function that takes one argument.
55 */
56 template<typename L, typename A0>
57 class LoaderFunc1: public LoaderAction
58 {
59 private:
60         typedef void (L::*FuncType)(A0);
61
62         FuncType func;
63
64 public:
65         LoaderFunc1(FuncType f): func(f) { }
66
67         virtual void execute(Loader &l, const Statement &st) const
68         {
69                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>());
70         }
71
72         virtual std::string get_signature() const
73         { return std::string(1, TypeInfo<A0>::signature); }
74 };
75
76
77 /**
78 Loads a statement by calling a function that takes an array of values.
79 */
80 template<typename L, typename A0>
81 class LoaderFunc1<L, const std::vector<A0> &>: public LoaderAction
82 {
83 private:
84         typedef void (L::*FuncType)(const std::vector<A0> &);
85
86         FuncType func;
87
88 public:
89         LoaderFunc1(FuncType f): func(f) { }
90
91         virtual void execute(Loader &l, const Statement &st) const
92         {
93                 std::vector<A0> values;
94                 values.reserve(st.args.size());
95                 for(Statement::Arguments::const_iterator i=st.args.begin(); i!=st.args.end(); ++i)
96                         values.push_back(i->get<A0>());
97                 (dynamic_cast<L &>(l).*func)(values);
98         }
99
100         virtual std::string get_signature() const
101         {
102                 std::string result;
103                 result += TypeInfo<A0>::signature;
104                 result += '*';
105                 return result;
106         }
107 };
108
109
110 /**
111 Loads a statement by calling a function with the statement itself as argument.
112 */
113 template<typename L>
114 class LoaderFunc1<L, const Statement &>: public LoaderAction
115 {
116 private:
117         typedef void (L::*FuncType)(const Statement &);
118
119         FuncType func;
120
121 public:
122         LoaderFunc1(FuncType f): func(f) { }
123
124         virtual void execute(Loader &l, const Statement &st) const
125         {
126                 (dynamic_cast<L &>(l).*func)(st);
127         }
128
129         virtual std::string get_signature() const
130         { return "*"; }
131 };
132
133
134 template<typename L, typename A0, typename A1>
135 class LoaderFunc2: public LoaderAction
136 {
137 private:
138         typedef void (L::*FuncType)(A0, A1);
139
140         FuncType func;
141
142 public:
143         LoaderFunc2(FuncType f): func(f) { }
144
145         virtual void execute(Loader &l, const Statement &st) const
146         {
147                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>());
148         }
149
150         virtual std::string get_signature() const
151         {
152                 std::string result;
153                 result += TypeInfo<A0>::signature;
154                 result += TypeInfo<A1>::signature;
155                 return result;
156         }
157 };
158
159
160 template<typename L, typename A0, typename A1, typename A2>
161 class LoaderFunc3: public LoaderAction
162 {
163 private:
164         typedef void (L::*FuncType)(A0, A1, A2);
165
166         FuncType func;
167
168 public:
169         LoaderFunc3(FuncType f): func(f) { }
170
171         virtual void execute(Loader &l, const Statement &st) const
172         {
173                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>());
174         }
175
176         virtual std::string get_signature() const
177         {
178                 std::string result;
179                 result += TypeInfo<A0>::signature;
180                 result += TypeInfo<A1>::signature;
181                 result += TypeInfo<A2>::signature;
182                 return result;
183         }
184 };
185
186
187 template<typename L, typename A0, typename A1, typename A2, typename A3>
188 class LoaderFunc4: public LoaderAction
189 {
190 private:
191         typedef void (L::*FuncType)(A0, A1, A2, A3);
192
193         FuncType func;
194
195 public:
196         LoaderFunc4(FuncType f): func(f) { }
197
198         virtual void execute(Loader &l, const Statement &st) const
199         {
200                 (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>());
201         }
202
203         virtual std::string get_signature() const
204         {
205                 std::string result;
206                 result += TypeInfo<A0>::signature;
207                 result += TypeInfo<A1>::signature;
208                 result += TypeInfo<A2>::signature;
209                 result += TypeInfo<A3>::signature;
210                 return result;
211         }
212 };
213
214
215 template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
216 class LoaderFunc5: public LoaderAction
217 {
218 private:
219         typedef void (L::*FuncType)(A0, A1, A2, A3, A4);
220
221         FuncType func;
222
223 public:
224         LoaderFunc5(FuncType f): func(f) { }
225
226         virtual void execute(Loader &l, const Statement &st) const
227         {
228                 (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>());
229         }
230
231         virtual std::string get_signature() const
232         {
233                 std::string result;
234                 result += TypeInfo<A0>::signature;
235                 result += TypeInfo<A1>::signature;
236                 result += TypeInfo<A2>::signature;
237                 result += TypeInfo<A3>::signature;
238                 result += TypeInfo<A4>::signature;
239                 return result;
240         }
241 };
242
243
244 template<typename L, typename T0>
245 class LoadValue1: public LoaderAction
246 {
247 private:
248         typedef T0 L::*Pointer0Type;
249
250         Pointer0Type ptr0;
251
252 public:
253         LoadValue1(Pointer0Type p0): ptr0(p0) { }
254
255         virtual void execute(Loader &l, const Statement &st) const
256         {
257                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0 = st.args[0].get<T0>();
258         }
259
260         virtual std::string get_signature() const
261         { return std::string(1, TypeInfo<T0>::signature); }
262 };
263
264
265 template<typename L, typename T0>
266 class LoadValue1<L, T0 *>: public LoaderAction
267 {
268 private:
269         typedef T0 *L::*Pointer0Type;
270
271         Pointer0Type ptr0;
272
273 public:
274         LoadValue1(Pointer0Type p0): ptr0(p0) { }
275
276         virtual void execute(Loader &l, const Statement &st) const
277         {
278                 typename L::Loader &ldr = dynamic_cast<typename L::Loader &>(l);
279                 ldr.get_object().*ptr0 = ldr.get_collection().template get<T0>(st.args[0].get<std::string>());
280         }
281
282         virtual std::string get_signature() const
283         { return std::string(1, TypeInfo<std::string>::signature); }
284 };
285
286
287 template<typename L, typename T0, typename T1>
288 class LoadValue2: public LoaderAction
289 {
290 private:
291         typedef T0 L::*Pointer0Type;
292         typedef T1 L::*Pointer1Type;
293
294         Pointer0Type ptr0;
295         Pointer1Type ptr1;
296
297 public:
298         LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
299
300         virtual void execute(Loader &l, const Statement &st) const
301         {
302                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0 = st.args[0].get<T0>();
303                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr1 = st.args[1].get<T1>();
304         }
305
306         virtual std::string get_signature() const
307         {
308                 std::string result;
309                 result += TypeInfo<T0>::signature;
310                 result += TypeInfo<T1>::signature;
311                 return result;
312         }
313 };
314
315 } // namespace DataFile
316 } // namespace Msp
317
318 #endif