83696652330394f5666ab1a3b5e571d90a988ae6
[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
32 // Abstract data stream buffer
33 class IStream {
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   public:
43 /*    ISerializable(){};
44       ISerializable(IStream&){}; */
45     virtual void Serialize(IStream &) const = 0;
46     virtual ~ISerializable(){};
47 };
48
49 struct Serialization {
50 // serialization
51 // normal functions
52
53 // ISerializable objects
54 static void Serialize(IStream& stream, const ISerializable& object){
55     object.Serialize(stream);
56 }
57 static void Serialize(IStream& stream, const ISerializable* const object){
58     object->Serialize(stream);
59 }
60
61 // unsigned int
62 static void Serialize(IStream& stream, const unsigned value){
63     stream.Write(sizeof(value),&value);
64 }
65 static void Serialize(IStream& stream, const unsigned* const value){
66     stream.Write(sizeof(*value),value);
67 }
68
69 // int
70 static void Serialize(IStream& stream, const int value){
71     stream.Write(sizeof(value),&value);
72 }
73 static void Serialize(IStream& stream, const int* const value){
74     stream.Write(sizeof(*value),value);
75 }
76
77 // bool
78 static void Serialize(IStream& stream, const bool value){
79     stream.Write(sizeof(value),&value);
80 }
81 static void Serialize(IStream& stream, const bool* const value){
82     stream.Write(sizeof(*value),value);
83 }
84
85 // std::string
86 static void Serialize(IStream& stream, const std::string& str){
87     int length = str.size();
88     stream.Write(sizeof(length),&length);
89     stream.Write(length,str.c_str());
90 }
91 static void Serialize(IStream& stream, const std::string* const str){
92     int length = str->size();
93     stream.Write(sizeof(length),&length);
94     stream.Write(length,str->c_str());
95 }
96
97 // STL templates
98
99 // std::list
100 template <typename T>
101 static void Serialize(IStream& stream, const std::list<T>& list){
102     int length = list.size();
103     stream.Write(sizeof(length),&length);
104     for(typename std::list<T>::const_iterator list_iter = list.begin();
105         list_iter != list.end(); list_iter++)
106     {
107         Serialize(stream, *list_iter);
108     }
109 }
110 template <typename T>
111 static void Serialize(IStream& stream, const std::list<T>* const list){
112     Serialize(stream,*list);
113 }
114
115 // std::vector
116 template <typename T>
117 static void Serialize(IStream& stream, const std::vector<T>& vec){
118     int length = vec.size();
119     stream.Write(sizeof(length),&length);
120     for(typename std::vector<T>::const_iterator vec_iter = vec.begin();
121             vec_iter  != vec.end(); vec_iter ++)
122     {
123         Serialize(stream, *vec_iter);
124     }
125 }
126 template <typename T>
127 static void Serialize(IStream& stream, const std::vector<T>* const vec){
128     Serialize(stream,*vec);
129 }
130
131 // std::pair
132 template <typename A, typename B>
133 static void Serialize(IStream& stream, const std::pair<A,B>& p){
134     Serialize(stream, p.first);
135     Serialize(stream, p.second);
136 }
137 template <typename A, typename B>
138 static void Serialize(IStream& stream, const std::pair<A,B>* const p){
139     Serialize(stream,*p);
140 }
141
142 // std::map
143 template <typename K, typename T>
144 static void Serialize(IStream& stream, const std::map<K,T>& map){
145     int length = map.size();
146     stream.Write(sizeof(length),&length);
147     typename std::map<K,T>::const_iterator it;
148     for (it = map.begin(); it != map.end(); ++it) {
149         Serialize(stream,(*it).first);
150         Serialize(stream,(*it).second);
151     }
152 }
153 template <typename K, typename T>
154 static void Serialize(IStream& stream, const std::map<K,T>* const map){
155     Serialize(stream,*map);
156 }
157 }; // struct Serialization
158
159 struct Deserialization {
160 // deserialization
161 // normal functions
162
163 // ISerializable objects
164 // T instead of ISerializable is needed to call proper constructor
165 template <typename T>
166 static void Deserialize(IStream& stream, T& object){
167     object = T(stream);
168 }
169 template <typename T>
170 static void Deserialize(IStream& stream, T*& object){
171     object = new T(stream);
172 }
173
174 // unsigned int
175 static void Deserialize(IStream& stream, unsigned& value){
176     stream.Read(sizeof(value),&value);
177 }
178 static void Deserialize(IStream& stream, unsigned*& value){
179     value = new unsigned;
180     stream.Read(sizeof(*value),value);
181 }
182
183 // int
184 static void Deserialize(IStream& stream, int& value){
185     stream.Read(sizeof(value),&value);
186 }
187 static void Deserialize(IStream& stream, int*& value){
188     value = new int;
189     stream.Read(sizeof(*value),value);
190 }
191
192 // bool
193 static void Deserialize(IStream& stream, bool& value){
194     stream.Read(sizeof(value),&value);
195 }
196 static void Deserialize(IStream& stream, bool*& value){
197     value = new bool;
198     stream.Read(sizeof(*value),value);
199 }
200
201 // std::string
202 static void Deserialize(IStream& stream, std::string& str){
203     int length;
204     stream.Read(sizeof(length),&length);
205     char * buf = new char[length+1];
206     stream.Read(length,buf);
207     buf[length] = 0;
208     str = std::string(buf);
209     delete [] buf;
210 }
211 static void Deserialize(IStream& stream, std::string*& str){
212     int length;
213     stream.Read(sizeof(length),&length);
214     char * buf = new char[length+1];
215     stream.Read(length,buf);
216     buf[length] = 0;
217     str = new std::string(buf);
218     delete [] buf;
219 }
220
221 // STL templates
222
223 // std::list
224 template <typename T>
225 static void Deserialize(IStream& stream, std::list<T>& list){
226     int length;
227     stream.Read(sizeof(length),&length);
228     for (int i = 0; i < length; ++i) {
229         T obj;
230         Deserialize(stream, obj);
231         list.push_back(obj);
232     }
233 }
234 template <typename T>
235 static void Deserialize(IStream& stream, std::list<T>*& list){
236     list = new std::list<T>;
237     Deserialize(stream,*list);
238 }
239
240 // std::vector
241 template <typename T>
242 static void Deserialize(IStream& stream, std::vector<T>& vec){
243     int length;
244     stream.Read(sizeof(length),&length);
245     for (int i = 0; i < length; ++i) {
246         T obj;
247         Deserialize(stream, obj);
248         vec.push_back(obj);
249     }
250 }
251 template <typename T>
252 static void Deserialize(IStream& stream, std::vector<T>*& vec){
253     vec = new std::vector<T>;
254     Deserialize(stream,*vec);
255 }
256
257 // std::pair
258 template <typename A, typename B>
259 static void Deserialize(IStream& stream, std::pair<A,B>& p){
260     Deserialize(stream, p.first);
261     Deserialize(stream, p.second);
262 }
263 template <typename A, typename B>
264 static void Deserialize(IStream& stream, std::pair<A,B>*& p){
265     p = new std::pair<A,B>;
266     Deserialize(stream,*p);
267 }
268
269 // std::map
270 template <typename K, typename T>
271 static void Deserialize(IStream& stream, std::map<K,T>& map){
272     int length;
273     stream.Read(sizeof(length),&length);
274     for (int i = 0; i < length; ++i) {
275         K key;
276         T obj;
277         Deserialize(stream,key);
278         Deserialize(stream,obj);
279         map[key] = obj;
280     }
281 }
282 template <typename K, typename T>
283 static void Deserialize(IStream& stream, std::map<K,T>*& map){
284     map = new std::map<K,T>;
285     Deserialize(stream,*map);
286 }
287 }; // struct Deserialization
288
289 } // namespace DPL
290
291 #endif // SERIALIZATION_H