Implement and use template methods for serialization of multiple variables
[platform/core/security/security-manager.git] / src / 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
215     // serialize 'em all
216     template<typename T1, typename T2, typename... Tail>
217     static void Serialize(IStream& stream, const T1& first, const T2& second, const Tail&... tail)
218     {
219         Serialize(stream, first);
220         Serialize(stream, second, tail...);
221     }
222 }; // struct Serialization
223
224 struct Deserialization {
225     // deserialization
226     // normal functions
227
228     // ISerializable objects
229     // T instead of ISerializable is needed to call proper constructor
230     template <typename T>
231     static void Deserialize(IStream& stream, T& object)
232     {
233         object = T(stream);
234     }
235     template <typename T>
236     static void Deserialize(IStream& stream, T*& object)
237     {
238         object = new T(stream);
239     }
240
241     // char
242     static void Deserialize(IStream& stream, char& value)
243     {
244         stream.Read(sizeof(value), &value);
245     }
246     static void Deserialize(IStream& stream, char*& value)
247     {
248         value = new char;
249         stream.Read(sizeof(*value), value);
250     }
251
252     // unsigned char
253     static void Deserialize(IStream& stream, unsigned char& value)
254     {
255         stream.Read(sizeof(value), &value);
256     }
257     static void Deserialize(IStream& stream, unsigned char*& value)
258     {
259         value = new unsigned char;
260         stream.Read(sizeof(*value), value);
261     }
262
263     // unsigned int
264     static void Deserialize(IStream& stream, unsigned& value)
265     {
266         stream.Read(sizeof(value), &value);
267     }
268     static void Deserialize(IStream& stream, unsigned*& value)
269     {
270         value = new unsigned;
271         stream.Read(sizeof(*value), value);
272     }
273
274     // int
275     static void Deserialize(IStream& stream, int& value)
276     {
277         stream.Read(sizeof(value), &value);
278     }
279     static void Deserialize(IStream& stream, int*& value)
280     {
281         value = new int;
282         stream.Read(sizeof(*value), value);
283     }
284
285     // bool
286     static void Deserialize(IStream& stream, bool& value)
287     {
288         stream.Read(sizeof(value), &value);
289     }
290     static void Deserialize(IStream& stream, bool*& value)
291     {
292         value = new bool;
293         stream.Read(sizeof(*value), value);
294     }
295
296     // time_t
297     static void Deserialize(IStream& stream, time_t& value)
298     {
299         stream.Read(sizeof(value), &value);
300     }
301     static void Deserialize(IStream& stream, time_t*& value)
302     {
303         value = new time_t;
304         stream.Read(sizeof(*value), value);
305     }
306
307     // std::string
308     static void Deserialize(IStream& stream, std::string& str)
309     {
310         int length;
311         stream.Read(sizeof(length), &length);
312         char * buf = new char[length + 1];
313         stream.Read(length, buf);
314         buf[length] = 0;
315         str = std::string(buf);
316         delete[] buf;
317     }
318     static void Deserialize(IStream& stream, std::string*& str)
319     {
320         int length;
321         stream.Read(sizeof(length), &length);
322         char * buf = new char[length + 1];
323         stream.Read(length, buf);
324         buf[length] = 0;
325         str = new std::string(buf);
326         delete[] buf;
327     }
328
329     // STL templates
330
331     // std::list
332     template <typename T>
333     static void Deserialize(IStream& stream, std::list<T>& list)
334     {
335         int length;
336         stream.Read(sizeof(length), &length);
337         for (int i = 0; i < length; ++i) {
338             T obj;
339             Deserialize(stream, obj);
340             list.push_back(std::move(obj));
341         }
342     }
343     template <typename T>
344     static void Deserialize(IStream& stream, std::list<T>*& list)
345     {
346         list = new std::list<T>;
347         Deserialize(stream, *list);
348     }
349
350     // std::vector
351     template <typename T>
352     static void Deserialize(IStream& stream, std::vector<T>& vec)
353     {
354         int length;
355         stream.Read(sizeof(length), &length);
356         for (int i = 0; i < length; ++i) {
357             T obj;
358             Deserialize(stream, obj);
359             vec.push_back(std::move(obj));
360         }
361     }
362     template <typename T>
363     static void Deserialize(IStream& stream, std::vector<T>*& vec)
364     {
365         vec = new std::vector<T>;
366         Deserialize(stream, *vec);
367     }
368
369     // std::pair
370     template <typename A, typename B>
371     static void Deserialize(IStream& stream, std::pair<A, B>& p)
372     {
373         Deserialize(stream, p.first);
374         Deserialize(stream, p.second);
375     }
376     template <typename A, typename B>
377     static void Deserialize(IStream& stream, std::pair<A, B>*& p)
378     {
379         p = new std::pair<A, B>;
380         Deserialize(stream, *p);
381     }
382
383     // std::map
384     template <typename K, typename T>
385     static void Deserialize(IStream& stream, std::map<K, T>& map)
386     {
387         int length;
388         stream.Read(sizeof(length), &length);
389         for (int i = 0; i < length; ++i) {
390             K key;
391             T obj;
392             Deserialize(stream, key);
393             Deserialize(stream, obj);
394             map[key] = std::move(obj);
395         }
396     }
397     template <typename K, typename T>
398     static void Deserialize(IStream& stream, std::map<K, T>*& map)
399     {
400         map = new std::map<K, T>;
401         Deserialize(stream, *map);
402     }
403 }; // struct Deserialization
404 } // namespace SecurityManager
405
406 #endif // SERIALIZATION_H