]> git.tdb.fi Git - libs/datafile.git/blob - source/loader.h
Switch template parameters of load_sub around to allow automatic deduction
[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 "error.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 T0>
155 class LoadValue1: public LoaderAction
156 {
157 public:
158         typedef T0 L::*Pointer0Type;
159
160         LoadValue1(Pointer0Type p0): ptr0(p0) { }
161         void execute(Loader &l, const Statement &st) const
162         {
163                 if(st.args.size()!=1) throw TypeError(st.get_location()+": Wrong number of arguments");
164                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
165         }
166 private:
167         Pointer0Type ptr0;
168 };
169
170
171 template<typename L, typename T0, typename T1>
172 class LoadValue2: public LoaderAction
173 {
174 public:
175         typedef T0 L::*Pointer0Type;
176         typedef T1 L::*Pointer1Type;
177
178         LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
179         void execute(Loader &l, const Statement &st) const
180         {
181                 if(st.args.size()!=2) throw TypeError(st.get_location()+": Wrong number of arguments");
182                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
183                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr1=st.args[1].get<T1>();
184         }
185 private:
186         Pointer0Type ptr0;
187         Pointer1Type ptr1;
188 };
189
190
191 /**
192 Base class for data loaders.  To enable objects of a certain class to be loaded
193 from datafiles, create a public Loader class in it, derived from this class.
194 Typically the Loader class contains a reference to the object being loaded.  If
195 you want to load data members of the object directly, the Loader class must
196 have a member function get_object() returning that reference.
197 */
198 class Loader
199 {
200 public:
201         /**
202         Loads data from a statement.  This is normally only used by the Loader class
203         itself for loading sub-items, but needs to be public so it can be accessed
204         in derived objects.
205         */
206         void load(const Statement &st);
207
208         /**
209         Loads statements from a parser.
210         */
211         void load(Parser &p);
212
213         virtual ~Loader();
214 protected:
215         Loader(): cur_st(0) { }
216
217         template<typename L>
218         void add(const std::string &k, void (L::*func)())
219         { actions.insert(typename ActionMap::value_type(k, new LoaderFunc0<L>(func))); }
220
221         template<typename L, typename A0>
222         void add(const std::string &k, void (L::*func)(A0))
223         { actions.insert(typename ActionMap::value_type(k, new LoaderFunc1<L, A0>(func))); }
224
225         template<typename L, typename A0, typename A1>
226         void add(const std::string &k, void (L::*func)(A0, A1))
227         { actions.insert(typename ActionMap::value_type(k, new LoaderFunc2<L, A0, A1>(func))); }
228
229         template<typename L, typename A0, typename A1, typename A2>
230         void add(const std::string &k, void (L::*func)(A0, A1, A2))
231         { actions.insert(typename ActionMap::value_type(k, new LoaderFunc3<L, A0, A1, A2>(func))); }
232
233         template<typename L, typename A0, typename A1, typename A2, typename A3>
234         void add(const std::string &k, void (L::*func)(A0, A1, A2, A3))
235         { actions.insert(typename ActionMap::value_type(k, new LoaderFunc4<L, A0, A1, A2, A3>(func))); }
236
237         template<typename L, typename T0>
238         void add(const std::string &k, T0 L::*p0)
239         { actions.insert(typename ActionMap::value_type(k, new LoadValue1<L, T0>(p0))); }
240
241         template<typename L, typename T0, typename T1>
242         void add(const std::string &k, T0 L::*p0, T1 L::*p1)
243         { actions.insert(typename ActionMap::value_type(k, new LoadValue2<L, T0, T1>(p0, p1))); }
244
245         void add(const std::string &k)
246         { actions.insert(ActionMap::value_type(k, 0)); }
247
248         /**
249         Loads a sub-object from the statement being currently processed.  The Loader
250         class of the sub-object is automatically used.
251         */
252         template<typename S>
253         void load_sub(S &s)
254         { load_sub<typename S::Loader, S>(s); }
255
256         /**
257         Loads a sub-object with a custom Loader class.
258         */
259         template<typename L, typename S>
260         void load_sub(S &s)
261         {
262                 if(!cur_st)
263                         throw InvalidState("load_sub called without current statement");
264                 L loader(s);
265                 loader.load(*cur_st);
266         }
267
268         /**
269         Loads a sub-object with a custom Loader class that takes one argument in
270         addition to to object to be loaded.
271         */
272         template<typename L, typename S, typename T>
273         void load_sub(S &s, T &p)
274         {
275                 if(!cur_st)
276                         throw InvalidState("load_sub called without current statement");
277                 L loader(s, p);
278                 loader.load(*cur_st);
279         }
280 private:
281         typedef std::map<std::string, LoaderAction *> ActionMap;
282
283         ActionMap       actions;
284         const Statement *cur_st;
285
286         void load_statement(const Statement &st);
287 };
288
289 /**
290 Loads an object from a file.  The object must have a public Loader class.
291 */
292 template<typename T>
293 void load(T &obj, const std::string &fn)
294 {
295         std::ifstream in(fn.c_str());
296         if(!in)
297                 throw Exception("Couldn't open "+fn);
298
299         Parser parser(in, fn);
300         typename T::Loader loader(obj);
301         loader.load(parser);
302 }
303
304 } // namespace DataFile
305 } // namespace Msp
306
307 #endif