2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file serialization.h
18 * @author Tomasz Swierczek (t.swierczek@samsung.com)
20 * @brief Interfaces and templates used for data serialization.
22 #ifndef SERIALIZATION_H
23 #define SERIALIZATION_H
31 namespace SecurityManager {
32 // Abstract data stream buffer
36 virtual void Read(size_t num, void * bytes) = 0;
37 virtual void Write(size_t num, const void * bytes) = 0;
41 // Serializable interface
46 * ISerializable(IStream&){}; */
47 virtual void Serialize(IStream &) const = 0;
48 virtual ~ISerializable(){}
51 struct Serialization {
55 // ISerializable objects
56 static void Serialize(IStream& stream, const ISerializable& object)
58 object.Serialize(stream);
60 static void Serialize(IStream& stream, const ISerializable* const object)
62 object->Serialize(stream);
66 static void Serialize(IStream& stream, const char value)
68 stream.Write(sizeof(value), &value);
70 static void Serialize(IStream& stream, const char* const value)
72 stream.Write(sizeof(*value), value);
76 static void Serialize(IStream& stream, const unsigned char value)
78 stream.Write(sizeof(value), &value);
80 static void Serialize(IStream& stream, const unsigned char* const value)
82 stream.Write(sizeof(*value), value);
86 static void Serialize(IStream& stream, const unsigned value)
88 stream.Write(sizeof(value), &value);
90 static void Serialize(IStream& stream, const unsigned* const value)
92 stream.Write(sizeof(*value), value);
96 static void Serialize(IStream& stream, const int value)
98 stream.Write(sizeof(value), &value);
100 static void Serialize(IStream& stream, const int* const value)
102 stream.Write(sizeof(*value), value);
106 static void Serialize(IStream& stream, const bool value)
108 stream.Write(sizeof(value), &value);
110 static void Serialize(IStream& stream, const bool* const value)
112 stream.Write(sizeof(*value), value);
116 static void Serialize(IStream& stream, const time_t value)
118 stream.Write(sizeof(value), &value);
120 static void Serialize(IStream& stream, const time_t* const value)
122 stream.Write(sizeof(*value), value);
126 static void Serialize(IStream& stream, const std::string& str)
128 int length = str.size();
129 stream.Write(sizeof(length), &length);
130 stream.Write(length, str.c_str());
132 static void Serialize(IStream& stream, const std::string* const str)
134 int length = str->size();
135 stream.Write(sizeof(length), &length);
136 stream.Write(length, str->c_str());
142 template <typename T>
143 static void Serialize(IStream& stream, const std::list<T>& list)
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++)
150 Serialize(stream, *list_iter);
153 template <typename T>
154 static void Serialize(IStream& stream, const std::list<T>* const list)
156 Serialize(stream, *list);
160 template <typename T>
161 static void Serialize(IStream& stream, const std::vector<T>& vec)
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++)
168 Serialize(stream, *vec_iter);
171 template <typename T>
172 static void Serialize(IStream& stream, const std::vector<T>* const vec)
174 Serialize(stream, *vec);
178 template <typename A, typename B>
179 static void Serialize(IStream& stream, const std::pair<A, B>& p)
181 Serialize(stream, p.first);
182 Serialize(stream, p.second);
184 template <typename A, typename B>
185 static void Serialize(IStream& stream, const std::pair<A, B>* const p)
187 Serialize(stream, *p);
191 template <typename K, typename T>
192 static void Serialize(IStream& stream, const std::map<K, T>& map)
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);
202 template <typename K, typename T>
203 static void Serialize(IStream& stream, const std::map<K, T>* const map)
205 Serialize(stream, *map);
209 template <typename T>
210 static void Serialize(IStream& stream, const std::unique_ptr<T>& p)
212 Serialize(stream, *p);
214 }; // struct Serialization
216 struct Deserialization {
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)
227 template <typename T>
228 static void Deserialize(IStream& stream, T*& object)
230 object = new T(stream);
234 static void Deserialize(IStream& stream, char& value)
236 stream.Read(sizeof(value), &value);
238 static void Deserialize(IStream& stream, char*& value)
241 stream.Read(sizeof(*value), value);
245 static void Deserialize(IStream& stream, unsigned char& value)
247 stream.Read(sizeof(value), &value);
249 static void Deserialize(IStream& stream, unsigned char*& value)
251 value = new unsigned char;
252 stream.Read(sizeof(*value), value);
256 static void Deserialize(IStream& stream, unsigned& value)
258 stream.Read(sizeof(value), &value);
260 static void Deserialize(IStream& stream, unsigned*& value)
262 value = new unsigned;
263 stream.Read(sizeof(*value), value);
267 static void Deserialize(IStream& stream, int& value)
269 stream.Read(sizeof(value), &value);
271 static void Deserialize(IStream& stream, int*& value)
274 stream.Read(sizeof(*value), value);
278 static void Deserialize(IStream& stream, bool& value)
280 stream.Read(sizeof(value), &value);
282 static void Deserialize(IStream& stream, bool*& value)
285 stream.Read(sizeof(*value), value);
289 static void Deserialize(IStream& stream, time_t& value)
291 stream.Read(sizeof(value), &value);
293 static void Deserialize(IStream& stream, time_t*& value)
296 stream.Read(sizeof(*value), value);
300 static void Deserialize(IStream& stream, std::string& str)
303 stream.Read(sizeof(length), &length);
304 char * buf = new char[length + 1];
305 stream.Read(length, buf);
307 str = std::string(buf);
310 static void Deserialize(IStream& stream, std::string*& str)
313 stream.Read(sizeof(length), &length);
314 char * buf = new char[length + 1];
315 stream.Read(length, buf);
317 str = new std::string(buf);
324 template <typename T>
325 static void Deserialize(IStream& stream, std::list<T>& list)
328 stream.Read(sizeof(length), &length);
329 for (int i = 0; i < length; ++i) {
331 Deserialize(stream, obj);
332 list.push_back(std::move(obj));
335 template <typename T>
336 static void Deserialize(IStream& stream, std::list<T>*& list)
338 list = new std::list<T>;
339 Deserialize(stream, *list);
343 template <typename T>
344 static void Deserialize(IStream& stream, std::vector<T>& vec)
347 stream.Read(sizeof(length), &length);
348 for (int i = 0; i < length; ++i) {
350 Deserialize(stream, obj);
351 vec.push_back(std::move(obj));
354 template <typename T>
355 static void Deserialize(IStream& stream, std::vector<T>*& vec)
357 vec = new std::vector<T>;
358 Deserialize(stream, *vec);
362 template <typename A, typename B>
363 static void Deserialize(IStream& stream, std::pair<A, B>& p)
365 Deserialize(stream, p.first);
366 Deserialize(stream, p.second);
368 template <typename A, typename B>
369 static void Deserialize(IStream& stream, std::pair<A, B>*& p)
371 p = new std::pair<A, B>;
372 Deserialize(stream, *p);
376 template <typename K, typename T>
377 static void Deserialize(IStream& stream, std::map<K, T>& map)
380 stream.Read(sizeof(length), &length);
381 for (int i = 0; i < length; ++i) {
384 Deserialize(stream, key);
385 Deserialize(stream, obj);
386 map[key] = std::move(obj);
389 template <typename K, typename T>
390 static void Deserialize(IStream& stream, std::map<K, T>*& map)
392 map = new std::map<K, T>;
393 Deserialize(stream, *map);
395 }; // struct Deserialization
396 } // namespace SecurityManager
398 #endif // SERIALIZATION_H