]> Dogcows Code - chaz/yoink/blob - src/Moof/Serializable.hh
revamped scene drawing in preparation for octree
[chaz/yoink] / src / Moof / 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 _MOOF_SERIALIZABLE_HH_
30 #define _MOOF_SERIALIZABLE_HH_
31
32 #include <iostream>
33 #include <map>
34 #include <string>
35 #include <vector>
36
37 #include <Moof/Deserializer.hh>
38 #include <Moof/Serializer.hh>
39 #include <Moof/StringTools.hh>
40
41
42 namespace Mf {
43
44
45 /**
46 * Interface for a type which can be serialized and deserialized.
47 */
48
49 class Serializable
50 {
51 public:
52 virtual ~Serializable();
53
54 virtual void serialize(Serializer& out) const = 0;
55 virtual void deserialize(Deserializer& in) = 0;
56
57 virtual void print() const = 0;
58
59 virtual bool get(long& value);
60 virtual bool get(double& value);
61 virtual bool get(bool& value);
62 virtual bool get(std::string& value);
63 virtual bool get(std::wstring& value);
64 virtual bool get(std::vector<SerializablePtr>& value);
65 virtual bool get(std::map<std::string,SerializablePtr>& value);
66
67 /*
68 * To get a number value which may have been parsed as either an integer or
69 * double, use these getters instead.
70 */
71
72 bool getNumber(long&);
73 bool getNumber(double&);
74
75 virtual bool isNull();
76 };
77
78
79 template <class T>
80 class SerializableBase : public Serializable
81 {
82 public:
83 SerializableBase() {}
84 SerializableBase(const T& value) :
85 value_(value) {}
86
87 void serialize(Serializer& out) const;
88 void deserialize(Deserializer& in);
89
90 void print() const;
91 bool get(T& value);
92 public:
93 T value_;
94 };
95
96
97 class SerializableNull : public Serializable
98 {
99 public:
100 SerializableNull() {}
101 void serialize(Serializer& out) const;
102 void deserialize(Deserializer& in);
103
104 void print() const;
105 bool isNull();
106 };
107
108
109 typedef SerializableBase<long> SerializableInteger;
110 typedef SerializableBase<double> SerializableReal;
111 typedef SerializableBase<bool> SerializableBoolean;
112 typedef SerializableBase<std::string> SerializableString;
113 typedef SerializableBase<std::wstring> SerializableWideString;
114 typedef SerializableBase<std::vector<SerializablePtr> > SerializableArray;
115 typedef SerializableBase<std::map<std::string,SerializablePtr> >
116 SerializableMap;
117
118
119
120 template <class T>
121 inline void SerializableBase<T>::serialize(Serializer& out) const
122 {
123 out.push(value_);
124 }
125
126 template <>
127 inline void
128 SerializableBase<std::vector<SerializablePtr> >::serialize(Serializer& out) const
129 {
130 out.pushArrayHead();
131
132 std::vector<SerializablePtr>::const_iterator it;
133 for (it = value_.begin(); it < value_.end(); ++it)
134 {
135 (*it)->serialize(out);
136 }
137
138 out.pushArrayTail();
139 }
140
141 template <>
142 inline void
143 SerializableBase<std::map<std::string,SerializablePtr> >::serialize(Serializer& out) const
144 {
145 out.pushMapHead();
146
147 std::map<std::string,SerializablePtr>::const_iterator it;
148 for (it = value_.begin(); it != value_.end(); ++it)
149 {
150 out.push((*it).first);
151 (*it).second->serialize(out);
152 }
153
154 out.pushMapTail();
155 }
156
157 inline void SerializableNull::serialize(Serializer& out) const
158 {
159 out.pushNull();
160 }
161
162
163 template <class T>
164 inline void SerializableBase<T>::deserialize(Deserializer& in)
165 {
166 in.pop();
167 }
168
169 template <>
170 inline void SerializableBase<std::vector<SerializablePtr> >::deserialize(Deserializer& in)
171 {
172 SerializablePtr obj;
173
174 in.pop();
175
176 while (obj = in.deserialize())
177 {
178 value_.push_back(SerializablePtr(obj));
179 }
180
181 in.pop();
182 }
183
184 template <>
185 inline void
186 SerializableBase<std::map<std::string,SerializablePtr> >::deserialize(Deserializer& in)
187 {
188 SerializablePtr obj;
189
190 in.pop();
191
192 while (obj = in.deserialize())
193 {
194 std::string key;
195 if (obj->get(key))
196 {
197 value_[key] = in.deserialize();
198 }
199 }
200
201 in.pop();
202 }
203
204 inline void SerializableNull::deserialize(Deserializer& in)
205 {
206 in.pop();
207 }
208
209
210 template <class T>
211 inline void SerializableBase<T>::print() const
212 {
213 std::cout << std::boolalpha << typeid(T).name() << "(" << value_ << ")";
214 }
215
216 template <>
217 inline void SerializableBase<std::wstring>::print() const
218 {
219 std::wcout << value_;
220 }
221
222 template <>
223 inline void SerializableBase<std::vector<SerializablePtr> >::print() const
224 {
225 std::cout << "array";
226 }
227
228 template <>
229 inline void SerializableBase<std::map<std::string,SerializablePtr> >::print() const
230 {
231 std::cout << "map";
232 }
233
234 inline void SerializableNull::print() const
235 {
236 std::cout << "null";
237 }
238
239
240 template <class T>
241 inline bool SerializableBase<T>::get(T& value)
242 {
243 value = value_;
244 return true;
245 }
246
247 inline bool SerializableNull::isNull()
248 {
249 return true;
250 }
251
252
253 } // namespace Mf
254
255 #endif // _MOOF_SERIALIZABLE_HH_
256
257 /** vim: set ts=4 sw=4 tw=80: *************************************************/
258
This page took 0.048831 seconds and 4 git commands to generate.