]> git.tdb.fi Git - libs/datafile.git/blob - source/loader.h
Create improvement replacements for BasicLoader* in objectloader.h
[libs/datafile.git] / source / loader.h
1 /* $Id$
2
3 This file is part of libmspdatafile
4 Copyright © 2006-2008, 2010  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         struct ActionKey
48         {
49                 std::string keyword;
50                 std::string signature;
51
52                 ActionKey(const std::string &, const std::string &);
53
54                 bool operator<(const ActionKey &) const;
55         };
56
57         typedef std::map<ActionKey, LoaderAction *> ActionMap;
58
59         ActionMap       actions;
60         const Statement *cur_st;
61
62 protected:
63         Loader(): cur_st(0) { }
64 public:
65         virtual ~Loader();
66
67         /** Loads statements from a parser. */
68         void load(Parser &p);
69
70 private:
71         /** Loads data from a statement. */
72         void load(const Statement &st);
73
74         /** Processes a single statement */
75         void load_statement(const Statement &st);
76
77 protected:
78         /** Loads a sub-object from the statement being processed.  The Loader class
79         of the sub-object is automatically used. */
80         template<typename S>
81         void load_sub(S &s)
82         {
83                 typename S::Loader ldr(s);
84                 load_sub_with(ldr);
85         }
86
87         /** Loads a sub-object from the statement being processed with an extra
88         parameter for the Loader.  The Loader class of the sub-object is
89         automatically used. */
90         template<typename S, typename T>
91         void load_sub(S &s, T &p)
92         {
93                 typename S::Loader ldr(s, p);
94                 load_sub_with(ldr);
95         }
96
97         /** Processes the current statement's substatements with another Loader. */
98         void load_sub_with(Loader &);
99
100         /** Adds a keyword that is loaded by calling a function. */
101         template<typename L>
102         void add(const std::string &k, void (L::*func)())
103         { add(k, new LoaderFunc0<L>(func)); }
104
105         template<typename L, typename A0>
106         void add(const std::string &k, void (L::*func)(A0))
107         { add(k, new LoaderFunc1<L, A0>(func)); }
108
109         template<typename L, typename A0, typename A1>
110         void add(const std::string &k, void (L::*func)(A0, A1))
111         { add(k, new LoaderFunc2<L, A0, A1>(func)); }
112
113         template<typename L, typename A0, typename A1, typename A2>
114         void add(const std::string &k, void (L::*func)(A0, A1, A2))
115         { add(k, new LoaderFunc3<L, A0, A1, A2>(func)); }
116
117         template<typename L, typename A0, typename A1, typename A2, typename A3>
118         void add(const std::string &k, void (L::*func)(A0, A1, A2, A3))
119         { add(k, new LoaderFunc4<L, A0, A1, A2, A3>(func)); }
120
121         template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
122         void add(const std::string &k, void (L::*func)(A0, A1, A2, A3, A4))
123         { add(k, new LoaderFunc5<L, A0, A1, A2, A3, A4>(func)); }
124
125         /** Adds a keyword that is loaded into a member of the loaded object. */
126         template<typename L, typename T0>
127         void add(const std::string &k, T0 L::*p0)
128         { add(k, new LoadValue1<L, T0>(p0)); }
129
130         template<typename L, typename T0, typename T1>
131         void add(const std::string &k, T0 L::*p0, T1 L::*p1)
132         { add(k, new LoadValue2<L, T0, T1>(p0, p1)); }
133
134         /** Adds a keyword that is recognized but ignored. */
135         void add(const std::string &k)
136         { add(k, 0); }
137
138 private:
139         void add(const std::string &, LoaderAction *);
140
141         LoaderAction *find_action(const ActionKey &) const;
142
143 protected:
144         /** Returns the source of the statement being processed.  This can be used
145         to implement relative paths in include-like statements.  Note that the
146         source may not necessarily be a file. */
147         const std::string &get_source() const
148         {
149                 if(!cur_st)
150                         throw InvalidState("get_source called without current statement");
151                 return cur_st->source;
152         }
153
154         virtual void finish() { }
155 };
156
157
158 /**
159 Deprecated.  See ObjectLoader in objectloader.h.
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 Deprecated.  See CollectionObjectLoader in objectloader.h.
178 */
179 template<typename O, typename C>
180 class BasicLoader2: public BasicLoader<O>
181 {
182 public:
183         typedef C Collection;
184
185 protected:
186         C &coll;
187
188 public:
189         BasicLoader2(O &o, C &c): BasicLoader<O>(o), coll(c) { }
190         C &get_collection() const { return coll; }
191 };
192
193
194 /**
195 Loads an object from a file.  The object must have a public Loader class.
196 */
197 template<typename T>
198 void load(T &obj, const std::string &fn)
199 {
200         IO::File in(fn);
201         IO::Buffered buf(in);
202
203         Parser parser(buf, fn);
204         typename T::Loader loader(obj);
205         loader.load(parser);
206 }
207
208 template<typename T, typename U>
209 void load(T &obj, const std::string &fn, U &arg)
210 {
211         IO::File in(fn);
212         IO::Buffered buf(in);
213
214         Parser parser(buf, fn);
215         typename T::Loader loader(obj, arg);
216         loader.load(parser);
217 }
218
219 } // namespace DataFile
220 } // namespace Msp
221
222 #endif