2 This file is part of libmspparser
3 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
6 #ifndef MSP_PARSER_LOADER_H_
7 #define MSP_PARSER_LOADER_H_
10 #include <msp/error.h>
12 #include "statement.h"
24 virtual void execute(Loader &, const Statement &) const=0;
25 virtual ~LoaderAction() { }
31 class LoaderFunc0: public LoaderAction
34 typedef void (L::*FuncType)();
36 LoaderFunc0(FuncType f): func(f) { }
37 void execute(Loader &l, const Statement &st) const
39 if(st.args.size()!=0) throw TypeError("Wrong number of arguments");
40 (dynamic_cast<L &>(l).*func)();
46 template<typename L, typename A0>
47 class LoaderFunc1: public LoaderAction
50 typedef void (L::*FuncType)(A0);
52 LoaderFunc1(FuncType f): func(f) { }
53 void execute(Loader &l, const Statement &st) const
55 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
56 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>());
62 template<typename L, typename A0, typename A1>
63 class LoaderFunc2: public LoaderAction
66 typedef void (L::*FuncType)(A0, A1);
68 LoaderFunc2(FuncType f): func(f) { }
69 void execute(Loader &l, const Statement &st) const
71 if(st.args.size()!=2) throw TypeError("Wrong number of arguments");
72 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>());
78 template<typename L, typename A0, typename A1, typename A2>
79 class LoaderFunc3: public LoaderAction
82 typedef void (L::*FuncType)(A0, A1, A2);
84 LoaderFunc3(FuncType f): func(f) { }
85 void execute(Loader &l, const Statement &st) const
87 if(st.args.size()!=3) throw TypeError("Wrong number of arguments");
88 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>());
94 template<typename L, typename A0, typename A1, typename A2, typename A3>
95 class LoaderFunc4: public LoaderAction
98 typedef void (L::*FuncType)(A0, A1, A2, A3);
100 LoaderFunc4(FuncType f): func(f) { }
101 void execute(Loader &l, const Statement &st) const
103 if(st.args.size()!=4) throw TypeError("Wrong number of arguments");
104 (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>());
110 template<typename L, typename T>
111 class LoadValue: public LoaderAction
114 typedef T L::*PointerType;
116 LoadValue(PointerType p): ptr(p) { }
117 void execute(Loader &l, const Statement &st) const
119 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
120 dynamic_cast<typename L::Loader &>(l).get_object().*ptr=st.args[0].get<T>();
129 void load(const Statement &st)
131 for(std::list<Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
138 Statement st=p.parse();
145 for(ActionMap::iterator i=actions.begin(); i!=actions.end(); ++i)
149 Loader(): cur_st(0) { }
152 void add(const std::string &k, void (L::*func)())
153 { actions.insert(typename ActionMap::value_type(k, new LoaderFunc0<L>(func))); }
155 template<typename L, typename A0>
156 void add(const std::string &k, void (L::*func)(A0))
157 { actions.insert(typename ActionMap::value_type(k, new LoaderFunc1<L, A0>(func))); }
159 template<typename L, typename A0, typename A1>
160 void add(const std::string &k, void (L::*func)(A0, A1))
161 { actions.insert(typename ActionMap::value_type(k, new LoaderFunc2<L, A0, A1>(func))); }
163 template<typename L, typename A0, typename A1, typename A2>
164 void add(const std::string &k, void (L::*func)(A0, A1, A2))
165 { actions.insert(typename ActionMap::value_type(k, new LoaderFunc3<L, A0, A1, A2>(func))); }
167 template<typename L, typename A0, typename A1, typename A2, typename A3>
168 void add(const std::string &k, void (L::*func)(A0, A1, A2, A3))
169 { actions.insert(typename ActionMap::value_type(k, new LoaderFunc4<L, A0, A1, A2, A3>(func))); }
171 template<typename L, typename T>
172 void add(const std::string &k, T L::*p)
173 { actions.insert(typename ActionMap::value_type(k, new LoadValue<L, T>(p))); }
177 { load_sub<S, typename S::Loader>(s); }
179 template<typename S, typename L>
183 throw Exception("load_sub called without current statement");
185 loader.load(*cur_st);
188 typedef std::map<std::string, LoaderAction *> ActionMap;
191 const Statement *cur_st;
193 void load_statement(const Statement &st)
196 ActionMap::iterator j=actions.find(st.keyword);
198 throw Exception(st.get_location()+": Unknown keyword '"+st.keyword+"'");
199 j->second->execute(*this, st);
204 } // namespace Parser