Add Serialize/Deserialize function for unsigned long
[platform/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     // long int
75     static void Serialize(IStream& stream, const long unsigned int value)
76     {
77         stream.Write(sizeof(value), &value);
78     }
79     static void Serialize(IStream& stream, const long unsigned int* const value)
80     {
81         stream.Write(sizeof(*value), value);
82     }
83
84     // int
85     static void Serialize(IStream& stream, const int value)
86     {
87         stream.Write(sizeof(value), &value);
88     }
89     static void Serialize(IStream& stream, const int* const value)
90     {
91         stream.Write(sizeof(*value), value);
92     }
93
94     // bool
95     static void Serialize(IStream& stream, const bool value)
96     {
97         stream.Write(sizeof(value), &value);
98     }
99     static void Serialize(IStream& stream, const bool* const value)
100     {
101         stream.Write(sizeof(*value), value);
102     }
103
104     // std::string
105     static void Serialize(IStream& stream, const std::string& str)
106     {
107         int length = str.size();
108         stream.Write(sizeof(length), &length);
109         stream.Write(length, str.c_str());
110     }
111     static void Serialize(IStream& stream, const std::string* const str)
112     {
113         int length = str->size();
114         stream.Write(sizeof(length), &length);
115         stream.Write(length, str->c_str());
116     }
117
118     // STL templates
119
120     // std::list
121     template <typename T>
122     static void Serialize(IStream& stream, const std::list<T>& list)
123     {
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++)
128         {
129             Serialize(stream, *list_iter);
130         }
131     }
132     template <typename T>
133     static void Serialize(IStream& stream, const std::list<T>* const list)
134     {
135         Serialize(stream, *list);
136     }
137
138     // std::vector
139     template <typename T>
140     static void Serialize(IStream& stream, const std::vector<T>& vec)
141     {
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++)
146         {
147             Serialize(stream, *vec_iter);
148         }
149     }
150     template <typename T>
151     static void Serialize(IStream& stream, const std::vector<T>* const vec)
152     {
153         Serialize(stream, *vec);
154     }
155
156     // std::pair
157     template <typename A, typename B>
158     static void Serialize(IStream& stream, const std::pair<A, B>& p)
159     {
160         Serialize(stream, p.first);
161         Serialize(stream, p.second);
162     }
163     template <typename A, typename B>
164     static void Serialize(IStream& stream, const std::pair<A, B>* const p)
165     {
166         Serialize(stream, *p);
167     }
168
169     // std::map
170     template <typename K, typename T>
171     static void Serialize(IStream& stream, const std::map<K, T>& map)
172     {
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);
179         }
180     }
181     template <typename K, typename T>
182     static void Serialize(IStream& stream, const std::map<K, T>* const map)
183     {
184         Serialize(stream, *map);
185     }
186 }; // struct Serialization
187
188 struct Deserialization {
189     // deserialization
190     // normal functions
191
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)
196     {
197         object = T(stream);
198     }
199     template <typename T>
200     static void Deserialize(IStream& stream, T*& object)
201     {
202         object = new T(stream);
203     }
204
205     // unsigned int
206     static void Deserialize(IStream& stream, unsigned& value)
207     {
208         stream.Read(sizeof(value), &value);
209     }
210     static void Deserialize(IStream& stream, unsigned*& value)
211     {
212         value = new unsigned;
213         stream.Read(sizeof(*value), value);
214     }
215
216    // long int
217      static void Deserialize(IStream& stream, long unsigned  int& value)
218     {
219         stream.Read(sizeof(value), &value);
220     }
221     static void Deserialize(IStream& stream, long unsigned  int*& value)
222     {
223         value = new  long unsigned int;
224         stream.Read(sizeof(*value), value);
225     }
226
227     // int
228     static void Deserialize(IStream& stream, int& value)
229     {
230         stream.Read(sizeof(value), &value);
231     }
232     static void Deserialize(IStream& stream, int*& value)
233     {
234         value = new int;
235         stream.Read(sizeof(*value), value);
236     }
237
238     // bool
239     static void Deserialize(IStream& stream, bool& value)
240     {
241         stream.Read(sizeof(value), &value);
242     }
243     static void Deserialize(IStream& stream, bool*& value)
244     {
245         value = new bool;
246         stream.Read(sizeof(*value), value);
247     }
248
249     // std::string
250     static void Deserialize(IStream& stream, std::string& str)
251     {
252         int length;
253         stream.Read(sizeof(length), &length);
254         char * buf = new char[length + 1];
255         stream.Read(length, buf);
256         buf[length] = 0;
257         str = std::string(buf);
258         delete[] buf;
259     }
260     static void Deserialize(IStream& stream, std::string*& str)
261     {
262         int length;
263         stream.Read(sizeof(length), &length);
264         char * buf = new char[length + 1];
265         stream.Read(length, buf);
266         buf[length] = 0;
267         str = new std::string(buf);
268         delete[] buf;
269     }
270
271     // STL templates
272
273     // std::list
274     template <typename T>
275     static void Deserialize(IStream& stream, std::list<T>& list)
276     {
277         int length;
278         stream.Read(sizeof(length), &length);
279         for (int i = 0; i < length; ++i) {
280             T obj;
281             Deserialize(stream, obj);
282             list.push_back(obj);
283         }
284     }
285     template <typename T>
286     static void Deserialize(IStream& stream, std::list<T>*& list)
287     {
288         list = new std::list<T>;
289         Deserialize(stream, *list);
290     }
291
292     // std::vector
293     template <typename T>
294     static void Deserialize(IStream& stream, std::vector<T>& vec)
295     {
296         int length;
297         stream.Read(sizeof(length), &length);
298         for (int i = 0; i < length; ++i) {
299             T obj;
300             Deserialize(stream, obj);
301             vec.push_back(obj);
302         }
303     }
304     template <typename T>
305     static void Deserialize(IStream& stream, std::vector<T>*& vec)
306     {
307         vec = new std::vector<T>;
308         Deserialize(stream, *vec);
309     }
310
311     // std::pair
312     template <typename A, typename B>
313     static void Deserialize(IStream& stream, std::pair<A, B>& p)
314     {
315         Deserialize(stream, p.first);
316         Deserialize(stream, p.second);
317     }
318     template <typename A, typename B>
319     static void Deserialize(IStream& stream, std::pair<A, B>*& p)
320     {
321         p = new std::pair<A, B>;
322         Deserialize(stream, *p);
323     }
324
325     // std::map
326     template <typename K, typename T>
327     static void Deserialize(IStream& stream, std::map<K, T>& map)
328     {
329         int length;
330         stream.Read(sizeof(length), &length);
331         for (int i = 0; i < length; ++i) {
332             K key;
333             T obj;
334             Deserialize(stream, key);
335             Deserialize(stream, obj);
336             map[key] = obj;
337         }
338     }
339     template <typename K, typename T>
340     static void Deserialize(IStream& stream, std::map<K, T>*& map)
341     {
342         map = new std::map<K, T>;
343         Deserialize(stream, *map);
344     }
345 }; // struct Deserialization
346 } // namespace DPL
347
348 #endif // SERIALIZATION_H