]> git.tdb.fi Git - libs/datafile.git/blob - source/loader.h
Require mspcore instead of mspmisc
[libs/datafile.git] / source / loader.h
1 /*
2 This file is part of libmspparser
3 Copyright © 2006  Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
5 */
6 #ifndef MSP_PARSER_LOADER_H_
7 #define MSP_PARSER_LOADER_H_
8
9 #include <fstream>
10 #include <map>
11 #include "error.h"
12 #include "parser.h"
13 #include "statement.h"
14 #include "value.h"
15
16 namespace Msp {
17 namespace Parser {
18
19 class Loader;
20 class Statement;
21
22 class LoaderAction
23 {
24 public:
25         virtual void execute(Loader &, const Statement &) const=0;
26         virtual ~LoaderAction() { }
27 protected:
28         LoaderAction() { }
29 };
30
31 template<typename L>
32 class LoaderFunc0: public LoaderAction
33 {
34 public:
35         typedef void (L::*FuncType)();
36         
37         LoaderFunc0(FuncType f): func(f) { }
38         void execute(Loader &l, const Statement &st) const
39         {
40                 if(st.args.size()!=0) throw TypeError(st.get_location()+": Wrong number of arguments");
41                 (dynamic_cast<L &>(l).*func)();
42         };
43 private:
44         FuncType func;
45 };
46
47 template<typename L, typename A0>
48 class LoaderFunc1: public LoaderAction
49 {
50 public:
51         typedef void (L::*FuncType)(A0);
52         
53         LoaderFunc1(FuncType f): func(f) { }
54         void execute(Loader &l, const Statement &st) const
55         {
56                 if(st.args.size()!=1) throw TypeError(st.get_location()+": Wrong number of arguments");
57                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>());
58         }
59 private:
60         FuncType func;
61 };
62
63 template<typename L, typename A0, typename A1>
64 class LoaderFunc2: public LoaderAction
65 {
66 public:
67         typedef void (L::*FuncType)(A0, A1);
68         
69         LoaderFunc2(FuncType f): func(f) { }
70         void execute(Loader &l, const Statement &st) const
71         {
72                 if(st.args.size()!=2) throw TypeError(st.get_location()+": Wrong number of arguments");
73                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>());
74         }
75 private:
76         FuncType func;
77 };
78
79 template<typename L, typename A0, typename A1, typename A2>
80 class LoaderFunc3: public LoaderAction
81 {
82 public:
83         typedef void (L::*FuncType)(A0, A1, A2);
84         
85         LoaderFunc3(FuncType f): func(f) { }
86         void execute(Loader &l, const Statement &st) const
87         {
88                 if(st.args.size()!=3) throw TypeError(st.get_location()+": Wrong number of arguments");
89                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>());
90         }
91 private:
92         FuncType func;
93 };
94
95 template<typename L, typename A0, typename A1, typename A2, typename A3>
96 class LoaderFunc4: public LoaderAction
97 {
98 public:
99         typedef void (L::*FuncType)(A0, A1, A2, A3);
100         
101         LoaderFunc4(FuncType f): func(f) { }
102         void execute(Loader &l, const Statement &st) const
103         {
104                 if(st.args.size()!=4) throw TypeError(st.get_location()+": Wrong number of arguments");
105                 (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>());
106         }
107 private:
108         FuncType func;
109 };
110
111 template<typename L, typename T>
112 class LoadValue: public LoaderAction
113 {
114 public:
115         typedef T L::*PointerType;
116         
117         LoadValue(PointerType p): ptr(p) { }
118         void execute(Loader &l, const Statement &st) const
119         {
120                 if(st.args.size()!=1) throw TypeError(st.get_location()+": Wrong number of arguments");
121                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr=st.args[0].get<T>();
122         }
123 private:
124         PointerType ptr;
125 };
126
127 class Loader
128 {
129 public:
130         void load(const Statement &st)
131         {
132                 for(std::list<Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
133                         load_statement(*i);
134         }
135         void load(Parser &p)
136         {
137                 while(p)
138                 {
139                         Statement st=p.parse();
140                         if(st.valid)
141                                 load_statement(st);
142                 }
143         }
144         virtual ~Loader()
145         {
146                 for(ActionMap::iterator i=actions.begin(); i!=actions.end(); ++i)
147                         delete i->second;
148         }
149 protected:
150         Loader(): cur_st(0) { }
151         
152         template<typename L>
153         void add(const std::string &k, void (L::*func)())
154         { actions.insert(typename ActionMap::value_type(k, new LoaderFunc0<L>(func))); }
155         
156         template<typename L, typename A0>
157         void add(const std::string &k, void (L::*func)(A0))
158         { actions.insert(typename ActionMap::value_type(k, new LoaderFunc1<L, A0>(func))); }
159         
160         template<typename L, typename A0, typename A1>
161         void add(const std::string &k, void (L::*func)(A0, A1))
162         { actions.insert(typename ActionMap::value_type(k, new LoaderFunc2<L, A0, A1>(func))); }
163         
164         template<typename L, typename A0, typename A1, typename A2>
165         void add(const std::string &k, void (L::*func)(A0, A1, A2))
166         { actions.insert(typename ActionMap::value_type(k, new LoaderFunc3<L, A0, A1, A2>(func))); }
167         
168         template<typename L, typename A0, typename A1, typename A2, typename A3>
169         void add(const std::string &k, void (L::*func)(A0, A1, A2, A3))
170         { actions.insert(typename ActionMap::value_type(k, new LoaderFunc4<L, A0, A1, A2, A3>(func))); }
171
172         template<typename L, typename T>
173         void add(const std::string &k, T L::*p)
174         { actions.insert(typename ActionMap::value_type(k, new LoadValue<L, T>(p))); }
175
176         void add(const std::string &k)
177         { actions.insert(ActionMap::value_type(k, 0)); }
178
179         template<typename S>
180         void load_sub(S &s)
181         { load_sub<S, typename S::Loader>(s); }
182
183         template<typename S, typename L>
184         void load_sub(S &s)
185         {
186                 if(!cur_st)
187                         throw Exception("load_sub called without current statement");
188                 L loader(s);
189                 loader.load(*cur_st);
190         }
191 private:
192         typedef std::map<std::string, LoaderAction *> ActionMap;
193
194         ActionMap       actions;
195         const Statement *cur_st;
196
197         void load_statement(const Statement &st)
198         {
199                 cur_st=&st;
200                 ActionMap::iterator j=actions.find(st.keyword);
201                 if(j==actions.end())
202                         throw Exception(st.get_location()+": Unknown keyword '"+st.keyword+"'");
203                 if(j->second)
204                         j->second->execute(*this, st);
205                 cur_st=0;
206         }
207 };
208
209 template<typename T>
210 void load(T &obj, const std::string &fn)
211 {
212         std::ifstream in(fn.c_str());
213         if(!in)
214                 throw Exception("Couldn't open "+fn);
215
216         Parser parser(in, fn);
217         typename T::Loader loader(obj);
218         loader.load(parser);
219 }
220
221 } // namespace Parser
222 } // namespace Msp
223
224 #endif