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_
15 #include "statement.h"
25 Base class for loader actions.
31 Called when a statement is to be loaded.
33 virtual void execute(Loader &, const Statement &) const=0;
34 virtual ~LoaderAction() { }
41 Loads a statement by calling a function that takes no arguments.
44 class LoaderFunc0: public LoaderAction
47 typedef void (L::*FuncType)();
49 LoaderFunc0(FuncType f): func(f) { }
50 void execute(Loader &l, const Statement &st) const
52 if(st.args.size()!=0) throw TypeError(st.get_location()+": Wrong number of arguments");
53 (dynamic_cast<L &>(l).*func)();
61 Loads a statement by calling a function that takes one argument.
63 template<typename L, typename A0>
64 class LoaderFunc1: public LoaderAction
67 typedef void (L::*FuncType)(A0);
69 LoaderFunc1(FuncType f): func(f) { }
70 void execute(Loader &l, const Statement &st) const
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>());
81 Loads a statement by calling a function that takes an array of values.
83 template<typename L, typename A0>
84 class LoaderFunc1<L, const std::vector<A0> &>: public LoaderAction
87 typedef void (L::*FuncType)(const std::vector<A0> &);
89 LoaderFunc1(FuncType f): func(f) { }
90 void execute(Loader &l, const Statement &st) const
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);
103 template<typename L, typename A0, typename A1>
104 class LoaderFunc2: public LoaderAction
107 typedef void (L::*FuncType)(A0, A1);
109 LoaderFunc2(FuncType f): func(f) { }
110 void execute(Loader &l, const Statement &st) const
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>());
120 template<typename L, typename A0, typename A1, typename A2>
121 class LoaderFunc3: public LoaderAction
124 typedef void (L::*FuncType)(A0, A1, A2);
126 LoaderFunc3(FuncType f): func(f) { }
127 void execute(Loader &l, const Statement &st) const
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>());
137 template<typename L, typename A0, typename A1, typename A2, typename A3>
138 class LoaderFunc4: public LoaderAction
141 typedef void (L::*FuncType)(A0, A1, A2, A3);
143 LoaderFunc4(FuncType f): func(f) { }
144 void execute(Loader &l, const Statement &st) const
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>());
154 template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
155 class LoaderFunc5: public LoaderAction
158 typedef void (L::*FuncType)(A0, A1, A2, A3, A4);
160 LoaderFunc5(FuncType f): func(f) { }
161 void execute(Loader &l, const Statement &st) const
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>());
171 template<typename L, typename T0>
172 class LoadValue1: public LoaderAction
175 typedef T0 L::*Pointer0Type;
177 LoadValue1(Pointer0Type p0): ptr0(p0) { }
178 void execute(Loader &l, const Statement &st) const
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>();
188 template<typename L, typename T0>
189 class LoadValue1<L, T0 *>: public LoaderAction
192 typedef T0 *L::*Pointer0Type;
194 LoadValue1(Pointer0Type p0): ptr0(p0) { }
195 void execute(Loader &l, const Statement &st) const
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>());
206 template<typename L, typename T0, typename T1>
207 class LoadValue2: public LoaderAction
210 typedef T0 L::*Pointer0Type;
211 typedef T1 L::*Pointer1Type;
213 LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
214 void execute(Loader &l, const Statement &st) const
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>();
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.
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
243 void load(const Statement &st);
246 Loads statements from a parser.
248 void load(Parser &p);
252 Loader(): cur_st(0) { }
255 Adds a keyword that is loaded with a zero-argument function.
258 void add(const std::string &k, void (L::*func)())
259 { add(k, new LoaderFunc0<L>(func)); }
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)); }
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)); }
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)); }
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)); }
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)); }
282 Adds a keyword that is loaded into a variable of the loaded object.
284 template<typename L, typename T0>
285 void add(const std::string &k, T0 L::*p0)
286 { add(k, new LoadValue1<L, T0>(p0)); }
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)); }
293 Adds a keyword that is recognized but ignored.
295 void add(const std::string &k)
299 Loads a sub-object from the statement being processed. The Loader class of
300 the sub-object is automatically used.
304 { load_sub<typename S::Loader, S>(s); }
307 Loads a sub-object with a custom Loader class.
309 template<typename L, typename S>
313 throw InvalidState("load_sub called without current statement");
315 loader.load(*cur_st);
318 template<typename S, typename T>
319 void load_sub(S &s, T &p)
320 { load_sub<typename S::Loader, S, T>(s, p); }
323 Loads a sub-object with a custom Loader class that takes one argument in
324 addition to to object to be loaded.
326 template<typename L, typename S, typename T>
327 void load_sub(S &s, T &p)
330 throw InvalidState("load_sub called without current statement");
332 loader.load(*cur_st);
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.
340 const std::string &get_source() const
343 throw InvalidState("get_source called without current statement");
344 return cur_st->source;
347 typedef std::map<std::string, LoaderAction *> ActionMap;
350 const Statement *cur_st;
352 void add(const std::string &, LoaderAction *);
353 void load_statement(const Statement &st);
357 Loads an object from a file. The object must have a public Loader class.
360 void load(T &obj, const std::string &fn)
362 std::ifstream in(fn.c_str());
364 throw Exception("Couldn't open "+fn);
366 Parser parser(in, fn);
367 typename T::Loader loader(obj);
371 template<typename T, typename U>
372 void load(T &obj, const std::string &fn, U arg)
374 std::ifstream in(fn.c_str());
376 throw Exception("Couldn't open "+fn);
378 Parser parser(in, fn);
379 typename T::Loader loader(obj, arg);
383 } // namespace DataFile