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 CENT_KEY_SERIALIZATION_H
23 #define CENT_KEY_SERIALIZATION_H
34 // Abstract data stream buffer
38 virtual void Read(size_t num, void * bytes) = 0;
39 virtual void Write(size_t num, const void * bytes) = 0;
43 // Serializable interface
48 * ISerializable(IStream&){}; */
49 virtual void Serialize(IStream &) const = 0;
50 virtual void Deserialize(IStream &) = 0;
51 virtual ~ISerializable(){}
54 struct Serialization {
58 // ISerializable objects
59 static void Serialize(IStream& stream, const ISerializable& object)
61 object.Serialize(stream);
64 static void Serialize(IStream& stream, const ISerializable* const object)
66 object->Serialize(stream);
70 static void Serialize(IStream& stream, const char value)
72 stream.Write(sizeof(value), &value);
74 static void Serialize(IStream& stream, const char* const value)
76 stream.Write(sizeof(*value), value);
80 static void Serialize(IStream& stream, const unsigned char value)
82 stream.Write(sizeof(value), &value);
84 static void Serialize(IStream& stream, const unsigned char* const value)
86 stream.Write(sizeof(*value), value);
90 static void Serialize(IStream& stream, const uint32_t value)
92 stream.Write(sizeof(value), &value);
94 static void Serialize(IStream& stream, const uint32_t* const value)
96 stream.Write(sizeof(*value), value);
100 static void Serialize(IStream& stream, const int32_t value)
102 stream.Write(sizeof(value), &value);
104 static void Serialize(IStream& stream, const int32_t* const value)
106 stream.Write(sizeof(*value), value);
110 static void Serialize(IStream& stream, const uint64_t value)
112 stream.Write(sizeof(value), &value);
114 static void Serialize(IStream& stream, const uint64_t* const value)
116 stream.Write(sizeof(*value), value);
120 static void Serialize(IStream& stream, const int64_t value)
122 stream.Write(sizeof(value), &value);
124 static void Serialize(IStream& stream, const int64_t* const value)
126 stream.Write(sizeof(*value), value);
130 static void Serialize(IStream& stream, const bool value)
132 stream.Write(sizeof(value), &value);
134 static void Serialize(IStream& stream, const bool* const value)
136 stream.Write(sizeof(*value), value);
140 template <typename T, typename R, typename A>
141 static void Serialize(IStream& stream, const std::basic_string<T,R,A>& str)
143 int length = str.size();
144 stream.Write(sizeof(length), &length);
145 stream.Write(length*sizeof(T), str.data());
148 template<typename T, typename R, typename A>
149 static void Serialize(IStream& stream, const std::basic_string<T,R,A>* const str)
151 int length = str->size();
152 stream.Write(sizeof(length), &length);
153 stream.Write(length*sizeof(T), str->data());
159 template <typename T>
160 static void Serialize(IStream& stream, const std::list<T>& list)
162 int length = list.size();
163 stream.Write(sizeof(length), &length);
164 for (typename std::list<T>::const_iterator list_iter = list.begin();
165 list_iter != list.end(); list_iter++)
167 Serialize(stream, *list_iter);
170 template <typename T>
171 static void Serialize(IStream& stream, const std::list<T>* const list)
173 Serialize(stream, *list);
177 template <typename A>
178 static void Serialize(IStream& stream, const std::vector<unsigned char, A>& vec)
180 int length = vec.size();
181 stream.Write(sizeof(length), &length);
182 stream.Write(length, vec.data());
185 template <typename A>
186 static void Serialize(IStream& stream, const std::vector<unsigned char, A>* const vec)
188 Serialize(stream, *vec);
192 template <typename T, typename A>
193 static void Serialize(IStream& stream, const std::vector<T, A>& vec)
195 int length = vec.size();
196 stream.Write(sizeof(length), &length);
197 for (const auto &i : vec)
199 Serialize(stream, i);
202 template <typename T, typename A>
203 static void Serialize(IStream& stream, const std::vector<T, A>* const vec)
205 Serialize(stream, *vec);
209 template <typename A, typename B>
210 static void Serialize(IStream& stream, const std::pair<A, B>& p)
212 Serialize(stream, p.first);
213 Serialize(stream, p.second);
215 template <typename A, typename B>
216 static void Serialize(IStream& stream, const std::pair<A, B>* const p)
218 Serialize(stream, *p);
222 template <typename K, typename T>
223 static void Serialize(IStream& stream, const std::map<K, T>& map)
225 int length = map.size();
226 stream.Write(sizeof(length), &length);
227 typename std::map<K, T>::const_iterator it;
228 for (it = map.begin(); it != map.end(); ++it) {
229 Serialize(stream, (*it).first);
230 Serialize(stream, (*it).second);
233 template <typename K, typename T>
234 static void Serialize(IStream& stream, const std::map<K, T>* const map)
236 Serialize(stream, *map);
240 template <typename T>
241 static void Serialize(IStream& stream, const std::unique_ptr<T>& p)
243 Serialize(stream, *p);
246 }; // struct Serialization
248 struct Deserialization {
252 // ISerializable objects
253 // T instead of ISerializable is needed to call proper constructor
254 template <typename T>
255 static void Deserialize(IStream& stream, T& object)
257 object.Deserialize(stream);
259 template <typename T>
260 static void Deserialize(IStream& stream, T*& object)
262 object->Deserialize(stream);
266 static void Deserialize(IStream& stream, char& value)
268 stream.Read(sizeof(value), &value);
270 static void Deserialize(IStream& stream, char*& value)
273 stream.Read(sizeof(*value), value);
277 static void Deserialize(IStream& stream, unsigned char& value)
279 stream.Read(sizeof(value), &value);
281 static void Deserialize(IStream& stream, unsigned char*& value)
283 value = new unsigned char;
284 stream.Read(sizeof(*value), value);
288 static void Deserialize(IStream& stream, uint32_t& value)
290 stream.Read(sizeof(value), &value);
292 static void Deserialize(IStream& stream, uint32_t*& value)
294 value = new uint32_t;
295 stream.Read(sizeof(*value), value);
299 static void Deserialize(IStream& stream, int32_t& value)
301 stream.Read(sizeof(value), &value);
303 static void Deserialize(IStream& stream, int32_t*& value)
306 stream.Read(sizeof(*value), value);
310 static void Deserialize(IStream& stream, uint64_t& value)
312 stream.Read(sizeof(value), &value);
314 static void Deserialize(IStream& stream, uint64_t*& value)
316 value = new uint64_t;
317 stream.Read(sizeof(*value), value);
321 static void Deserialize(IStream& stream, int64_t& value)
323 stream.Read(sizeof(value), &value);
325 static void Deserialize(IStream& stream, int64_t*& value)
328 stream.Read(sizeof(*value), value);
332 static void Deserialize(IStream& stream, bool& value)
334 stream.Read(sizeof(value), &value);
336 static void Deserialize(IStream& stream, bool*& value)
339 stream.Read(sizeof(*value), value);
342 template <typename T, typename R, typename A>
343 static void Deserialize(IStream& stream, std::basic_string<T,R,A>& str)
346 stream.Read(sizeof(length), &length);
347 std::vector<T> buf(length);
348 stream.Read(length*sizeof(T), buf.data());
349 str = std::basic_string<T,R,A>(buf.data(), buf.data()+length);
352 template <typename T, typename R, typename A>
353 static void Deserialize(IStream& stream, std::basic_string<T,R,A>*& str)
356 stream.Read(sizeof(length), &length);
357 std::vector<T> buf(length);
358 stream.Read(length*sizeof(T), buf.data());
359 str = new std::basic_string<T,R,A>(buf.data(), buf.data()+length);
365 template <typename T>
366 static void Deserialize(IStream& stream, std::list<T>& list)
369 stream.Read(sizeof(length), &length);
370 for (int i = 0; i < length; ++i) {
372 Deserialize(stream, obj);
373 list.push_back(std::move(obj));
376 template <typename T>
377 static void Deserialize(IStream& stream, std::list<T>*& list)
379 list = new std::list<T>;
380 Deserialize(stream, *list);
384 template <typename A>
385 static void Deserialize(IStream& stream, std::vector<unsigned char, A>& vec)
388 stream.Read(sizeof(length), &length);
390 stream.Read(length, vec.data());
393 template <typename A>
394 static void Deserialize(IStream& stream, std::vector<unsigned char, A>*& vec)
396 vec = new std::vector<unsigned char,A>;
397 Deserialize(stream, *vec);
401 template <typename T, typename A>
402 static void Deserialize(IStream& stream, std::vector<T,A>& vec)
405 stream.Read(sizeof(length), &length);
406 for (int i = 0; i < length; ++i) {
408 Deserialize(stream, obj);
409 vec.push_back(std::move(obj));
412 template <typename T, typename A>
413 static void Deserialize(IStream& stream, std::vector<T,A>*& vec)
415 vec = new std::vector<T,A>;
416 Deserialize(stream, *vec);
420 template <typename A, typename B>
421 static void Deserialize(IStream& stream, std::pair<A, B>& p)
423 Deserialize(stream, p.first);
424 Deserialize(stream, p.second);
426 template <typename A, typename B>
427 static void Deserialize(IStream& stream, std::pair<A, B>*& p)
429 p = new std::pair<A, B>;
430 Deserialize(stream, *p);
434 template <typename K, typename T>
435 static void Deserialize(IStream& stream, std::map<K, T>& map)
438 stream.Read(sizeof(length), &length);
439 for (int i = 0; i < length; ++i) {
442 Deserialize(stream, key);
443 Deserialize(stream, obj);
444 map[key] = std::move(obj);
447 template <typename K, typename T>
448 static void Deserialize(IStream& stream, std::map<K, T>*& map)
450 map = new std::map<K, T>;
451 Deserialize(stream, *map);
453 }; // struct Deserialization
455 // generic serialization
456 template <typename... Args>
459 template <typename First, typename... Args>
460 struct Serializer<First, Args...> : public Serializer<Args...> {
461 static void Serialize(IStream& stream, const First& f, const Args&... args) {
462 Serialization::Serialize(stream, f);
463 Serializer<Args...>::Serialize(stream, args...);
469 struct Serializer<> {
470 static void Serialize(IStream&) {
475 // generic deserialization
476 template <typename... Args>
479 template <typename First, typename... Args>
480 struct Deserializer<First, Args...> : public Deserializer<Args...> {
481 static void Deserialize(IStream& stream, First& f, Args&... args) {
482 Deserialization::Deserialize(stream, f);
483 Deserializer<Args...>::Deserialize(stream, args...);
489 struct Deserializer<> {
490 static void Deserialize(IStream&) {
497 #endif // CENT_KEY_SERIALIZATION_H