]> git.tdb.fi Git - libs/datafile.git/blob - source/loader.h
Minor style/typo fixes
[libs/datafile.git] / source / loader.h
1 #ifndef MSP_DATAFILE_LOADER_H_
2 #define MSP_DATAFILE_LOADER_H_
3
4 #include <map>
5 #include <msp/io/file.h>
6 #include "loaderaction.h"
7 #include "meta.h"
8 #include "parser.h"
9 #include "statement.h"
10
11 namespace Msp {
12 namespace DataFile {
13
14 /**
15 Base class for data loaders.  This class only provides core functionality.
16 You'll almost certainly want to use one of the BasicLoader classes instead.
17
18 Under normal circumstances, a class capable of being loaded should have a
19 nested typed called Loader which resolves to a descendant of this class.  If
20 another structure is used, the loader object must be constructed manually.
21
22 A loader class should execute one or more calls to the various add() functions
23 to register actions with expected keywords.  Currently possible actions are
24 calling a function of the loader, storing values in member variables of an
25 object and ignoring the statement.  If a unexpected keyword is encountered, an
26 exception is thrown and the loading is aborted.
27
28 A sub-object can be loaded with one of the load_sub functions.
29
30 When loading has finished successfully, the virtual function finish() is
31 called.  Any post-processing of the data should be placed here and not in the
32 destructor.
33
34 See also classes ObjectLoader and CollectionObjectLoader in objectloader.h.
35 */
36 class Loader: private NonCopyable
37 {
38 protected:
39         class ActionMap: public std::map<StatementKey, LoaderAction *>, private NonCopyable
40         {
41         public:
42                 ~ActionMap();
43         };
44
45 private:
46         ActionMap local_actions;
47         ActionMap *actions;
48         Parser *cur_parser;
49         unsigned cur_level;
50         const Statement *cur_st;
51         bool sub_loaded;
52         bool direct;
53         std::vector<Loader *> aux_loaders;
54 protected:
55         bool check_sub_loads;
56
57         Loader();
58 public:
59         virtual ~Loader() { }
60
61         /** Loads statements from a parser. */
62         void load(Parser &p);
63
64 private:
65         /** Loads data from a statement. */
66         void load(const Statement &st);
67
68         /** Loads statemsnts from a parser, feeding them directly to actions. */
69         void load_direct(Parser &, unsigned);
70
71         /** Processes a single statement */
72         void load_statement(const Statement &st);
73
74 protected:
75         /** Loads a sub-object from the statement being processed.  The Loader class
76         of the sub-object is automatically used. */
77         template<typename S>
78         void load_sub(S &s)
79         {
80                 typename S::Loader ldr(s);
81                 load_sub_with(ldr);
82         }
83
84         /** Loads a sub-object from the statement being processed with an extra
85         parameter for the Loader.  The Loader class of the sub-object is
86         automatically used. */
87         template<typename S, typename T>
88         void load_sub(S &s, T &p)
89         {
90                 typename S::Loader ldr(s, p);
91                 load_sub_with(ldr);
92         }
93
94         /** Processes the current statement's substatements with another Loader. */
95         void load_sub_with(Loader &);
96
97         /** Sets the actions to be used when loading.  If the map is empty,
98         init_actions will be called. */
99         void set_actions(ActionMap &);
100         virtual void init_actions() { }
101
102         /** Adds a keyword that is loaded by calling a function. */
103         template<typename L>
104         void add(const std::string &k, void (L::*func)())
105         { add(k, new LoaderFunc0<L>(func)); }
106
107         template<typename L, typename A0>
108         void add(const std::string &k, void (L::*func)(A0))
109         { add(k, new LoaderFunc1<L, A0>(func)); }
110
111 #if __cplusplus>=201103L
112         template<typename L, typename... Args>
113         void add(const std::string &k, void (L::*func)(Args...))
114         { add(k, new LoaderFuncN<L, Args...>(func)); }
115
116 #else
117         template<typename L, typename A0, typename A1>
118         void add(const std::string &k, void (L::*func)(A0, A1))
119         { add(k, new LoaderFunc2<L, A0, A1>(func)); }
120
121         template<typename L, typename A0, typename A1, typename A2>
122         void add(const std::string &k, void (L::*func)(A0, A1, A2))
123         { add(k, new LoaderFunc3<L, A0, A1, A2>(func)); }
124
125         template<typename L, typename A0, typename A1, typename A2, typename A3>
126         void add(const std::string &k, void (L::*func)(A0, A1, A2, A3))
127         { add(k, new LoaderFunc4<L, A0, A1, A2, A3>(func)); }
128
129         template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
130         void add(const std::string &k, void (L::*func)(A0, A1, A2, A3, A4))
131         { add(k, new LoaderFunc5<L, A0, A1, A2, A3, A4>(func)); }
132 #endif
133
134 #if __cplusplus>=201103L
135         /** Adds a keyword that is loaded by calling a function with a bound
136         first argument. */
137         template<typename L, typename B0, typename... Args>
138         void add(const std::string &k, void (L::*func)(B0, Args...), const typename RemoveReference<B0>::Type &b0)
139         { add(k, new LoaderFuncNBound1<L, B0, Args...>(func, b0)); }
140 #endif
141
142         /** Adds a keyword that is loaded into a member of the loaded object. */
143         template<typename L, typename T0>
144         void add(const std::string &k, T0 L::*p0)
145         { add(k, new LoadValue1<L, T0>(p0)); }
146
147         template<typename L, typename T0, typename T1>
148         void add(const std::string &k, T0 L::*p0, T1 L::*p1)
149         { add(k, new LoadValue2<L, T0, T1>(p0, p1)); }
150
151         /** Adds a keyword that is recognized but ignored. */
152         void add(const std::string &k)
153         { add(k, 0); }
154
155 private:
156         void add(const std::string &, LoaderAction *);
157
158 protected:
159         void add_auxiliary_loader(Loader &);
160
161 private:
162         bool has_action(const StatementKey &) const;
163         LoaderAction *find_action(const StatementKey &) const;
164
165 protected:
166         /** Returns the source of the statement being processed.  This can be used
167         to implement relative paths in include-like statements.  Note that the
168         source may not necessarily be a file. */
169         const std::string &get_source() const;
170
171         /** Returns the keyword of the statement being processed.  Can be used to
172         implement dynamic keywords. */
173         const std::string &get_keyword() const;
174
175         virtual void finish() { }
176 };
177
178
179 #if __cplusplus>=201103L
180 /**
181 Loads an object from a file.  The object must have a public Loader class.  Any
182 extra arguments are passed to the Loader constructor.
183 */
184 template<typename T, typename... Args>
185 void load(T &obj, const std::string &fn, Args &... args)
186 {
187         IO::BufferedFile in(fn);
188
189         Parser parser(in, fn);
190         typename T::Loader loader(obj, args...);
191         loader.load(parser);
192 }
193
194 /**
195 Loads an object from a file stored in a collection.  The object must have a
196 public Loader class.  The collection is passed to the Loader constructor,
197 followed by any extra arguments.
198 */
199 template<typename T, typename... Args>
200 void load(T &obj, typename T::Loader::Collection &coll, const std::string &fn, Args &... args)
201 {
202         RefPtr<IO::Seekable> in = coll.open_raw(fn);
203         if(!in)
204                 throw IO::file_not_found(fn);
205
206         Parser parser(*in, fn);
207         typename T::Loader loader(obj, coll, args...);
208         loader.load(parser);
209 }
210
211 /**
212 Loads an object from a file stored in a collection.  The object must havea
213 public Loader class.  Any extra arguments are passed to the Loader constructor.
214 */
215 template<typename T, typename C, typename... Args>
216 typename EnableIf<NeedsCollection<typename T::Loader>::value, void>::No load(T &obj, C &coll, const std::string &fn, Args &... args)
217 {
218         RefPtr<IO::Seekable> in = coll.open_raw(fn);
219         if(!in)
220                 throw IO::file_not_found(fn);
221
222         Parser parser(*in, fn);
223         typename T::Loader loader(obj, args...);
224         loader.load(parser);
225 }
226
227 #else
228
229 /**
230 Loads an object from a file.  The object must have a public Loader class.
231 */
232 template<typename T>
233 void load(T &obj, const std::string &fn)
234 {
235         IO::BufferedFile in(fn);
236
237         Parser parser(in, fn);
238         typename T::Loader loader(obj);
239         loader.load(parser);
240 }
241
242 /**
243 Loads an object from a file, passing one extra argument to the Loader
244 constructor.  The object must have a public Loader class.
245 */
246 template<typename T, typename U>
247 void load(T &obj, const std::string &fn, U &arg)
248 {
249         IO::BufferedFile in(fn);
250
251         Parser parser(in, fn);
252         typename T::Loader loader(obj, arg);
253         loader.load(parser);
254 }
255 #endif
256
257 } // namespace DataFile
258 } // namespace Msp
259
260 #endif