2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file serialization.h
18 * @author Tomasz Swierczek (t.swierczek@samsung.com)
20 * @brief Interfaces and templates used for data serialization.
22 #ifndef SERIALIZATION_H
23 #define SERIALIZATION_H
31 // Abstract data stream buffer
35 virtual void Read(size_t num, void * bytes) = 0;
36 virtual void Write(size_t num, const void * bytes) = 0;
40 // Serializable interface
45 * ISerializable(IStream&){}; */
46 virtual void Serialize(IStream &) const = 0;
47 virtual ~ISerializable(){}
50 struct Serialization {
54 // ISerializable objects
55 static void Serialize(IStream& stream, const ISerializable& object)
57 object.Serialize(stream);
59 static void Serialize(IStream& stream, const ISerializable* const object)
61 object->Serialize(stream);
65 static void Serialize(IStream& stream, const unsigned value)
67 stream.Write(sizeof(value), &value);
69 static void Serialize(IStream& stream, const unsigned* const value)
71 stream.Write(sizeof(*value), value);
75 static void Serialize(IStream& stream, const int value)
77 stream.Write(sizeof(value), &value);
79 static void Serialize(IStream& stream, const int* const value)
81 stream.Write(sizeof(*value), value);
85 static void Serialize(IStream& stream, const bool value)
87 stream.Write(sizeof(value), &value);
89 static void Serialize(IStream& stream, const bool* const value)
91 stream.Write(sizeof(*value), value);
95 static void Serialize(IStream& stream, const std::string& str)
97 int length = str.size();
98 stream.Write(sizeof(length), &length);
99 stream.Write(length, str.c_str());
101 static void Serialize(IStream& stream, const std::string* const str)
103 int length = str->size();
104 stream.Write(sizeof(length), &length);
105 stream.Write(length, str->c_str());
111 template <typename T>
112 static void Serialize(IStream& stream, const std::list<T>& list)
114 int length = list.size();
115 stream.Write(sizeof(length), &length);
116 for (typename std::list<T>::const_iterator list_iter = list.begin();
117 list_iter != list.end(); list_iter++)
119 Serialize(stream, *list_iter);
122 template <typename T>
123 static void Serialize(IStream& stream, const std::list<T>* const list)
125 Serialize(stream, *list);
129 template <typename T>
130 static void Serialize(IStream& stream, const std::vector<T>& vec)
132 int length = vec.size();
133 stream.Write(sizeof(length), &length);
134 for (typename std::vector<T>::const_iterator vec_iter = vec.begin();
135 vec_iter != vec.end(); vec_iter++)
137 Serialize(stream, *vec_iter);
140 template <typename T>
141 static void Serialize(IStream& stream, const std::vector<T>* const vec)
143 Serialize(stream, *vec);
147 template <typename A, typename B>
148 static void Serialize(IStream& stream, const std::pair<A, B>& p)
150 Serialize(stream, p.first);
151 Serialize(stream, p.second);
153 template <typename A, typename B>
154 static void Serialize(IStream& stream, const std::pair<A, B>* const p)
156 Serialize(stream, *p);
160 template <typename K, typename T>
161 static void Serialize(IStream& stream, const std::map<K, T>& map)
163 int length = map.size();
164 stream.Write(sizeof(length), &length);
165 typename std::map<K, T>::const_iterator it;
166 for (it = map.begin(); it != map.end(); ++it) {
167 Serialize(stream, (*it).first);
168 Serialize(stream, (*it).second);
171 template <typename K, typename T>
172 static void Serialize(IStream& stream, const std::map<K, T>* const map)
174 Serialize(stream, *map);
176 }; // struct Serialization
178 struct Deserialization {
182 // ISerializable objects
183 // T instead of ISerializable is needed to call proper constructor
184 template <typename T>
185 static void Deserialize(IStream& stream, T& object)
189 template <typename T>
190 static void Deserialize(IStream& stream, T*& object)
192 object = new T(stream);
196 static void Deserialize(IStream& stream, unsigned& value)
198 stream.Read(sizeof(value), &value);
200 static void Deserialize(IStream& stream, unsigned*& value)
202 value = new unsigned;
203 stream.Read(sizeof(*value), value);
207 static void Deserialize(IStream& stream, int& value)
209 stream.Read(sizeof(value), &value);
211 static void Deserialize(IStream& stream, int*& value)
214 stream.Read(sizeof(*value), value);
218 static void Deserialize(IStream& stream, bool& value)
220 stream.Read(sizeof(value), &value);
222 static void Deserialize(IStream& stream, bool*& value)
225 stream.Read(sizeof(*value), value);
229 static void Deserialize(IStream& stream, std::string& str)
232 stream.Read(sizeof(length), &length);
233 char * buf = new char[length + 1];
234 stream.Read(length, buf);
236 str = std::string(buf);
239 static void Deserialize(IStream& stream, std::string*& str)
242 stream.Read(sizeof(length), &length);
243 char * buf = new char[length + 1];
244 stream.Read(length, buf);
246 str = new std::string(buf);
253 template <typename T>
254 static void Deserialize(IStream& stream, std::list<T>& list)
257 stream.Read(sizeof(length), &length);
258 for (int i = 0; i < length; ++i) {
260 Deserialize(stream, obj);
264 template <typename T>
265 static void Deserialize(IStream& stream, std::list<T>*& list)
267 list = new std::list<T>;
268 Deserialize(stream, *list);
272 template <typename T>
273 static void Deserialize(IStream& stream, std::vector<T>& vec)
276 stream.Read(sizeof(length), &length);
277 for (int i = 0; i < length; ++i) {
279 Deserialize(stream, obj);
283 template <typename T>
284 static void Deserialize(IStream& stream, std::vector<T>*& vec)
286 vec = new std::vector<T>;
287 Deserialize(stream, *vec);
291 template <typename A, typename B>
292 static void Deserialize(IStream& stream, std::pair<A, B>& p)
294 Deserialize(stream, p.first);
295 Deserialize(stream, p.second);
297 template <typename A, typename B>
298 static void Deserialize(IStream& stream, std::pair<A, B>*& p)
300 p = new std::pair<A, B>;
301 Deserialize(stream, *p);
305 template <typename K, typename T>
306 static void Deserialize(IStream& stream, std::map<K, T>& map)
309 stream.Read(sizeof(length), &length);
310 for (int i = 0; i < length; ++i) {
313 Deserialize(stream, key);
314 Deserialize(stream, obj);
318 template <typename K, typename T>
319 static void Deserialize(IStream& stream, std::map<K, T>*& map)
321 map = new std::map<K, T>;
322 Deserialize(stream, *map);
324 }; // struct Deserialization
327 #endif // SERIALIZATION_H