]> git.tdb.fi Git - libs/datafile.git/blob - source/loaderaction.h
Move LoaderActions to loaderaction.h
[libs/datafile.git] / source / loaderaction.h
1 /* $Id$
2
3 This file is part of libmspdatafile
4 Copyright © 2006-2008  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_DATAFILE_LOADERACTION_H_
9 #define MSP_DATAFILE_LOADERACTION_H_
10
11 #include "except.h"
12 #include "statement.h"
13
14 namespace Msp {
15 namespace DataFile {
16
17 class Loader;
18
19 /**
20 Base class for loader actions.
21 */
22 class LoaderAction
23 {
24 public:
25         /**
26         Called when a statement is to be loaded.
27         */
28         virtual void execute(Loader &, const Statement &) const=0;
29         virtual ~LoaderAction() { }
30 protected:
31         LoaderAction() { }
32 };
33
34
35 /**
36 Loads a statement by calling a function that takes no arguments.
37 */
38 template<typename L>
39 class LoaderFunc0: public LoaderAction
40 {
41 public:
42         typedef void (L::*FuncType)();
43
44         LoaderFunc0(FuncType f): func(f) { }
45         void execute(Loader &l, const Statement &st) const
46         {
47                 if(st.args.size()!=0) throw TypeError("Wrong number of arguments");
48                 (dynamic_cast<L &>(l).*func)();
49         };
50 private:
51         FuncType func;
52 };
53
54
55 /**
56 Loads a statement by calling a function that takes one argument.
57 */
58 template<typename L, typename A0>
59 class LoaderFunc1: public LoaderAction
60 {
61 public:
62         typedef void (L::*FuncType)(A0);
63
64         LoaderFunc1(FuncType f): func(f) { }
65         void execute(Loader &l, const Statement &st) const
66         {
67                 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
68                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>());
69         }
70 private:
71         FuncType func;
72 };
73
74
75 /**
76 Loads a statement by calling a function that takes an array of values.
77 */
78 template<typename L, typename A0>
79 class LoaderFunc1<L, const std::vector<A0> &>: public LoaderAction
80 {
81 public:
82         typedef void (L::*FuncType)(const std::vector<A0> &);
83
84         LoaderFunc1(FuncType f): func(f) { }
85         void execute(Loader &l, const Statement &st) const
86         {
87                 std::vector<A0> values;
88                 values.reserve(st.args.size());
89                 for(ValueArray::const_iterator i=st.args.begin(); i!=st.args.end(); ++i)
90                         values.push_back(i->get<A0>());
91                 (dynamic_cast<L &>(l).*func)(values);
92         }
93 private:
94         FuncType func;
95 };
96
97
98 template<typename L, typename A0, typename A1>
99 class LoaderFunc2: public LoaderAction
100 {
101 public:
102         typedef void (L::*FuncType)(A0, A1);
103
104         LoaderFunc2(FuncType f): func(f) { }
105         void execute(Loader &l, const Statement &st) const
106         {
107                 if(st.args.size()!=2) throw TypeError("Wrong number of arguments");
108                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>());
109         }
110 private:
111         FuncType func;
112 };
113
114
115 template<typename L, typename A0, typename A1, typename A2>
116 class LoaderFunc3: public LoaderAction
117 {
118 public:
119         typedef void (L::*FuncType)(A0, A1, A2);
120
121         LoaderFunc3(FuncType f): func(f) { }
122         void execute(Loader &l, const Statement &st) const
123         {
124                 if(st.args.size()!=3) throw TypeError("Wrong number of arguments");
125                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>());
126         }
127 private:
128         FuncType func;
129 };
130
131
132 template<typename L, typename A0, typename A1, typename A2, typename A3>
133 class LoaderFunc4: public LoaderAction
134 {
135 public:
136         typedef void (L::*FuncType)(A0, A1, A2, A3);
137
138         LoaderFunc4(FuncType f): func(f) { }
139         void execute(Loader &l, const Statement &st) const
140         {
141                 if(st.args.size()!=4) throw TypeError("Wrong number of arguments");
142                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>(), st.args[3].get<A3>());
143         }
144 private:
145         FuncType func;
146 };
147
148
149 template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
150 class LoaderFunc5: public LoaderAction
151 {
152 public:
153         typedef void (L::*FuncType)(A0, A1, A2, A3, A4);
154
155         LoaderFunc5(FuncType f): func(f) { }
156         void execute(Loader &l, const Statement &st) const
157         {
158                 if(st.args.size()!=5) throw TypeError("Wrong number of arguments");
159                 (dynamic_cast<L &>(l).*func)(st.args[0].get<A0>(), st.args[1].get<A1>(), st.args[2].get<A2>(), st.args[3].get<A3>(), st.args[4].get<A4>());
160         }
161 private:
162         FuncType func;
163 };
164
165
166 template<typename L, typename T0>
167 class LoadValue1: public LoaderAction
168 {
169 public:
170         typedef T0 L::*Pointer0Type;
171
172         LoadValue1(Pointer0Type p0): ptr0(p0) { }
173         void execute(Loader &l, const Statement &st) const
174         {
175                 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
176                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
177         }
178 private:
179         Pointer0Type ptr0;
180 };
181
182
183 template<typename L, typename T0>
184 class LoadValue1<L, T0 *>: public LoaderAction
185 {
186 public:
187         typedef T0 *L::*Pointer0Type;
188
189         LoadValue1(Pointer0Type p0): ptr0(p0) { }
190         void execute(Loader &l, const Statement &st) const
191         {
192                 if(st.args.size()!=1) throw TypeError("Wrong number of arguments");
193                 typename L::Loader &ldr=dynamic_cast<typename L::Loader &>(l);
194                 ldr.get_object().*ptr0=ldr.get_collection().template get<T0>(st.args[0].get<std::string>());
195         }
196 private:
197         Pointer0Type ptr0;
198 };
199
200
201 template<typename L, typename T0, typename T1>
202 class LoadValue2: public LoaderAction
203 {
204 public:
205         typedef T0 L::*Pointer0Type;
206         typedef T1 L::*Pointer1Type;
207
208         LoadValue2(Pointer0Type p0, Pointer1Type p1): ptr0(p0), ptr1(p1) { }
209         void execute(Loader &l, const Statement &st) const
210         {
211                 if(st.args.size()!=2) throw TypeError("Wrong number of arguments");
212                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr0=st.args[0].get<T0>();
213                 dynamic_cast<typename L::Loader &>(l).get_object().*ptr1=st.args[1].get<T1>();
214         }
215 private:
216         Pointer0Type ptr0;
217         Pointer1Type ptr1;
218 };
219
220 } // namespace DataFile
221 } // namespace Msp
222
223 #endif