]> Dogcows Code - chaz/yoink/blob - src/serializable.hh
beginnings of scene rendering
[chaz/yoink] / src / serializable.hh
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #ifndef _SERIALIZABLE_HH_
30 #define _SERIALIZABLE_HH_
31
32 #include <iostream>
33 #include <string>
34 #include <map>
35 #include <vector>
36
37 #include "stringtools.hh"
38 #include "serializer.hh"
39 #include "deserializer.hh"
40
41
42 namespace dc {
43
44
45 class serializable
46 {
47 public:
48 virtual ~serializable();
49
50 virtual void serialize(serializer& out) const = 0;
51 virtual void deserialize(deserializer& in) = 0;
52
53 virtual void print() const = 0;
54
55 virtual bool get(long& value);
56 virtual bool get(double& value);
57 virtual bool get(bool& value);
58 virtual bool get(std::string& value);
59 virtual bool get(std::wstring& value);
60 virtual bool get(std::vector<serializable_ptr>& value);
61 virtual bool get(std::map<std::string,serializable_ptr>& value);
62
63 /*
64 * To get a number value which may have been parsed as either an integer or
65 * double, use these instead.
66 */
67
68 bool getNumber(long&);
69 bool getNumber(double&);
70
71 virtual bool isNull();
72 };
73
74
75 template <class T>
76 class wrapper : public serializable
77 {
78 public:
79 wrapper() {}
80 wrapper(const T& var) : variable(var) {}
81
82 void serialize(serializer& out) const;
83 void deserialize(deserializer& in);
84
85 void print() const;
86 bool get(T& value);
87 public:
88 T variable;
89 };
90
91
92 class null : public serializable
93 {
94 public:
95 null() {}
96 void serialize(serializer& out) const;
97 void deserialize(deserializer& in);
98
99 void print() const;
100 bool isNull();
101 };
102
103
104 typedef wrapper<long> wrapped_integer;
105 typedef wrapper<double> wrapped_real;
106 typedef wrapper<bool> wrapped_boolean;
107 typedef wrapper<std::string> wrapped_string;
108 typedef wrapper<std::wstring> wrapped_wstring;
109 typedef wrapper<std::vector<serializable_ptr> > wrapped_array;
110 typedef wrapper<std::map<std::string,serializable_ptr> > wrapped_dictionary;
111
112
113
114 template <class T>
115 inline void wrapper<T>::serialize(serializer& out) const
116 {
117 out.push(variable);
118 }
119
120 template <>
121 inline void
122 wrapper<std::vector<serializable_ptr> >::serialize(serializer& out) const
123 {
124 out.pushArrayHead();
125
126 std::vector<serializable_ptr>::const_iterator i;
127 for (i = variable.begin(); i < variable.end(); i++)
128 {
129 (*i)->serialize(out);
130 }
131
132 out.pushArrayTail();
133 }
134
135 template <>
136 inline void
137 wrapper<std::map<std::string,serializable_ptr> >::serialize(serializer& out) const
138 {
139 out.pushMapHead();
140
141 std::map<std::string,serializable_ptr>::const_iterator i;
142 for (i = variable.begin(); i != variable.end(); i++)
143 {
144 out.push((*i).first);
145 (*i).second->serialize(out);
146 }
147
148 out.pushMapTail();
149 }
150
151 inline void null::serialize(serializer& out) const
152 {
153 out.pushNull();
154 }
155
156
157 template <class T>
158 inline void wrapper<T>::deserialize(deserializer& in)
159 {
160 in.pop();
161 }
162
163 template <>
164 inline void wrapper<std::vector<serializable_ptr> >::deserialize(deserializer& in)
165 {
166 serializable_ptr obj;
167
168 in.pop();
169
170 while (obj = in.deserialize())
171 {
172 variable.push_back(serializable_ptr(obj));
173 }
174
175 in.pop();
176 }
177
178 template <>
179 inline void
180 wrapper<std::map<std::string,serializable_ptr> >::deserialize(deserializer& in)
181 {
182 serializable_ptr obj;
183
184 in.pop();
185
186 while (obj = in.deserialize())
187 {
188 std::string key;
189 if (obj->get(key))
190 {
191 variable[key] = in.deserialize();
192 }
193 }
194
195 in.pop();
196 }
197
198 inline void null::deserialize(deserializer& in)
199 {
200 in.pop();
201 }
202
203
204 template <class T>
205 inline void wrapper<T>::print() const
206 {
207 std::cout << std::boolalpha << typeid(T).name() << "(" << variable << ")";
208 }
209
210 template <>
211 inline void wrapper<std::wstring>::print() const
212 {
213 std::wcout << variable;
214 }
215
216 template <>
217 inline void wrapper<std::vector<serializable_ptr> >::print() const
218 {
219 std::cout << "array";
220 }
221
222 template <>
223 inline void wrapper<std::map<std::string,serializable_ptr> >::print() const
224 {
225 std::cout << "dictionary";
226 }
227
228 inline void null::print() const
229 {
230 std::cout << "null";
231 }
232
233
234 template <class T>
235 inline bool wrapper<T>::get(T& value)
236 {
237 value = variable;
238 return true;
239 }
240
241 inline bool null::isNull()
242 {
243 return true;
244 }
245
246
247 } // namespace dc
248
249 #endif // _SERIALIZABLE_HH_
250
251 /** vim: set ts=4 sw=4 tw=80: *************************************************/
252
This page took 0.041912 seconds and 4 git commands to generate.