tizen 2.4 release
[framework/web/wrt-commons.git] / modules / core / include / dpl / serialization.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file    serialization.h
18  * @author  Tomasz Swierczek (t.swierczek@samsung.com)
19  * @version 1.0
20  * @brief   Interfaces and templates used for data serialization.
21  */
22 #ifndef SERIALIZATION_H
23 #define SERIALIZATION_H
24
25 #include <string>
26 #include <vector>
27 #include <list>
28 #include <map>
29
30 namespace DPL {
31 // Abstract data stream buffer
32 class IStream
33 {
34   public:
35     virtual void Read(size_t num, void * bytes) = 0;
36     virtual void Write(size_t num, const void * bytes) = 0;
37     virtual ~IStream(){}
38 };
39
40 // Serializable interface
41 class ISerializable
42 {
43   public:
44     /*    ISerializable(){};
45      *    ISerializable(IStream&){}; */
46     virtual void Serialize(IStream &) const = 0;
47     virtual ~ISerializable(){}
48 };
49
50 struct Serialization {
51     // serialization
52     // normal functions
53
54     // ISerializable objects
55     static void Serialize(IStream& stream, const ISerializable& object)
56     {
57         object.Serialize(stream);
58     }
59     static void Serialize(IStream& stream, const ISerializable* const object)
60     {
61         object->Serialize(stream);
62     }
63
64     // unsigned int
65     static void Serialize(IStream& stream, const unsigned value)
66     {
67         stream.Write(sizeof(value), &value);
68     }
69     static void Serialize(IStream& stream, const unsigned* const value)
70     {
71         stream.Write(sizeof(*value), value);
72     }
73
74     // int
75     static void Serialize(IStream& stream, const int value)
76     {
77         stream.Write(sizeof(value), &value);
78     }
79     static void Serialize(IStream& stream, const int* const value)
80     {
81         stream.Write(sizeof(*value), value);
82     }
83
84     // bool
85     static void Serialize(IStream& stream, const bool value)
86     {
87         stream.Write(sizeof(value), &value);
88     }
89     static void Serialize(IStream& stream, const bool* const value)
90     {
91         stream.Write(sizeof(*value), value);
92     }
93
94     // std::string
95     static void Serialize(IStream& stream, const std::string& str)
96     {
97         int length = str.size();
98         stream.Write(sizeof(length), &length);
99         stream.Write(length, str.c_str());
100     }
101     static void Serialize(IStream& stream, const std::string* const str)
102     {
103         int length = str->size();
104         stream.Write(sizeof(length), &length);
105         stream.Write(length, str->c_str());
106     }
107
108     // STL templates
109
110     // std::list
111     template <typename T>
112     static void Serialize(IStream& stream, const std::list<T>& list)
113     {
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++)
118         {
119             Serialize(stream, *list_iter);
120         }
121     }
122     template <typename T>
123     static void Serialize(IStream& stream, const std::list<T>* const list)
124     {
125         Serialize(stream, *list);
126     }
127
128     // std::vector
129     template <typename T>
130     static void Serialize(IStream& stream, const std::vector<T>& vec)
131     {
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++)
136         {
137             Serialize(stream, *vec_iter);
138         }
139     }
140     template <typename T>
141     static void Serialize(IStream& stream, const std::vector<T>* const vec)
142     {
143         Serialize(stream, *vec);
144     }
145
146     // std::pair
147     template <typename A, typename B>
148     static void Serialize(IStream& stream, const std::pair<A, B>& p)
149     {
150         Serialize(stream, p.first);
151         Serialize(stream, p.second);
152     }
153     template <typename A, typename B>
154     static void Serialize(IStream& stream, const std::pair<A, B>* const p)
155     {
156         Serialize(stream, *p);
157     }
158
159     // std::map
160     template <typename K, typename T>
161     static void Serialize(IStream& stream, const std::map<K, T>& map)
162     {
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);
169         }
170     }
171     template <typename K, typename T>
172     static void Serialize(IStream& stream, const std::map<K, T>* const map)
173     {
174         Serialize(stream, *map);
175     }
176 }; // struct Serialization
177
178 struct Deserialization {
179     // deserialization
180     // normal functions
181
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)
186     {
187         object = T(stream);
188     }
189     template <typename T>
190     static void Deserialize(IStream& stream, T*& object)
191     {
192         object = new T(stream);
193     }
194
195     // unsigned int
196     static void Deserialize(IStream& stream, unsigned& value)
197     {
198         stream.Read(sizeof(value), &value);
199     }
200     static void Deserialize(IStream& stream, unsigned*& value)
201     {
202         value = new unsigned;
203         stream.Read(sizeof(*value), value);
204     }
205
206     // int
207     static void Deserialize(IStream& stream, int& value)
208     {
209         stream.Read(sizeof(value), &value);
210     }
211     static void Deserialize(IStream& stream, int*& value)
212     {
213         value = new int;
214         stream.Read(sizeof(*value), value);
215     }
216
217     // bool
218     static void Deserialize(IStream& stream, bool& value)
219     {
220         stream.Read(sizeof(value), &value);
221     }
222     static void Deserialize(IStream& stream, bool*& value)
223     {
224         value = new bool;
225         stream.Read(sizeof(*value), value);
226     }
227
228     // std::string
229     static void Deserialize(IStream& stream, std::string& str)
230     {
231         int length;
232         stream.Read(sizeof(length), &length);
233         char * buf = new char[length + 1];
234         stream.Read(length, buf);
235         buf[length] = 0;
236         str = std::string(buf);
237         delete[] buf;
238     }
239     static void Deserialize(IStream& stream, std::string*& str)
240     {
241         int length;
242         stream.Read(sizeof(length), &length);
243         char * buf = new char[length + 1];
244         stream.Read(length, buf);
245         buf[length] = 0;
246         str = new std::string(buf);
247         delete[] buf;
248     }
249
250     // STL templates
251
252     // std::list
253     template <typename T>
254     static void Deserialize(IStream& stream, std::list<T>& list)
255     {
256         int length;
257         stream.Read(sizeof(length), &length);
258         for (int i = 0; i < length; ++i) {
259             T obj;
260             Deserialize(stream, obj);
261             list.push_back(obj);
262         }
263     }
264     template <typename T>
265     static void Deserialize(IStream& stream, std::list<T>*& list)
266     {
267         list = new std::list<T>;
268         Deserialize(stream, *list);
269     }
270
271     // std::vector
272     template <typename T>
273     static void Deserialize(IStream& stream, std::vector<T>& vec)
274     {
275         int length;
276         stream.Read(sizeof(length), &length);
277         for (int i = 0; i < length; ++i) {
278             T obj;
279             Deserialize(stream, obj);
280             vec.push_back(obj);
281         }
282     }
283     template <typename T>
284     static void Deserialize(IStream& stream, std::vector<T>*& vec)
285     {
286         vec = new std::vector<T>;
287         Deserialize(stream, *vec);
288     }
289
290     // std::pair
291     template <typename A, typename B>
292     static void Deserialize(IStream& stream, std::pair<A, B>& p)
293     {
294         Deserialize(stream, p.first);
295         Deserialize(stream, p.second);
296     }
297     template <typename A, typename B>
298     static void Deserialize(IStream& stream, std::pair<A, B>*& p)
299     {
300         p = new std::pair<A, B>;
301         Deserialize(stream, *p);
302     }
303
304     // std::map
305     template <typename K, typename T>
306     static void Deserialize(IStream& stream, std::map<K, T>& map)
307     {
308         int length;
309         stream.Read(sizeof(length), &length);
310         for (int i = 0; i < length; ++i) {
311             K key;
312             T obj;
313             Deserialize(stream, key);
314             Deserialize(stream, obj);
315             map[key] = obj;
316         }
317     }
318     template <typename K, typename T>
319     static void Deserialize(IStream& stream, std::map<K, T>*& map)
320     {
321         map = new std::map<K, T>;
322         Deserialize(stream, *map);
323     }
324 }; // struct Deserialization
325 } // namespace DPL
326
327 #endif // SERIALIZATION_H