3 This file is part of libmspdatafile
4 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #ifndef MSP_DATAFILE_LOADER_H_
9 #define MSP_DATAFILE_LOADER_H_
12 #include <msp/io/buffered.h>
13 #include <msp/io/file.h>
16 #include "statement.h"
26 Base class for loader actions.
32 Called when a statement is to be loaded.
34 virtual void execute(Loader &, const Statement &) const=0;
35 virtual ~LoaderAction() { }
42 Loads a statement by calling a function that takes no arguments.
45 class LoaderFunc0: public LoaderAction
48 typedef void (L::*FuncType)();
50 LoaderFunc0(FuncType f): func(f) { }
51 void execute(Loader &l, const Statement &st) const
53 if(st.args.size()!=0) throw TypeError(st.get_location()+": Wrong number of arguments");
54 (dynamic_cast<L &>(l).*func)();
62 Loads a statement by calling a function that takes one argument.
64 template<typename L, typename A0>
65 class LoaderFunc1: public LoaderAction
68 typedef void (L::*FuncType)(A0);
70 LoaderFunc1(FuncType f): func(f) { }
71 void execute(Loader &l, const Statement &st) const
73 if(st.args.size()!=1) throw TypeError(st.get_location()+": Wrong number of arguments");
74 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>());
82 Loads a statement by calling a function that takes an array of values.
84 template<typename L, typename A0>
85 class LoaderFunc1<L, const std::vector<A0> &>: public LoaderAction
88 typedef void (L::*FuncType)(const std::vector<A0> &);
90 LoaderFunc1(FuncType f): func(f) { }
91 void execute(Loader &l, const Statement &st) const
93 std::vector<A0> values;
94 values.reserve(st.args.size());
95 for(ValueArray::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);
104 template<typename L, typename A0, typename A1>
105 class LoaderFunc2: public LoaderAction
108 typedef void (L::*FuncType)(A0, A1);
110 LoaderFunc2(FuncType f): func(f) { }
111 void execute(Loader &l, const Statement &st) const
113 if(st.args.size()!=2) throw TypeError(st.get_location()+": Wrong number of arguments");
114 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>());
121 template<typename L, typename A0, typename A1, typename A2>
122 class LoaderFunc3: public LoaderAction
125 typedef void (L::*FuncType)(A0, A1, A2);
127 LoaderFunc3(FuncType f): func(f) { }
128 void execute(Loader &l, const Statement &st) const
130 if(st.args.size()!=3) throw TypeError(st.get_location()+": Wrong number of arguments");
131 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>());
138 template<typename L, typename A0, typename A1, typename A2, typename A3>
139 class LoaderFunc4: public LoaderAction
142 typedef void (L::*FuncType)(A0, A1, A2, A3);
144 LoaderFunc4(FuncType f): func(f) { }
145 void execute(Loader &l, const Statement &st) const
147 if(st.args.size()!=4) throw TypeError(st.get_location()+": Wrong number of arguments");
148 (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>());
155 template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
156 class LoaderFunc5: public LoaderAction
159 typedef void (L::*FuncType)(A0, A1, A2, A3, A4);
161 LoaderFunc5(FuncType f): func(f) { }
162 void execute(Loader &l, const Statement &st) const
164 if(st.args.size()!=5) throw TypeError(st.get_location()+": Wrong number of arguments");
165 (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>());
172 template<typename L, typename T0>
173 class LoadValue1: public LoaderAction
176 typedef T0 L::*Pointer0Type;
178 LoadValue1(Pointer0Type p0): ptr0(p0) { }
179 void execute(Loader &l, const Statement &st) const
181 if(st.args.size()!=1) throw TypeError(st.get_location()+": Wrong number of arguments");
182 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
189 template<typename L, typename T0>
190 class LoadValue1<L, T0 *>: public LoaderAction
193 typedef T0 *L::*Pointer0Type;
195 LoadValue1(Pointer0Type p0): ptr0(p0) { }
196 void execute(Loader &l, const Statement &st) const
198 if(st.args.size()!=1) throw TypeError(st.get_location()+": Wrong number of arguments");
199 typename L::Loader &ldr=dynamic_cast<typename L::Loader &>(l);
200 ldr.get_object().*ptr0=ldr.get_collection().template get<T0>(st.args[0].get<std::string>());
207 template<typename L, typename T0, typename T1>
208 class LoadValue2: public LoaderAction
211 typedef T0 L::*Pointer0Type;
212 typedef T1 L::*Pointer1Type;
214 LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
215 void execute(Loader &l, const Statement &st) const
217 if(st.args.size()!=2) throw TypeError(st.get_location()+": Wrong number of arguments");
218 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
219 dynamic_cast<typename L::Loader &>(l).get_object().*ptr1=st.args[1].get<T1>();
228 Base class for data loaders. To give loading capabilities to a class, create a
229 public Loader class in it, derived from this class. Typically a loader object
230 contains a reference to the loaded object. To make use of loading directly
231 into data members, the Loader class must have a get_object() member function,
232 returning that reference. If direct loading of pointers is desired, the Loader
233 class must also have a get_collection() member function, returning a collection
234 to get pointers from.
240 Loads data from a statement. This is normally only used by the Loader class
241 itself for loading sub-items, but needs to be public so it can be accessed
244 void load(const Statement &st);
247 Loads statements from a parser.
249 void load(Parser &p);
253 Loader(): cur_st(0) { }
256 Adds a keyword that is loaded with a zero-argument function.
259 void add(const std::string &k, void (L::*func)())
260 { add(k, new LoaderFunc0<L>(func)); }
262 template<typename L, typename A0>
263 void add(const std::string &k, void (L::*func)(A0))
264 { add(k, new LoaderFunc1<L, A0>(func)); }
266 template<typename L, typename A0, typename A1>
267 void add(const std::string &k, void (L::*func)(A0, A1))
268 { add(k, new LoaderFunc2<L, A0, A1>(func)); }
270 template<typename L, typename A0, typename A1, typename A2>
271 void add(const std::string &k, void (L::*func)(A0, A1, A2))
272 { add(k, new LoaderFunc3<L, A0, A1, A2>(func)); }
274 template<typename L, typename A0, typename A1, typename A2, typename A3>
275 void add(const std::string &k, void (L::*func)(A0, A1, A2, A3))
276 { add(k, new LoaderFunc4<L, A0, A1, A2, A3>(func)); }
278 template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
279 void add(const std::string &k, void (L::*func)(A0, A1, A2, A3, A4))
280 { add(k, new LoaderFunc5<L, A0, A1, A2, A3, A4>(func)); }
283 Adds a keyword that is loaded into a variable of the loaded object.
285 template<typename L, typename T0>
286 void add(const std::string &k, T0 L::*p0)
287 { add(k, new LoadValue1<L, T0>(p0)); }
289 template<typename L, typename T0, typename T1>
290 void add(const std::string &k, T0 L::*p0, T1 L::*p1)
291 { add(k, new LoadValue2<L, T0, T1>(p0, p1)); }
294 Adds a keyword that is recognized but ignored.
296 void add(const std::string &k)
300 Loads a sub-object from the statement being processed. The Loader class of
301 the sub-object is automatically used.
305 { load_sub<typename S::Loader, S>(s); }
308 Loads a sub-object with a custom Loader class.
310 template<typename L, typename S>
314 throw InvalidState("load_sub called without current statement");
316 loader.load(*cur_st);
319 template<typename S, typename T>
320 void load_sub(S &s, T &p)
321 { load_sub<typename S::Loader, S, T>(s, p); }
324 Loads a sub-object with a custom Loader class that takes one argument in
325 addition to to object to be loaded.
327 template<typename L, typename S, typename T>
328 void load_sub(S &s, T &p)
331 throw InvalidState("load_sub called without current statement");
333 loader.load(*cur_st);
337 Returns the source of the statement being processed. This can be used to
338 implement relative paths in include-like statements. Note that the source
339 may not necessarily be a file.
341 const std::string &get_source() const
344 throw InvalidState("get_source called without current statement");
345 return cur_st->source;
348 virtual void finish() { }
350 typedef std::map<std::string, LoaderAction *> ActionMap;
353 const Statement *cur_st;
355 void add(const std::string &, LoaderAction *);
356 void load_statement(const Statement &st);
360 Loads an object from a file. The object must have a public Loader class.
363 void load(T &obj, const std::string &fn)
366 IO::Buffered buf(in);
368 Parser parser(buf, fn);
369 typename T::Loader loader(obj);
373 template<typename T, typename U>
374 void load(T &obj, const std::string &fn, U arg)
377 IO::Buffered buf(in);
379 Parser parser(in, fn);
380 typename T::Loader loader(obj, arg);
384 } // namespace DataFile