1 #ifndef MSP_DATAFILE_LOADER_H_
2 #define MSP_DATAFILE_LOADER_H_
5 #include <msp/io/file.h>
7 #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.
42 std::string signature;
44 ActionKey(const std::string &, const std::string &);
46 bool operator<(const ActionKey &) const;
49 typedef std::map<ActionKey, LoaderAction *> ActionMap;
52 const Statement *cur_st;
61 /** Loads statements from a parser. */
65 /** Loads data from a statement. */
66 void load(const Statement &st);
68 /** Processes a single statement */
69 void load_statement(const Statement &st);
72 /** Loads a sub-object from the statement being processed. The Loader class
73 of the sub-object is automatically used. */
77 typename S::Loader ldr(s);
81 /** Loads a sub-object from the statement being processed with an extra
82 parameter for the Loader. The Loader class of the sub-object is
83 automatically used. */
84 template<typename S, typename T>
85 void load_sub(S &s, T &p)
87 typename S::Loader ldr(s, p);
91 /** Processes the current statement's substatements with another Loader. */
92 void load_sub_with(Loader &);
94 /** Adds a keyword that is loaded by calling a function. */
96 void add(const std::string &k, void (L::*func)())
97 { add(k, new LoaderFunc0<L>(func)); }
99 template<typename L, typename A0>
100 void add(const std::string &k, void (L::*func)(A0))
101 { add(k, new LoaderFunc1<L, A0>(func)); }
103 template<typename L, typename A0, typename A1>
104 void add(const std::string &k, void (L::*func)(A0, A1))
105 { add(k, new LoaderFunc2<L, A0, A1>(func)); }
107 template<typename L, typename A0, typename A1, typename A2>
108 void add(const std::string &k, void (L::*func)(A0, A1, A2))
109 { add(k, new LoaderFunc3<L, A0, A1, A2>(func)); }
111 template<typename L, typename A0, typename A1, typename A2, typename A3>
112 void add(const std::string &k, void (L::*func)(A0, A1, A2, A3))
113 { add(k, new LoaderFunc4<L, A0, A1, A2, A3>(func)); }
115 template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
116 void add(const std::string &k, void (L::*func)(A0, A1, A2, A3, A4))
117 { add(k, new LoaderFunc5<L, A0, A1, A2, A3, A4>(func)); }
119 /** Adds a keyword that is loaded into a member of the loaded object. */
120 template<typename L, typename T0>
121 void add(const std::string &k, T0 L::*p0)
122 { add(k, new LoadValue1<L, T0>(p0)); }
124 template<typename L, typename T0, typename T1>
125 void add(const std::string &k, T0 L::*p0, T1 L::*p1)
126 { add(k, new LoadValue2<L, T0, T1>(p0, p1)); }
128 /** Adds a keyword that is recognized but ignored. */
129 void add(const std::string &k)
133 void add(const std::string &, LoaderAction *);
135 LoaderAction *find_action(const ActionKey &) const;
138 /** Returns the source of the statement being processed. This can be used
139 to implement relative paths in include-like statements. Note that the
140 source may not necessarily be a file. */
141 const std::string &get_source() const
144 throw InvalidState("get_source called without current statement");
145 return cur_st->source;
148 virtual void finish() { }
153 Loads an object from a file. The object must have a public Loader class.
156 void load(T &obj, const std::string &fn)
158 IO::BufferedFile in(fn);
160 Parser parser(in, fn);
161 typename T::Loader loader(obj);
165 template<typename T, typename U>
166 void load(T &obj, const std::string &fn, U &arg)
168 IO::BufferedFile in(fn);
170 Parser parser(in, fn);
171 typename T::Loader loader(obj, arg);
175 } // namespace DataFile