]> git.tdb.fi Git - libs/datafile.git/blob - source/loader.h
Move mspdatatool source to its own directory
[libs/datafile.git] / source / loader.h
1 /* $Id$
2
3 This file is part of libmspdatafile
4 Copyright © 2006-2008  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_DATAFILE_LOADER_H_
9 #define MSP_DATAFILE_LOADER_H_
10
11 #include <map>
12 #include <msp/io/buffered.h>
13 #include <msp/io/file.h>
14 #include "except.h"
15 #include "loaderaction.h"
16 #include "parser.h"
17 #include "statement.h"
18
19 namespace Msp {
20 namespace DataFile {
21
22 /**
23 Base class for data loaders.  This class only provides core functionality.
24 You'll almost certainly want to use one of the BasicLoader classes instead.
25
26 Under normal circumstances, a class capable of being loaded should have a
27 nested typed called Loader which resolves to a descendant of this class.  If
28 another structure is used, the loader object must be constructed manually.
29
30 A loader class should execute one or more calls to the various add() functions
31 to register actions with expected keywords.  Currently possible actions are
32 calling a function of the loader, storing values in member variables of an
33 object and ignoring the statement.  If a unexpected keyword is encountered, an
34 exception is thrown and the loading is aborted.
35
36 A sub-object can be loaded with one of the load_sub functions.
37
38 When loading has finished successfully, the virtual function finish() is
39 called.  Any post-processing of the data should be placed here and not in the
40 destructor.
41
42 See also classes BasicLoader and BasicLoader2.
43 */
44 class Loader
45 {
46 private:
47         /**
48         Loads data from a statement.
49         */
50         void load(const Statement &st);
51
52 public:
53         /**
54         Loads statements from a parser.
55         */
56         void load(Parser &p);
57
58         virtual ~Loader();
59 protected:
60         Loader(): cur_st(0) { }
61
62         /**
63         Adds a keyword that is loaded with a zero-argument function.
64         */
65         template<typename L>
66         void add(const std::string &k, void (L::*func)())
67         { add(k, new LoaderFunc0<L>(func)); }
68
69         template<typename L, typename A0>
70         void add(const std::string &k, void (L::*func)(A0))
71         { add(k, new LoaderFunc1<L, A0>(func)); }
72
73         template<typename L, typename A0, typename A1>
74         void add(const std::string &k, void (L::*func)(A0, A1))
75         { add(k, new LoaderFunc2<L, A0, A1>(func)); }
76
77         template<typename L, typename A0, typename A1, typename A2>
78         void add(const std::string &k, void (L::*func)(A0, A1, A2))
79         { add(k, new LoaderFunc3<L, A0, A1, A2>(func)); }
80
81         template<typename L, typename A0, typename A1, typename A2, typename A3>
82         void add(const std::string &k, void (L::*func)(A0, A1, A2, A3))
83         { add(k, new LoaderFunc4<L, A0, A1, A2, A3>(func)); }
84
85         template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
86         void add(const std::string &k, void (L::*func)(A0, A1, A2, A3, A4))
87         { add(k, new LoaderFunc5<L, A0, A1, A2, A3, A4>(func)); }
88
89         /**
90         Adds a keyword that is loaded into a variable of the loaded object.
91         */
92         template<typename L, typename T0>
93         void add(const std::string &k, T0 L::*p0)
94         { add(k, new LoadValue1<L, T0>(p0)); }
95
96         template<typename L, typename T0, typename T1>
97         void add(const std::string &k, T0 L::*p0, T1 L::*p1)
98         { add(k, new LoadValue2<L, T0, T1>(p0, p1)); }
99
100         /**
101         Adds a keyword that is recognized but ignored.
102         */
103         void add(const std::string &k)
104         { add(k, 0); }
105
106         /**
107         Loads a sub-object from the statement being processed.  The Loader class of
108         the sub-object is automatically used.
109         */
110         template<typename S>
111         void load_sub(S &s)
112         {
113                 typename S::Loader ldr(s);
114                 load_sub_with(ldr);
115         }
116
117         /**
118         Loads a sub-object from the statement being processed with an extra parameter
119         for the Loader.  The Loader class of the sub-object is automatically used.
120         */
121         template<typename S, typename T>
122         void load_sub(S &s, T &p)
123         {
124                 typename S::Loader ldr(s, p);
125                 load_sub_with(ldr);
126         }
127
128         /**
129         Processes the current statement's substatements with another Loader.
130         */
131         void load_sub_with(Loader &);
132
133         /**
134         Returns the source of the statement being processed.  This can be used to
135         implement relative paths in include-like statements.  Note that the source
136         may not necessarily be a file.
137         */
138         const std::string &get_source() const
139         {
140                 if(!cur_st)
141                         throw InvalidState("get_source called without current statement");
142                 return cur_st->source;
143         }
144
145         virtual void finish() { }
146 private:
147         typedef std::map<std::string, LoaderAction *> ActionMap;
148
149         ActionMap       actions;
150         const Statement *cur_st;
151
152         void add(const std::string &, LoaderAction *);
153         void load_statement(const Statement &st);
154 };
155
156
157 /**
158 Provides the basic functionality of an object loader.  Deriving from this
159 allows loading values directly into member variables of the objects.
160 */
161 template<typename O>
162 class BasicLoader: public Loader
163 {
164 public:
165         typedef O Object;
166
167 protected:
168         O &obj;
169
170 public:
171         BasicLoader(O &o): obj(o) { }
172         O &get_object() const { return obj; }
173 };
174
175
176 /**
177 Provides functionality for loading objects with a Collection.  Deriving from
178 this allows loading pointers to objects in the collection automatically.
179 */
180 template<typename O, typename C>
181 class BasicLoader2: public BasicLoader<O>
182 {
183 public:
184         typedef C Collection;
185
186 protected:
187         C &coll;
188
189 public:
190         BasicLoader2(O &o, C &c): BasicLoader<O>(o), coll(c) { }
191         C &get_collection() const { return coll; }
192 };
193
194
195 /**
196 Loads an object from a file.  The object must have a public Loader class.
197 */
198 template<typename T>
199 void load(T &obj, const std::string &fn)
200 {
201         IO::File in(fn);
202         IO::Buffered buf(in);
203
204         Parser parser(buf, fn);
205         typename T::Loader loader(obj);
206         loader.load(parser);
207 }
208
209 template<typename T, typename U>
210 void load(T &obj, const std::string &fn, U &arg)
211 {
212         IO::File in(fn);
213         IO::Buffered buf(in);
214
215         Parser parser(buf, fn);
216         typename T::Loader loader(obj, arg);
217         loader.load(parser);
218 }
219
220 } // namespace DataFile
221 } // namespace Msp
222
223 #endif