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);
216 template<typename T1, typename T2, typename... Tail>
217 static void Serialize(IStream& stream, const T1& first, const T2& second, const Tail&... tail)
219 Serialize(stream, first);
220 Serialize(stream, second, tail...);
222 }; // struct Serialization
224 struct Deserialization {
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)
235 template <typename T>
236 static void Deserialize(IStream& stream, T*& object)
238 object = new T(stream);
242 static void Deserialize(IStream& stream, char& value)
244 stream.Read(sizeof(value), &value);
246 static void Deserialize(IStream& stream, char*& value)
249 stream.Read(sizeof(*value), value);
253 static void Deserialize(IStream& stream, unsigned char& value)
255 stream.Read(sizeof(value), &value);
257 static void Deserialize(IStream& stream, unsigned char*& value)
259 value = new unsigned char;
260 stream.Read(sizeof(*value), value);
264 static void Deserialize(IStream& stream, unsigned& value)
266 stream.Read(sizeof(value), &value);
268 static void Deserialize(IStream& stream, unsigned*& value)
270 value = new unsigned;
271 stream.Read(sizeof(*value), value);
275 static void Deserialize(IStream& stream, int& value)
277 stream.Read(sizeof(value), &value);
279 static void Deserialize(IStream& stream, int*& value)
282 stream.Read(sizeof(*value), value);
286 static void Deserialize(IStream& stream, bool& value)
288 stream.Read(sizeof(value), &value);
290 static void Deserialize(IStream& stream, bool*& value)
293 stream.Read(sizeof(*value), value);
297 static void Deserialize(IStream& stream, time_t& value)
299 stream.Read(sizeof(value), &value);
301 static void Deserialize(IStream& stream, time_t*& value)
304 stream.Read(sizeof(*value), value);
308 static void Deserialize(IStream& stream, std::string& str)
311 stream.Read(sizeof(length), &length);
312 char * buf = new char[length + 1];
313 stream.Read(length, buf);
315 str = std::string(buf);
318 static void Deserialize(IStream& stream, std::string*& str)
321 stream.Read(sizeof(length), &length);
322 char * buf = new char[length + 1];
323 stream.Read(length, buf);
325 str = new std::string(buf);
332 template <typename T>
333 static void Deserialize(IStream& stream, std::list<T>& list)
336 stream.Read(sizeof(length), &length);
337 for (int i = 0; i < length; ++i) {
339 Deserialize(stream, obj);
340 list.push_back(std::move(obj));
343 template <typename T>
344 static void Deserialize(IStream& stream, std::list<T>*& list)
346 list = new std::list<T>;
347 Deserialize(stream, *list);
351 template <typename T>
352 static void Deserialize(IStream& stream, std::vector<T>& vec)
355 stream.Read(sizeof(length), &length);
356 for (int i = 0; i < length; ++i) {
358 Deserialize(stream, obj);
359 vec.push_back(std::move(obj));
362 template <typename T>
363 static void Deserialize(IStream& stream, std::vector<T>*& vec)
365 vec = new std::vector<T>;
366 Deserialize(stream, *vec);
370 template <typename A, typename B>
371 static void Deserialize(IStream& stream, std::pair<A, B>& p)
373 Deserialize(stream, p.first);
374 Deserialize(stream, p.second);
376 template <typename A, typename B>
377 static void Deserialize(IStream& stream, std::pair<A, B>*& p)
379 p = new std::pair<A, B>;
380 Deserialize(stream, *p);
384 template <typename K, typename T>
385 static void Deserialize(IStream& stream, std::map<K, T>& map)
388 stream.Read(sizeof(length), &length);
389 for (int i = 0; i < length; ++i) {
392 Deserialize(stream, key);
393 Deserialize(stream, obj);
394 map[key] = std::move(obj);
397 template <typename K, typename T>
398 static void Deserialize(IStream& stream, std::map<K, T>*& map)
400 map = new std::map<K, T>;
401 Deserialize(stream, *map);
403 }; // struct Deserialization
404 } // namespace SecurityManager
406 #endif // SERIALIZATION_H