]> Dogcows Code - chaz/yoink/blob - src/serializable.hh
new classes; yajl library
[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 "serializer.hh"
38 #include "deserializer.hh"
39 #include "stringtools.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 virtual bool isNull();
63 };
64
65
66 template <class T>
67 class wrapper : public serializable
68 {
69 public:
70 wrapper() {}
71 wrapper(const T& var) : variable(var) {}
72
73 void serialize(serializer& out) const;
74 void deserialize(deserializer& in);
75
76 void print() const;
77 bool get(T& value);
78
79 public:
80 T variable;
81 };
82
83
84 class null : public serializable
85 {
86 public:
87 null() {}
88 void serialize(serializer& out) const;
89 void deserialize(deserializer& in);
90
91 void print() const;
92 bool isNull();
93 };
94
95
96 typedef wrapper<long> wrapped_integer;
97 typedef wrapper<double> wrapped_real;
98 typedef wrapper<bool> wrapped_boolean;
99 typedef wrapper<std::string> wrapped_string;
100 typedef wrapper<std::wstring> wrapped_wstring;
101 typedef wrapper<std::vector<serializable_ptr> > wrapped_array;
102 typedef wrapper<std::map<std::string,serializable_ptr> > wrapped_dictionary;
103
104
105
106 template <class T>
107 inline void wrapper<T>::serialize(serializer& out) const
108 {
109 out.push(variable);
110 }
111
112 template <>
113 inline void
114 wrapper<std::vector<serializable_ptr> >::serialize(serializer& out) const
115 {
116 out.pushArrayHead();
117
118 std::vector<serializable_ptr>::const_iterator i;
119 for (i = variable.begin(); i < variable.end(); i++)
120 {
121 (*i)->serialize(out);
122 }
123
124 out.pushArrayTail();
125 }
126
127 template <>
128 inline void
129 wrapper<std::map<std::string,serializable_ptr> >::serialize(serializer& out) const
130 {
131 out.pushMapHead();
132
133 std::map<std::string,serializable_ptr>::const_iterator i;
134 for (i = variable.begin(); i != variable.end(); i++)
135 {
136 out.push((*i).first);
137 (*i).second->serialize(out);
138 }
139
140 out.pushMapTail();
141 }
142
143 inline void null::serialize(serializer& out) const
144 {
145 out.pushNull();
146 }
147
148
149 template <class T>
150 inline void wrapper<T>::deserialize(deserializer& in)
151 {
152 in.pop();
153 }
154
155 template <>
156 inline void wrapper<std::vector<serializable_ptr> >::deserialize(deserializer& in)
157 {
158 serializable_ptr obj;
159
160 in.pop();
161
162 while (obj = in.deserialize())
163 {
164 variable.push_back(serializable_ptr(obj));
165 }
166
167 in.pop();
168 }
169
170 template <>
171 inline void
172 wrapper<std::map<std::string,serializable_ptr> >::deserialize(deserializer& in)
173 {
174 serializable_ptr obj;
175
176 in.pop();
177
178 while (obj = in.deserialize())
179 {
180 std::string key;
181 if (obj->get(key))
182 {
183 variable[key] = in.deserialize();
184 }
185 }
186
187 in.pop();
188 }
189
190 inline void null::deserialize(deserializer& in)
191 {
192 in.pop();
193 }
194
195
196 template <class T>
197 inline void wrapper<T>::print() const
198 {
199 std::cout << std::boolalpha << typeid(T).name() << "(" << variable << ")";
200 }
201
202 template <>
203 inline void wrapper<std::wstring>::print() const
204 {
205 std::wcout << variable;
206 }
207
208 template <>
209 inline void wrapper<std::vector<serializable_ptr> >::print() const
210 {
211 std::cout << "array";
212 }
213
214 template <>
215 inline void wrapper<std::map<std::string,serializable_ptr> >::print() const
216 {
217 std::cout << "dictionary";
218 }
219
220 inline void null::print() const
221 {
222 std::cout << "null";
223 }
224
225
226 template <class T>
227 inline bool wrapper<T>::get(T& value)
228 {
229 value = variable;
230 return true;
231 }
232
233 inline bool null::isNull()
234 {
235 return true;
236 }
237
238
239 } // namespace dc
240
241 #endif // _SERIALIZABLE_HH_
242
This page took 0.041864 seconds and 4 git commands to generate.