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 long unsigned int value)
77 stream.Write(sizeof(value), &value);
79 static void Serialize(IStream& stream, const long unsigned int* const value)
81 stream.Write(sizeof(*value), value);
85 static void Serialize(IStream& stream, const int value)
87 stream.Write(sizeof(value), &value);
89 static void Serialize(IStream& stream, const int* const value)
91 stream.Write(sizeof(*value), value);
95 static void Serialize(IStream& stream, const bool value)
97 stream.Write(sizeof(value), &value);
99 static void Serialize(IStream& stream, const bool* const value)
101 stream.Write(sizeof(*value), value);
105 static void Serialize(IStream& stream, const std::string& str)
107 int length = str.size();
108 stream.Write(sizeof(length), &length);
109 stream.Write(length, str.c_str());
111 static void Serialize(IStream& stream, const std::string* const str)
113 int length = str->size();
114 stream.Write(sizeof(length), &length);
115 stream.Write(length, str->c_str());
121 template <typename T>
122 static void Serialize(IStream& stream, const std::list<T>& list)
124 int length = list.size();
125 stream.Write(sizeof(length), &length);
126 for (typename std::list<T>::const_iterator list_iter = list.begin();
127 list_iter != list.end(); list_iter++)
129 Serialize(stream, *list_iter);
132 template <typename T>
133 static void Serialize(IStream& stream, const std::list<T>* const list)
135 Serialize(stream, *list);
139 template <typename T>
140 static void Serialize(IStream& stream, const std::vector<T>& vec)
142 int length = vec.size();
143 stream.Write(sizeof(length), &length);
144 for (typename std::vector<T>::const_iterator vec_iter = vec.begin();
145 vec_iter != vec.end(); vec_iter++)
147 Serialize(stream, *vec_iter);
150 template <typename T>
151 static void Serialize(IStream& stream, const std::vector<T>* const vec)
153 Serialize(stream, *vec);
157 template <typename A, typename B>
158 static void Serialize(IStream& stream, const std::pair<A, B>& p)
160 Serialize(stream, p.first);
161 Serialize(stream, p.second);
163 template <typename A, typename B>
164 static void Serialize(IStream& stream, const std::pair<A, B>* const p)
166 Serialize(stream, *p);
170 template <typename K, typename T>
171 static void Serialize(IStream& stream, const std::map<K, T>& map)
173 int length = map.size();
174 stream.Write(sizeof(length), &length);
175 typename std::map<K, T>::const_iterator it;
176 for (it = map.begin(); it != map.end(); ++it) {
177 Serialize(stream, (*it).first);
178 Serialize(stream, (*it).second);
181 template <typename K, typename T>
182 static void Serialize(IStream& stream, const std::map<K, T>* const map)
184 Serialize(stream, *map);
186 }; // struct Serialization
188 struct Deserialization {
192 // ISerializable objects
193 // T instead of ISerializable is needed to call proper constructor
194 template <typename T>
195 static void Deserialize(IStream& stream, T& object)
199 template <typename T>
200 static void Deserialize(IStream& stream, T*& object)
202 object = new T(stream);
206 static void Deserialize(IStream& stream, unsigned& value)
208 stream.Read(sizeof(value), &value);
210 static void Deserialize(IStream& stream, unsigned*& value)
212 value = new unsigned;
213 stream.Read(sizeof(*value), value);
217 static void Deserialize(IStream& stream, long unsigned int& value)
219 stream.Read(sizeof(value), &value);
221 static void Deserialize(IStream& stream, long unsigned int*& value)
223 value = new long unsigned int;
224 stream.Read(sizeof(*value), value);
228 static void Deserialize(IStream& stream, int& value)
230 stream.Read(sizeof(value), &value);
232 static void Deserialize(IStream& stream, int*& value)
235 stream.Read(sizeof(*value), value);
239 static void Deserialize(IStream& stream, bool& value)
241 stream.Read(sizeof(value), &value);
243 static void Deserialize(IStream& stream, bool*& value)
246 stream.Read(sizeof(*value), value);
250 static void Deserialize(IStream& stream, std::string& str)
253 stream.Read(sizeof(length), &length);
254 char * buf = new char[length + 1];
255 stream.Read(length, buf);
257 str = std::string(buf);
260 static void Deserialize(IStream& stream, std::string*& str)
263 stream.Read(sizeof(length), &length);
264 char * buf = new char[length + 1];
265 stream.Read(length, buf);
267 str = new std::string(buf);
274 template <typename T>
275 static void Deserialize(IStream& stream, std::list<T>& list)
278 stream.Read(sizeof(length), &length);
279 for (int i = 0; i < length; ++i) {
281 Deserialize(stream, obj);
285 template <typename T>
286 static void Deserialize(IStream& stream, std::list<T>*& list)
288 list = new std::list<T>;
289 Deserialize(stream, *list);
293 template <typename T>
294 static void Deserialize(IStream& stream, std::vector<T>& vec)
297 stream.Read(sizeof(length), &length);
298 for (int i = 0; i < length; ++i) {
300 Deserialize(stream, obj);
304 template <typename T>
305 static void Deserialize(IStream& stream, std::vector<T>*& vec)
307 vec = new std::vector<T>;
308 Deserialize(stream, *vec);
312 template <typename A, typename B>
313 static void Deserialize(IStream& stream, std::pair<A, B>& p)
315 Deserialize(stream, p.first);
316 Deserialize(stream, p.second);
318 template <typename A, typename B>
319 static void Deserialize(IStream& stream, std::pair<A, B>*& p)
321 p = new std::pair<A, B>;
322 Deserialize(stream, *p);
326 template <typename K, typename T>
327 static void Deserialize(IStream& stream, std::map<K, T>& map)
330 stream.Read(sizeof(length), &length);
331 for (int i = 0; i < length; ++i) {
334 Deserialize(stream, key);
335 Deserialize(stream, obj);
339 template <typename K, typename T>
340 static void Deserialize(IStream& stream, std::map<K, T>*& map)
342 map = new std::map<K, T>;
343 Deserialize(stream, *map);
345 }; // struct Deserialization
348 #endif // SERIALIZATION_H