]> git.tdb.fi Git - libs/datafile.git/blob - source/loader.h
Use a common StatementKey structure for Loader and BinaryParser/Writer
[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         typedef std::map<StatementKey, LoaderAction *> ActionMap;
39
40         ActionMap actions;
41         const Statement *cur_st;
42         bool sub_loaded;
43         std::list<Loader *> aux_loaders;
44 protected:
45         bool check_sub_loads;
46
47         Loader();
48 public:
49         virtual ~Loader();
50
51         /** Loads statements from a parser. */
52         void load(Parser &p);
53
54 private:
55         /** Loads data from a statement. */
56         void load(const Statement &st);
57
58         /** Processes a single statement */
59         void load_statement(const Statement &st);
60
61 protected:
62         /** Loads a sub-object from the statement being processed.  The Loader class
63         of the sub-object is automatically used. */
64         template<typename S>
65         void load_sub(S &s)
66         {
67                 typename S::Loader ldr(s);
68                 load_sub_with(ldr);
69         }
70
71         /** Loads a sub-object from the statement being processed with an extra
72         parameter for the Loader.  The Loader class of the sub-object is
73         automatically used. */
74         template<typename S, typename T>
75         void load_sub(S &s, T &p)
76         {
77                 typename S::Loader ldr(s, p);
78                 load_sub_with(ldr);
79         }
80
81         /** Processes the current statement's substatements with another Loader. */
82         void load_sub_with(Loader &);
83
84         /** Adds a keyword that is loaded by calling a function. */
85         template<typename L>
86         void add(const std::string &k, void (L::*func)())
87         { add(k, new LoaderFunc0<L>(func)); }
88
89         template<typename L, typename A0>
90         void add(const std::string &k, void (L::*func)(A0))
91         { add(k, new LoaderFunc1<L, A0>(func)); }
92
93         template<typename L, typename A0, typename A1>
94         void add(const std::string &k, void (L::*func)(A0, A1))
95         { add(k, new LoaderFunc2<L, A0, A1>(func)); }
96
97         template<typename L, typename A0, typename A1, typename A2>
98         void add(const std::string &k, void (L::*func)(A0, A1, A2))
99         { add(k, new LoaderFunc3<L, A0, A1, A2>(func)); }
100
101         template<typename L, typename A0, typename A1, typename A2, typename A3>
102         void add(const std::string &k, void (L::*func)(A0, A1, A2, A3))
103         { add(k, new LoaderFunc4<L, A0, A1, A2, A3>(func)); }
104
105         template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
106         void add(const std::string &k, void (L::*func)(A0, A1, A2, A3, A4))
107         { add(k, new LoaderFunc5<L, A0, A1, A2, A3, A4>(func)); }
108
109         /** Adds a keyword that is loaded into a member of the loaded object. */
110         template<typename L, typename T0>
111         void add(const std::string &k, T0 L::*p0)
112         { add(k, new LoadValue1<L, T0>(p0)); }
113
114         template<typename L, typename T0, typename T1>
115         void add(const std::string &k, T0 L::*p0, T1 L::*p1)
116         { add(k, new LoadValue2<L, T0, T1>(p0, p1)); }
117
118         /** Adds a keyword that is recognized but ignored. */
119         void add(const std::string &k)
120         { add(k, 0); }
121
122 private:
123         void add(const std::string &, LoaderAction *);
124
125 protected:
126         void add_auxiliary_loader(Loader &);
127
128 private:
129         bool has_action(const StatementKey &) const;
130         LoaderAction *find_action(const StatementKey &) const;
131
132 protected:
133         /** Returns the source of the statement being processed.  This can be used
134         to implement relative paths in include-like statements.  Note that the
135         source may not necessarily be a file. */
136         const std::string &get_source() const;
137
138         virtual void finish() { }
139 };
140
141
142 /**
143 Loads an object from a file.  The object must have a public Loader class.
144 */
145 template<typename T>
146 void load(T &obj, const std::string &fn)
147 {
148         IO::BufferedFile in(fn);
149
150         Parser parser(in, fn);
151         typename T::Loader loader(obj);
152         loader.load(parser);
153 }
154
155 template<typename T, typename U>
156 void load(T &obj, const std::string &fn, U &arg)
157 {
158         IO::BufferedFile in(fn);
159
160         Parser parser(in, fn);
161         typename T::Loader loader(obj, arg);
162         loader.load(parser);
163 }
164
165 } // namespace DataFile
166 } // namespace Msp
167
168 #endif