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 T0>
155 class LoadValue1: public LoaderAction
158 typedef T0 L::*Pointer0Type;
160 LoadValue1(Pointer0Type p0): ptr0(p0) { }
161 void execute(Loader &l, const Statement &st) const
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>();
171 template<typename L, typename T0, typename T1>
172 class LoadValue2: public LoaderAction
175 typedef T0 L::*Pointer0Type;
176 typedef T1 L::*Pointer1Type;
178 LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
179 void execute(Loader &l, const Statement &st) const
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>();
192 Base class for data loaders. To give loading capabilities to a class, create a
193 public Loader class in it, derived from this class. Typically a loader object
194 contains a reference to the loaded object. To make use of loading directly
195 into data members, the Loader class must have a get_object() member function,
196 returning that reference.
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
206 void load(const Statement &st);
209 Loads statements from a parser.
211 void load(Parser &p);
215 Loader(): cur_st(0) { }
218 Adds a keyword that is loaded with a zero-argument function.
221 void add(const std::string &k, void (L::*func)())
222 { actions.insert(typename ActionMap::value_type(k, new LoaderFunc0<L>(func))); }
224 template<typename L, typename A0>
225 void add(const std::string &k, void (L::*func)(A0))
226 { actions.insert(typename ActionMap::value_type(k, new LoaderFunc1<L, A0>(func))); }
228 template<typename L, typename A0, typename A1>
229 void add(const std::string &k, void (L::*func)(A0, A1))
230 { actions.insert(typename ActionMap::value_type(k, new LoaderFunc2<L, A0, A1>(func))); }
232 template<typename L, typename A0, typename A1, typename A2>
233 void add(const std::string &k, void (L::*func)(A0, A1, A2))
234 { actions.insert(typename ActionMap::value_type(k, new LoaderFunc3<L, A0, A1, A2>(func))); }
236 template<typename L, typename A0, typename A1, typename A2, typename A3>
237 void add(const std::string &k, void (L::*func)(A0, A1, A2, A3))
238 { actions.insert(typename ActionMap::value_type(k, new LoaderFunc4<L, A0, A1, A2, A3>(func))); }
241 Adds a keyword that is loaded into a variable of the loaded object.
243 template<typename L, typename T0>
244 void add(const std::string &k, T0 L::*p0)
245 { actions.insert(typename ActionMap::value_type(k, new LoadValue1<L, T0>(p0))); }
247 template<typename L, typename T0, typename T1>
248 void add(const std::string &k, T0 L::*p0, T1 L::*p1)
249 { actions.insert(typename ActionMap::value_type(k, new LoadValue2<L, T0, T1>(p0, p1))); }
252 Adds a keyword that is recognized but ignored.
254 void add(const std::string &k)
255 { actions.insert(ActionMap::value_type(k, 0)); }
258 Loads a sub-object from the statement being processed. The Loader class of
259 the sub-object is automatically used.
263 { load_sub<typename S::Loader, S>(s); }
266 Loads a sub-object with a custom Loader class.
268 template<typename L, typename S>
272 throw InvalidState("load_sub called without current statement");
274 loader.load(*cur_st);
277 template<typename S, typename T>
278 void load_sub(S &s, T &p)
279 { load_sub<typename S::Loader, S, T>(s, p); }
282 Loads a sub-object with a custom Loader class that takes one argument in
283 addition to to object to be loaded.
285 template<typename L, typename S, typename T>
286 void load_sub(S &s, T &p)
289 throw InvalidState("load_sub called without current statement");
291 loader.load(*cur_st);
295 Returns the source of the statement being processed. This can be used to
296 implement relative paths in include-like statements. Note that the source
297 may not necessarily be a file.
299 const std::string &get_source() const
302 throw InvalidState("get_source called without current statement");
303 return cur_st->source;
306 typedef std::map<std::string, LoaderAction *> ActionMap;
309 const Statement *cur_st;
311 void load_statement(const Statement &st);
315 Loads an object from a file. The object must have a public Loader class.
318 void load(T &obj, const std::string &fn)
320 std::ifstream in(fn.c_str());
322 throw Exception("Couldn't open "+fn);
324 Parser parser(in, fn);
325 typename T::Loader loader(obj);
329 } // namespace DataFile