1 #ifndef MSP_DATAFILE_LOADER_H_
2 #define MSP_DATAFILE_LOADER_H_
5 #include <msp/io/file.h>
6 #include "loaderaction.h"
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.
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.
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.
28 A sub-object can be loaded with one of the load_sub functions.
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
34 See also classes ObjectLoader and CollectionObjectLoader in objectloader.h.
39 typedef std::map<StatementKey, LoaderAction *> ActionMap;
44 const Statement *cur_st;
47 std::vector<Loader *> aux_loaders;
55 /** Loads statements from a parser. */
59 /** Loads data from a statement. */
60 void load(const Statement &st);
62 /** Loads statemsnts from a parser, feeding them directly to actions. */
63 void load_direct(Parser &, unsigned);
65 /** Processes a single statement */
66 void load_statement(const Statement &st);
69 /** Loads a sub-object from the statement being processed. The Loader class
70 of the sub-object is automatically used. */
74 typename S::Loader ldr(s);
78 /** Loads a sub-object from the statement being processed with an extra
79 parameter for the Loader. The Loader class of the sub-object is
80 automatically used. */
81 template<typename S, typename T>
82 void load_sub(S &s, T &p)
84 typename S::Loader ldr(s, p);
88 /** Processes the current statement's substatements with another Loader. */
89 void load_sub_with(Loader &);
91 /** Adds a keyword that is loaded by calling a function. */
93 void add(const std::string &k, void (L::*func)())
94 { add(k, new LoaderFunc0<L>(func)); }
96 template<typename L, typename A0>
97 void add(const std::string &k, void (L::*func)(A0))
98 { add(k, new LoaderFunc1<L, A0>(func)); }
100 #if __cplusplus>=201103L
101 template<typename L, typename... Args>
102 void add(const std::string &k, void (L::*func)(Args...))
103 { add(k, new LoaderFuncN<L, Args...>(func)); }
106 template<typename L, typename A0, typename A1>
107 void add(const std::string &k, void (L::*func)(A0, A1))
108 { add(k, new LoaderFunc2<L, A0, A1>(func)); }
110 template<typename L, typename A0, typename A1, typename A2>
111 void add(const std::string &k, void (L::*func)(A0, A1, A2))
112 { add(k, new LoaderFunc3<L, A0, A1, A2>(func)); }
114 template<typename L, typename A0, typename A1, typename A2, typename A3>
115 void add(const std::string &k, void (L::*func)(A0, A1, A2, A3))
116 { add(k, new LoaderFunc4<L, A0, A1, A2, A3>(func)); }
118 template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
119 void add(const std::string &k, void (L::*func)(A0, A1, A2, A3, A4))
120 { add(k, new LoaderFunc5<L, A0, A1, A2, A3, A4>(func)); }
123 /** Adds a keyword that is loaded into a member of the loaded object. */
124 template<typename L, typename T0>
125 void add(const std::string &k, T0 L::*p0)
126 { add(k, new LoadValue1<L, T0>(p0)); }
128 template<typename L, typename T0, typename T1>
129 void add(const std::string &k, T0 L::*p0, T1 L::*p1)
130 { add(k, new LoadValue2<L, T0, T1>(p0, p1)); }
132 /** Adds a keyword that is recognized but ignored. */
133 void add(const std::string &k)
137 void add(const std::string &, LoaderAction *);
140 void add_auxiliary_loader(Loader &);
143 bool has_action(const StatementKey &) const;
144 LoaderAction *find_action(const StatementKey &) const;
147 /** Returns the source of the statement being processed. This can be used
148 to implement relative paths in include-like statements. Note that the
149 source may not necessarily be a file. */
150 const std::string &get_source() const;
152 /** Returns the keyword of the statement being processed. Can be used to
153 implement dynamic keywords. */
154 const std::string &get_keyword() const;
156 virtual void finish() { }
160 #if __cplusplus>=201103L
162 Loads an object from a file. The object must have a public Loader class. Any
163 extra arguments are passed to the Loader constructor.
165 template<typename T, typename... Args>
166 void load(T &obj, const std::string &fn, Args &... args)
168 IO::BufferedFile in(fn);
170 Parser parser(in, fn);
171 typename T::Loader loader(obj, args...);
176 Loads an object from a file stored in a collection. The object must havea
177 public Loader class. The collection is passed to the Loader constructor,
178 followed by any extra arguments.
180 template<typename T, typename... Args>
181 void load(T &obj, typename T::Loader::Collection &coll, const std::string &fn, Args &... args)
183 RefPtr<IO::Seekable> in = coll.open_raw(fn);
185 throw IO::file_not_found(fn);
187 Parser parser(*in, fn);
188 typename T::Loader loader(obj, coll, args...);
193 Loads an object from a file stored in a collection. The object must havea
194 public Loader class. Any extra arguments are passed to the Loader constructor.
196 template<typename T, typename C, typename... Args>
197 typename EnableIf<NeedsCollection<typename T::Loader>::value, void>::No load(T &obj, C &coll, const std::string &fn, Args &... args)
199 RefPtr<IO::Seekable> in = coll.open_raw(fn);
201 throw IO::file_not_found(fn);
203 Parser parser(*in, fn);
204 typename T::Loader loader(obj, args...);
211 Loads an object from a file. The object must have a public Loader class.
214 void load(T &obj, const std::string &fn)
216 IO::BufferedFile in(fn);
218 Parser parser(in, fn);
219 typename T::Loader loader(obj);
224 Loads an object from a file, passing one extra argument to the Loader
225 constructor. The object must have a public Loader class.
227 template<typename T, typename U>
228 void load(T &obj, const std::string &fn, U &arg)
230 IO::BufferedFile in(fn);
232 Parser parser(in, fn);
233 typename T::Loader loader(obj, arg);
238 } // namespace DataFile