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.
36 class Loader: private NonCopyable
39 class ActionMap: public std::map<StatementKey, LoaderAction *>, private NonCopyable
46 ActionMap local_actions;
50 const Statement *cur_st;
53 std::vector<Loader *> aux_loaders;
61 /** Loads statements from a parser. */
65 /** Loads data from a statement. */
66 void load(const Statement &st);
68 /** Loads statemsnts from a parser, feeding them directly to actions. */
69 void load_direct(Parser &, unsigned);
71 /** Processes a single statement */
72 void load_statement(const Statement &st);
75 /** Loads a sub-object from the statement being processed. The Loader class
76 of the sub-object is automatically used. */
80 typename S::Loader ldr(s);
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)
90 typename S::Loader ldr(s, p);
94 /** Processes the current statement's substatements with another Loader. */
95 void load_sub_with(Loader &);
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() { }
102 /** Adds a keyword that is loaded by calling a function. */
104 void add(const std::string &k, void (L::*func)())
105 { add(k, new LoaderFunc0<L>(func)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
134 #if __cplusplus>=201103L
135 /** Adds a keyword that is loaded by calling a function with a bound
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)); }
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)); }
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)); }
151 /** Adds a keyword that is recognized but ignored. */
152 void add(const std::string &k)
156 void add(const std::string &, LoaderAction *);
159 void add_auxiliary_loader(Loader &);
162 bool has_action(const StatementKey &) const;
163 LoaderAction *find_action(const StatementKey &) const;
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;
171 /** Returns the keyword of the statement being processed. Can be used to
172 implement dynamic keywords. */
173 const std::string &get_keyword() const;
175 virtual void finish() { }
179 #if __cplusplus>=201103L
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.
184 template<typename T, typename... Args>
185 void load(T &obj, const std::string &fn, Args &... args)
187 IO::BufferedFile in(fn);
189 Parser parser(in, fn);
190 typename T::Loader loader(obj, args...);
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.
199 template<typename T, typename... Args>
200 void load(T &obj, typename T::Loader::Collection &coll, const std::string &fn, Args &... args)
202 RefPtr<IO::Seekable> in = coll.open_raw(fn);
204 throw IO::file_not_found(fn);
206 Parser parser(*in, fn);
207 typename T::Loader loader(obj, coll, args...);
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.
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)
218 RefPtr<IO::Seekable> in = coll.open_raw(fn);
220 throw IO::file_not_found(fn);
222 Parser parser(*in, fn);
223 typename T::Loader loader(obj, args...);
230 Loads an object from a file. The object must have a public Loader class.
233 void load(T &obj, const std::string &fn)
235 IO::BufferedFile in(fn);
237 Parser parser(in, fn);
238 typename T::Loader loader(obj);
243 Loads an object from a file, passing one extra argument to the Loader
244 constructor. The object must have a public Loader class.
246 template<typename T, typename U>
247 void load(T &obj, const std::string &fn, U &arg)
249 IO::BufferedFile in(fn);
251 Parser parser(in, fn);
252 typename T::Loader loader(obj, arg);
257 } // namespace DataFile