bb6602c4828f50307c0adfd2eb2dd95a951ecabc
[platform/core/security/security-manager.git] / src / server / dpl / 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 #include <memory>
30
31 namespace SecurityManager {
32 // Abstract data stream buffer
33 class IStream
34 {
35   public:
36     virtual void Read(size_t num, void * bytes) = 0;
37     virtual void Write(size_t num, const void * bytes) = 0;
38     virtual ~IStream(){}
39 };
40
41 // Serializable interface
42 class ISerializable
43 {
44   public:
45     /*    ISerializable(){};
46      *    ISerializable(IStream&){}; */
47     virtual void Serialize(IStream &) const = 0;
48     virtual ~ISerializable(){}
49 };
50
51 struct Serialization {
52     // serialization
53     // normal functions
54
55     // ISerializable objects
56     static void Serialize(IStream& stream, const ISerializable& object)
57     {
58         object.Serialize(stream);
59     }
60     static void Serialize(IStream& stream, const ISerializable* const object)
61     {
62         object->Serialize(stream);
63     }
64
65     // char
66     static void Serialize(IStream& stream, const char value)
67     {
68         stream.Write(sizeof(value), &value);
69     }
70     static void Serialize(IStream& stream, const char* const value)
71     {
72         stream.Write(sizeof(*value), value);
73     }
74
75     // unsigned char
76     static void Serialize(IStream& stream, const unsigned char value)
77     {
78         stream.Write(sizeof(value), &value);
79     }
80     static void Serialize(IStream& stream, const unsigned char* const value)
81     {
82         stream.Write(sizeof(*value), value);
83     }
84
85     // unsigned int
86     static void Serialize(IStream& stream, const unsigned value)
87     {
88         stream.Write(sizeof(value), &value);
89     }
90     static void Serialize(IStream& stream, const unsigned* const value)
91     {
92         stream.Write(sizeof(*value), value);
93     }
94
95     // int
96     static void Serialize(IStream& stream, const int value)
97     {
98         stream.Write(sizeof(value), &value);
99     }
100     static void Serialize(IStream& stream, const int* const value)
101     {
102         stream.Write(sizeof(*value), value);
103     }
104
105     // bool
106     static void Serialize(IStream& stream, const bool value)
107     {
108         stream.Write(sizeof(value), &value);
109     }
110     static void Serialize(IStream& stream, const bool* const value)
111     {
112         stream.Write(sizeof(*value), value);
113     }
114
115     // time_t
116     static void Serialize(IStream& stream, const time_t value)
117     {
118         stream.Write(sizeof(value), &value);
119     }
120     static void Serialize(IStream& stream, const time_t* const value)
121     {
122         stream.Write(sizeof(*value), value);
123     }
124
125     // std::string
126     static void Serialize(IStream& stream, const std::string& str)
127     {
128         int length = str.size();
129         stream.Write(sizeof(length), &length);
130         stream.Write(length, str.c_str());
131     }
132     static void Serialize(IStream& stream, const std::string* const str)
133     {
134         int length = str->size();
135         stream.Write(sizeof(length), &length);
136         stream.Write(length, str->c_str());
137     }
138
139     // STL templates
140
141     // std::list
142     template <typename T>
143     static void Serialize(IStream& stream, const std::list<T>& list)
144     {
145         int length = list.size();
146         stream.Write(sizeof(length), &length);
147         for (typename std::list<T>::const_iterator list_iter = list.begin();
148              list_iter != list.end(); list_iter++)
149         {
150             Serialize(stream, *list_iter);
151         }
152     }
153     template <typename T>
154     static void Serialize(IStream& stream, const std::list<T>* const list)
155     {
156         Serialize(stream, *list);
157     }
158
159     // std::vector
160     template <typename T>
161     static void Serialize(IStream& stream, const std::vector<T>& vec)
162     {
163         int length = vec.size();
164         stream.Write(sizeof(length), &length);
165         for (typename std::vector<T>::const_iterator vec_iter = vec.begin();
166              vec_iter != vec.end(); vec_iter++)
167         {
168             Serialize(stream, *vec_iter);
169         }
170     }
171     template <typename T>
172     static void Serialize(IStream& stream, const std::vector<T>* const vec)
173     {
174         Serialize(stream, *vec);
175     }
176
177     // std::pair
178     template <typename A, typename B>
179     static void Serialize(IStream& stream, const std::pair<A, B>& p)
180     {
181         Serialize(stream, p.first);
182         Serialize(stream, p.second);
183     }
184     template <typename A, typename B>
185     static void Serialize(IStream& stream, const std::pair<A, B>* const p)
186     {
187         Serialize(stream, *p);
188     }
189
190     // std::map
191     template <typename K, typename T>
192     static void Serialize(IStream& stream, const std::map<K, T>& map)
193     {
194         int length = map.size();
195         stream.Write(sizeof(length), &length);
196         typename std::map<K, T>::const_iterator it;
197         for (it = map.begin(); it != map.end(); ++it) {
198             Serialize(stream, (*it).first);
199             Serialize(stream, (*it).second);
200         }
201     }
202     template <typename K, typename T>
203     static void Serialize(IStream& stream, const std::map<K, T>* const map)
204     {
205         Serialize(stream, *map);
206     }
207
208     // std::unique_ptr
209     template <typename T>
210     static void Serialize(IStream& stream, const std::unique_ptr<T>& p)
211     {
212         Serialize(stream, *p);
213     }
214 }; // struct Serialization
215
216 struct Deserialization {
217     // deserialization
218     // normal functions
219
220     // ISerializable objects
221     // T instead of ISerializable is needed to call proper constructor
222     template <typename T>
223     static void Deserialize(IStream& stream, T& object)
224     {
225         object = T(stream);
226     }
227     template <typename T>
228     static void Deserialize(IStream& stream, T*& object)
229     {
230         object = new T(stream);
231     }
232
233     // char
234     static void Deserialize(IStream& stream, char& value)
235     {
236         stream.Read(sizeof(value), &value);
237     }
238     static void Deserialize(IStream& stream, char*& value)
239     {
240         value = new char;
241         stream.Read(sizeof(*value), value);
242     }
243
244     // unsigned char
245     static void Deserialize(IStream& stream, unsigned char& value)
246     {
247         stream.Read(sizeof(value), &value);
248     }
249     static void Deserialize(IStream& stream, unsigned char*& value)
250     {
251         value = new unsigned char;
252         stream.Read(sizeof(*value), value);
253     }
254
255     // unsigned int
256     static void Deserialize(IStream& stream, unsigned& value)
257     {
258         stream.Read(sizeof(value), &value);
259     }
260     static void Deserialize(IStream& stream, unsigned*& value)
261     {
262         value = new unsigned;
263         stream.Read(sizeof(*value), value);
264     }
265
266     // int
267     static void Deserialize(IStream& stream, int& value)
268     {
269         stream.Read(sizeof(value), &value);
270     }
271     static void Deserialize(IStream& stream, int*& value)
272     {
273         value = new int;
274         stream.Read(sizeof(*value), value);
275     }
276
277     // bool
278     static void Deserialize(IStream& stream, bool& value)
279     {
280         stream.Read(sizeof(value), &value);
281     }
282     static void Deserialize(IStream& stream, bool*& value)
283     {
284         value = new bool;
285         stream.Read(sizeof(*value), value);
286     }
287
288     // time_t
289     static void Deserialize(IStream& stream, time_t& value)
290     {
291         stream.Read(sizeof(value), &value);
292     }
293     static void Deserialize(IStream& stream, time_t*& value)
294     {
295         value = new time_t;
296         stream.Read(sizeof(*value), value);
297     }
298
299     // std::string
300     static void Deserialize(IStream& stream, std::string& str)
301     {
302         int length;
303         stream.Read(sizeof(length), &length);
304         char * buf = new char[length + 1];
305         stream.Read(length, buf);
306         buf[length] = 0;
307         str = std::string(buf);
308         delete[] buf;
309     }
310     static void Deserialize(IStream& stream, std::string*& str)
311     {
312         int length;
313         stream.Read(sizeof(length), &length);
314         char * buf = new char[length + 1];
315         stream.Read(length, buf);
316         buf[length] = 0;
317         str = new std::string(buf);
318         delete[] buf;
319     }
320
321     // STL templates
322
323     // std::list
324     template <typename T>
325     static void Deserialize(IStream& stream, std::list<T>& list)
326     {
327         int length;
328         stream.Read(sizeof(length), &length);
329         for (int i = 0; i < length; ++i) {
330             T obj;
331             Deserialize(stream, obj);
332             list.push_back(std::move(obj));
333         }
334     }
335     template <typename T>
336     static void Deserialize(IStream& stream, std::list<T>*& list)
337     {
338         list = new std::list<T>;
339         Deserialize(stream, *list);
340     }
341
342     // std::vector
343     template <typename T>
344     static void Deserialize(IStream& stream, std::vector<T>& vec)
345     {
346         int length;
347         stream.Read(sizeof(length), &length);
348         for (int i = 0; i < length; ++i) {
349             T obj;
350             Deserialize(stream, obj);
351             vec.push_back(std::move(obj));
352         }
353     }
354     template <typename T>
355     static void Deserialize(IStream& stream, std::vector<T>*& vec)
356     {
357         vec = new std::vector<T>;
358         Deserialize(stream, *vec);
359     }
360
361     // std::pair
362     template <typename A, typename B>
363     static void Deserialize(IStream& stream, std::pair<A, B>& p)
364     {
365         Deserialize(stream, p.first);
366         Deserialize(stream, p.second);
367     }
368     template <typename A, typename B>
369     static void Deserialize(IStream& stream, std::pair<A, B>*& p)
370     {
371         p = new std::pair<A, B>;
372         Deserialize(stream, *p);
373     }
374
375     // std::map
376     template <typename K, typename T>
377     static void Deserialize(IStream& stream, std::map<K, T>& map)
378     {
379         int length;
380         stream.Read(sizeof(length), &length);
381         for (int i = 0; i < length; ++i) {
382             K key;
383             T obj;
384             Deserialize(stream, key);
385             Deserialize(stream, obj);
386             map[key] = std::move(obj);
387         }
388     }
389     template <typename K, typename T>
390     static void Deserialize(IStream& stream, std::map<K, T>*& map)
391     {
392         map = new std::map<K, T>;
393         Deserialize(stream, *map);
394     }
395 }; // struct Deserialization
396 } // namespace SecurityManager
397
398 #endif // SERIALIZATION_H