]> git.tdb.fi Git - libs/datafile.git/blob - source/loader.h
Add missing virtual destructor to Collection::ItemKeywordBase
[libs/datafile.git] / source / loader.h
1 /* $Id$
2
3 This file is part of libmspdatafile
4 Copyright © 2006  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_DATAFILE_LOADER_H_
9 #define MSP_DATAFILE_LOADER_H_
10
11 #include <fstream>
12 #include <map>
13 #include "except.h"
14 #include "parser.h"
15 #include "statement.h"
16 #include "value.h"
17
18 namespace Msp {
19 namespace DataFile {
20
21 class Loader;
22 class Statement;
23
24 /**
25 Base class for loader actions.
26 */
27 class LoaderAction
28 {
29 public:
30         /**
31         Called when a statement is to be loaded.
32         */
33         virtual void execute(Loader &, const Statement &) const=0;
34         virtual ~LoaderAction() { }
35 protected:
36         LoaderAction() { }
37 };
38
39
40 /**
41 Loads a statement by calling a function that takes no arguments.
42 */
43 template<typename L>
44 class LoaderFunc0: public LoaderAction
45 {
46 public:
47         typedef void (L::*FuncType)();
48
49         LoaderFunc0(FuncType f): func(f) { }
50         void execute(Loader &l, const Statement &st) const
51         {
52                 if(st.args.size()!=0) throw TypeError(st.get_location()+": Wrong number of arguments");
53                 (dynamic_cast<L &>(l).*func)();
54         };
55 private:
56         FuncType func;
57 };
58
59
60 /**
61 Loads a statement by calling a function that takes one argument.
62 */
63 template<typename L, typename A0>
64 class LoaderFunc1: public LoaderAction
65 {
66 public:
67         typedef void (L::*FuncType)(A0);
68
69         LoaderFunc1(FuncType f): func(f) { }
70         void execute(Loader &l, const Statement &st) const
71         {
72                 if(st.args.size()!=1) throw TypeError(st.get_location()+": Wrong number of arguments");
73                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>());
74         }
75 private:
76         FuncType func;
77 };
78
79
80 /**
81 Loads a statement by calling a function that takes an array of values.
82 */
83 template<typename L, typename A0>
84 class LoaderFunc1<L, const std::vector<A0> &>: public LoaderAction
85 {
86 public:
87         typedef void (L::*FuncType)(const std::vector<A0> &);
88
89         LoaderFunc1(FuncType f): func(f) { }
90         void execute(Loader &l, const Statement &st) const
91         {
92                 std::vector<A0> values;
93                 values.reserve(st.args.size());
94                 for(ValueArray::const_iterator i=st.args.begin(); i!=st.args.end(); ++i)
95                         values.push_back(i->get<A0>());
96                 (dynamic_cast<L &>(l).*func)(values);
97         }
98 private:
99         FuncType func;
100 };
101
102
103 template<typename L, typename A0, typename A1>
104 class LoaderFunc2: public LoaderAction
105 {
106 public:
107         typedef void (L::*FuncType)(A0, A1);
108
109         LoaderFunc2(FuncType f): func(f) { }
110         void execute(Loader &l, const Statement &st) const
111         {
112                 if(st.args.size()!=2) throw TypeError(st.get_location()+": Wrong number of arguments");
113                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>());
114         }
115 private:
116         FuncType func;
117 };
118
119
120 template<typename L, typename A0, typename A1, typename A2>
121 class LoaderFunc3: public LoaderAction
122 {
123 public:
124         typedef void (L::*FuncType)(A0, A1, A2);
125
126         LoaderFunc3(FuncType f): func(f) { }
127         void execute(Loader &l, const Statement &st) const
128         {
129                 if(st.args.size()!=3) throw TypeError(st.get_location()+": Wrong number of arguments");
130                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>());
131         }
132 private:
133         FuncType func;
134 };
135
136
137 template<typename L, typename A0, typename A1, typename A2, typename A3>
138 class LoaderFunc4: public LoaderAction
139 {
140 public:
141         typedef void (L::*FuncType)(A0, A1, A2, A3);
142
143         LoaderFunc4(FuncType f): func(f) { }
144         void execute(Loader &l, const Statement &st) const
145         {
146                 if(st.args.size()!=4) throw TypeError(st.get_location()+": Wrong number of arguments");
147                 (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>());
148         }
149 private:
150         FuncType func;
151 };
152
153
154 template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
155 class LoaderFunc5: public LoaderAction
156 {
157 public:
158         typedef void (L::*FuncType)(A0, A1, A2, A3, A4);
159
160         LoaderFunc5(FuncType f): func(f) { }
161         void execute(Loader &l, const Statement &st) const
162         {
163                 if(st.args.size()!=5) throw TypeError(st.get_location()+": Wrong number of arguments");
164                 (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>());
165         }
166 private:
167         FuncType func;
168 };
169
170
171 template<typename L, typename T0>
172 class LoadValue1: public LoaderAction
173 {
174 public:
175         typedef T0 L::*Pointer0Type;
176
177         LoadValue1(Pointer0Type p0): ptr0(p0) { }
178         void execute(Loader &l, const Statement &st) const
179         {
180                 if(st.args.size()!=1) throw TypeError(st.get_location()+": Wrong number of arguments");
181                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
182         }
183 private:
184         Pointer0Type ptr0;
185 };
186
187
188 template<typename L, typename T0>
189 class LoadValue1<L, T0 *>: public LoaderAction
190 {
191 public:
192         typedef T0 *L::*Pointer0Type;
193
194         LoadValue1(Pointer0Type p0): ptr0(p0) { }
195         void execute(Loader &l, const Statement &st) const
196         {
197                 if(st.args.size()!=1) throw TypeError(st.get_location()+": Wrong number of arguments");
198                 typename L::Loader &ldr=dynamic_cast<typename L::Loader &>(l);
199                 ldr.get_object().*ptr0=ldr.get_collection().template get<T0>(st.args[0].get<std::string>());
200         }
201 private:
202         Pointer0Type ptr0;
203 };
204
205
206 template<typename L, typename T0, typename T1>
207 class LoadValue2: public LoaderAction
208 {
209 public:
210         typedef T0 L::*Pointer0Type;
211         typedef T1 L::*Pointer1Type;
212
213         LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
214         void execute(Loader &l, const Statement &st) const
215         {
216                 if(st.args.size()!=2) throw TypeError(st.get_location()+": Wrong number of arguments");
217                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
218                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr1=st.args[1].get<T1>();
219         }
220 private:
221         Pointer0Type ptr0;
222         Pointer1Type ptr1;
223 };
224
225
226 /**
227 Base class for data loaders.  To give loading capabilities to a class, create a
228 public Loader class in it, derived from this class.  Typically a loader object
229 contains a reference to the loaded object.  To make use of loading directly
230 into data members, the Loader class must have a get_object() member function,
231 returning that reference.  If direct loading of pointers is desired, the Loader
232 class must also have a get_collection() member function, returning a collection
233 to get pointers from.
234 */
235 class Loader
236 {
237 public:
238         /**
239         Loads data from a statement.  This is normally only used by the Loader class
240         itself for loading sub-items, but needs to be public so it can be accessed
241         in derived objects.
242         */
243         void load(const Statement &st);
244
245         /**
246         Loads statements from a parser.
247         */
248         void load(Parser &p);
249
250         virtual ~Loader();
251 protected:
252         Loader(): cur_st(0) { }
253
254         /**
255         Adds a keyword that is loaded with a zero-argument function.
256         */
257         template<typename L>
258         void add(const std::string &k, void (L::*func)())
259         { add(k, new LoaderFunc0<L>(func)); }
260
261         template<typename L, typename A0>
262         void add(const std::string &k, void (L::*func)(A0))
263         { add(k, new LoaderFunc1<L, A0>(func)); }
264
265         template<typename L, typename A0, typename A1>
266         void add(const std::string &k, void (L::*func)(A0, A1))
267         { add(k, new LoaderFunc2<L, A0, A1>(func)); }
268
269         template<typename L, typename A0, typename A1, typename A2>
270         void add(const std::string &k, void (L::*func)(A0, A1, A2))
271         { add(k, new LoaderFunc3<L, A0, A1, A2>(func)); }
272
273         template<typename L, typename A0, typename A1, typename A2, typename A3>
274         void add(const std::string &k, void (L::*func)(A0, A1, A2, A3))
275         { add(k, new LoaderFunc4<L, A0, A1, A2, A3>(func)); }
276
277         template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
278         void add(const std::string &k, void (L::*func)(A0, A1, A2, A3, A4))
279         { add(k, new LoaderFunc5<L, A0, A1, A2, A3, A4>(func)); }
280
281         /**
282         Adds a keyword that is loaded into a variable of the loaded object.
283         */
284         template<typename L, typename T0>
285         void add(const std::string &k, T0 L::*p0)
286         { add(k, new LoadValue1<L, T0>(p0)); }
287
288         template<typename L, typename T0, typename T1>
289         void add(const std::string &k, T0 L::*p0, T1 L::*p1)
290         { add(k, new LoadValue2<L, T0, T1>(p0, p1)); }
291
292         /**
293         Adds a keyword that is recognized but ignored.
294         */
295         void add(const std::string &k)
296         { add(k, 0); }
297
298         /**
299         Loads a sub-object from the statement being processed.  The Loader class of
300         the sub-object is automatically used.
301         */
302         template<typename S>
303         void load_sub(S &s)
304         { load_sub<typename S::Loader, S>(s); }
305
306         /**
307         Loads a sub-object with a custom Loader class.
308         */
309         template<typename L, typename S>
310         void load_sub(S &s)
311         {
312                 if(!cur_st)
313                         throw InvalidState("load_sub called without current statement");
314                 L loader(s);
315                 loader.load(*cur_st);
316         }
317
318         template<typename S, typename T>
319         void load_sub(S &s, T &p)
320         { load_sub<typename S::Loader, S, T>(s, p); }
321
322         /**
323         Loads a sub-object with a custom Loader class that takes one argument in
324         addition to to object to be loaded.
325         */
326         template<typename L, typename S, typename T>
327         void load_sub(S &s, T &p)
328         {
329                 if(!cur_st)
330                         throw InvalidState("load_sub called without current statement");
331                 L loader(s, p);
332                 loader.load(*cur_st);
333         }
334
335         /**
336         Returns the source of the statement being processed.  This can be used to
337         implement relative paths in include-like statements.  Note that the source
338         may not necessarily be a file.
339         */
340         const std::string &get_source() const
341         {
342                 if(!cur_st)
343                         throw InvalidState("get_source called without current statement");
344                 return cur_st->source;
345         }
346
347         virtual void finish() { }
348 private:
349         typedef std::map<std::string, LoaderAction *> ActionMap;
350
351         ActionMap       actions;
352         const Statement *cur_st;
353
354         void add(const std::string &, LoaderAction *);
355         void load_statement(const Statement &st);
356 };
357
358 /**
359 Loads an object from a file.  The object must have a public Loader class.
360 */
361 template<typename T>
362 void load(T &obj, const std::string &fn)
363 {
364         std::ifstream in(fn.c_str());
365         if(!in)
366                 throw Exception("Couldn't open "+fn);
367
368         Parser parser(in, fn);
369         typename T::Loader loader(obj);
370         loader.load(parser);
371 }
372
373 template<typename T, typename U>
374 void load(T &obj, const std::string &fn, U arg)
375 {
376         std::ifstream in(fn.c_str());
377         if(!in)
378                 throw Exception("Couldn't open "+fn);
379
380         Parser parser(in, fn);
381         typename T::Loader loader(obj, arg);
382         loader.load(parser);
383 }
384
385 } // namespace DataFile
386 } // namespace Msp
387
388 #endif