]> git.tdb.fi Git - libs/datafile.git/blob - source/loader.h
Convenience function for reporting errors in loaded data
[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 "parser.h"
8 #include "statement.h"
9
10 namespace Msp {
11 namespace DataFile {
12
13 /**
14 Base class for data loaders.  This class only provides core functionality.
15 You'll almost certainly want to use one of the BasicLoader classes instead.
16
17 Under normal circumstances, a class capable of being loaded should have a
18 nested typed called Loader which resolves to a descendant of this class.  If
19 another structure is used, the loader object must be constructed manually.
20
21 A loader class should execute one or more calls to the various add() functions
22 to register actions with expected keywords.  Currently possible actions are
23 calling a function of the loader, storing values in member variables of an
24 object and ignoring the statement.  If a unexpected keyword is encountered, an
25 exception is thrown and the loading is aborted.
26
27 A sub-object can be loaded with one of the load_sub functions.
28
29 When loading has finished successfully, the virtual function finish() is
30 called.  Any post-processing of the data should be placed here and not in the
31 destructor.
32
33 See also classes ObjectLoader and CollectionObjectLoader in objectloader.h.
34 */
35 class Loader
36 {
37 private:
38         struct ActionKey
39         {
40                 std::string keyword;
41                 std::string signature;
42
43                 ActionKey(const std::string &, const std::string &);
44
45                 bool operator<(const ActionKey &) const;
46         };
47
48         typedef std::map<ActionKey, LoaderAction *> ActionMap;
49
50         ActionMap actions;
51         const Statement *cur_st;
52         bool sub_loaded;
53 protected:
54         bool check_sub_loads;
55
56         Loader();
57 public:
58         virtual ~Loader();
59
60         /** Loads statements from a parser. */
61         void load(Parser &p);
62
63 private:
64         /** Loads data from a statement. */
65         void load(const Statement &st);
66
67         /** Processes a single statement */
68         void load_statement(const Statement &st);
69
70 protected:
71         /** Loads a sub-object from the statement being processed.  The Loader class
72         of the sub-object is automatically used. */
73         template<typename S>
74         void load_sub(S &s)
75         {
76                 typename S::Loader ldr(s);
77                 load_sub_with(ldr);
78         }
79
80         /** Loads a sub-object from the statement being processed with an extra
81         parameter for the Loader.  The Loader class of the sub-object is
82         automatically used. */
83         template<typename S, typename T>
84         void load_sub(S &s, T &p)
85         {
86                 typename S::Loader ldr(s, p);
87                 load_sub_with(ldr);
88         }
89
90         /** Processes the current statement's substatements with another Loader. */
91         void load_sub_with(Loader &);
92
93         /** Adds a keyword that is loaded by calling a function. */
94         template<typename L>
95         void add(const std::string &k, void (L::*func)())
96         { add(k, new LoaderFunc0<L>(func)); }
97
98         template<typename L, typename A0>
99         void add(const std::string &k, void (L::*func)(A0))
100         { add(k, new LoaderFunc1<L, A0>(func)); }
101
102         template<typename L, typename A0, typename A1>
103         void add(const std::string &k, void (L::*func)(A0, A1))
104         { add(k, new LoaderFunc2<L, A0, A1>(func)); }
105
106         template<typename L, typename A0, typename A1, typename A2>
107         void add(const std::string &k, void (L::*func)(A0, A1, A2))
108         { add(k, new LoaderFunc3<L, A0, A1, A2>(func)); }
109
110         template<typename L, typename A0, typename A1, typename A2, typename A3>
111         void add(const std::string &k, void (L::*func)(A0, A1, A2, A3))
112         { add(k, new LoaderFunc4<L, A0, A1, A2, A3>(func)); }
113
114         template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
115         void add(const std::string &k, void (L::*func)(A0, A1, A2, A3, A4))
116         { add(k, new LoaderFunc5<L, A0, A1, A2, A3, A4>(func)); }
117
118         /** Adds a keyword that is loaded into a member of the loaded object. */
119         template<typename L, typename T0>
120         void add(const std::string &k, T0 L::*p0)
121         { add(k, new LoadValue1<L, T0>(p0)); }
122
123         template<typename L, typename T0, typename T1>
124         void add(const std::string &k, T0 L::*p0, T1 L::*p1)
125         { add(k, new LoadValue2<L, T0, T1>(p0, p1)); }
126
127         /** Adds a keyword that is recognized but ignored. */
128         void add(const std::string &k)
129         { add(k, 0); }
130
131 private:
132         void add(const std::string &, LoaderAction *);
133
134         LoaderAction *find_action(const ActionKey &) const;
135
136 protected:
137         /** Returns the source of the statement being processed.  This can be used
138         to implement relative paths in include-like statements.  Note that the
139         source may not necessarily be a file. */
140         const std::string &get_source() const;
141
142         /** Throws a data_error from the current line. */
143         void error(const std::string &) const;
144
145         virtual void finish() { }
146 };
147
148
149 /**
150 Loads an object from a file.  The object must have a public Loader class.
151 */
152 template<typename T>
153 void load(T &obj, const std::string &fn)
154 {
155         IO::BufferedFile in(fn);
156
157         Parser parser(in, fn);
158         typename T::Loader loader(obj);
159         loader.load(parser);
160 }
161
162 template<typename T, typename U>
163 void load(T &obj, const std::string &fn, U &arg)
164 {
165         IO::BufferedFile in(fn);
166
167         Parser parser(in, fn);
168         typename T::Loader loader(obj, arg);
169         loader.load(parser);
170 }
171
172 } // namespace DataFile
173 } // namespace Msp
174
175 #endif