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
31 #include <dpl/raw-buffer.h>
32 // temporary fix for tizen 2.3
33 #include <ckm/ckm-password.h>
36 // Abstract data stream buffer
40 virtual void Read(size_t num, void * bytes) = 0;
41 virtual void Write(size_t num, const void * bytes) = 0;
45 // Serializable interface
50 * ISerializable(IStream&){}; */
51 virtual void Serialize(IStream &) const = 0;
52 virtual ~ISerializable(){}
55 struct Serialization {
59 // ISerializable objects
60 static void Serialize(IStream& stream, const ISerializable& object)
62 object.Serialize(stream);
65 static void Serialize(IStream& stream, const ISerializable* const object)
67 object->Serialize(stream);
71 static void Serialize(IStream& stream, const char value)
73 stream.Write(sizeof(value), &value);
75 static void Serialize(IStream& stream, const char* const value)
77 stream.Write(sizeof(*value), value);
81 static void Serialize(IStream& stream, const unsigned char value)
83 stream.Write(sizeof(value), &value);
85 static void Serialize(IStream& stream, const unsigned char* const value)
87 stream.Write(sizeof(*value), value);
91 static void Serialize(IStream& stream, const unsigned value)
93 stream.Write(sizeof(value), &value);
95 static void Serialize(IStream& stream, const unsigned* const value)
97 stream.Write(sizeof(*value), value);
101 static void Serialize(IStream& stream, const int value)
103 stream.Write(sizeof(value), &value);
105 static void Serialize(IStream& stream, const int* const value)
107 stream.Write(sizeof(*value), value);
111 static void Serialize(IStream& stream, const bool value)
113 stream.Write(sizeof(value), &value);
115 static void Serialize(IStream& stream, const bool* const value)
117 stream.Write(sizeof(*value), value);
121 static void Serialize(IStream& stream, const time_t value)
123 stream.Write(sizeof(value), &value);
125 static void Serialize(IStream& stream, const time_t* const value)
127 stream.Write(sizeof(*value), value);
131 static void Serialize(IStream& stream, const std::string& str)
133 int length = str.size();
134 stream.Write(sizeof(length), &length);
135 stream.Write(length, str.c_str());
137 static void Serialize(IStream& stream, const std::string* const str)
139 int length = str->size();
140 stream.Write(sizeof(length), &length);
141 stream.Write(length, str->c_str());
145 static void Serialize(IStream& stream, const Password& str)
147 int length = str.size();
148 stream.Write(sizeof(length), &length);
149 stream.Write(length, str.c_str());
151 static void Serialize(IStream& stream, const Password* const str)
153 int length = str->size();
154 stream.Write(sizeof(length), &length);
155 stream.Write(length, str->c_str());
161 template <typename T>
162 static void Serialize(IStream& stream, const std::list<T>& list)
164 int length = list.size();
165 stream.Write(sizeof(length), &length);
166 for (typename std::list<T>::const_iterator list_iter = list.begin();
167 list_iter != list.end(); list_iter++)
169 Serialize(stream, *list_iter);
172 template <typename T>
173 static void Serialize(IStream& stream, const std::list<T>* const list)
175 Serialize(stream, *list);
179 template <typename T>
180 static void Serialize(IStream& stream, const std::vector<T>& vec)
182 int length = vec.size();
183 stream.Write(sizeof(length), &length);
184 for (typename std::vector<T>::const_iterator vec_iter = vec.begin();
185 vec_iter != vec.end(); vec_iter++)
187 Serialize(stream, *vec_iter);
190 template <typename T>
191 static void Serialize(IStream& stream, const std::vector<T>* const vec)
193 Serialize(stream, *vec);
197 template <typename A, typename B>
198 static void Serialize(IStream& stream, const std::pair<A, B>& p)
200 Serialize(stream, p.first);
201 Serialize(stream, p.second);
203 template <typename A, typename B>
204 static void Serialize(IStream& stream, const std::pair<A, B>* const p)
206 Serialize(stream, *p);
210 template <typename K, typename T>
211 static void Serialize(IStream& stream, const std::map<K, T>& map)
213 int length = map.size();
214 stream.Write(sizeof(length), &length);
215 typename std::map<K, T>::const_iterator it;
216 for (it = map.begin(); it != map.end(); ++it) {
217 Serialize(stream, (*it).first);
218 Serialize(stream, (*it).second);
221 template <typename K, typename T>
222 static void Serialize(IStream& stream, const std::map<K, T>* const map)
224 Serialize(stream, *map);
228 template <typename T>
229 static void Serialize(IStream& stream, const std::unique_ptr<T>& p)
231 Serialize(stream, *p);
234 static void Serialize(IStream& stream, const RawBuffer& vec)
236 int length = vec.size();
237 stream.Write(sizeof(length), &length);
238 stream.Write(length, vec.data());
241 static void Serialize(IStream& stream, const RawBuffer* const vec)
243 Serialize(stream, *vec);
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)
259 template <typename T>
260 static void Deserialize(IStream& stream, T*& object)
262 object = new T(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, unsigned& value)
290 stream.Read(sizeof(value), &value);
292 static void Deserialize(IStream& stream, unsigned*& value)
294 value = new unsigned;
295 stream.Read(sizeof(*value), value);
299 static void Deserialize(IStream& stream, int& value)
301 stream.Read(sizeof(value), &value);
303 static void Deserialize(IStream& stream, int*& value)
306 stream.Read(sizeof(*value), value);
310 static void Deserialize(IStream& stream, bool& value)
312 stream.Read(sizeof(value), &value);
314 static void Deserialize(IStream& stream, bool*& value)
317 stream.Read(sizeof(*value), value);
321 static void Deserialize(IStream& stream, time_t& value)
323 stream.Read(sizeof(value), &value);
325 static void Deserialize(IStream& stream, time_t*& value)
328 stream.Read(sizeof(*value), value);
332 static void Deserialize(IStream& stream, std::string& str)
335 stream.Read(sizeof(length), &length);
336 char * buf = new char[length + 1];
337 stream.Read(length, buf);
339 str = std::string(buf);
342 static void Deserialize(IStream& stream, std::string*& str)
345 stream.Read(sizeof(length), &length);
346 char * buf = new char[length + 1];
347 stream.Read(length, buf);
349 str = new std::string(buf);
354 static void Deserialize(IStream& stream, Password& str)
357 stream.Read(sizeof(length), &length);
358 char * buf = new char[length + 1];
359 stream.Read(length, buf);
364 static void Deserialize(IStream& stream, Password*& str)
367 stream.Read(sizeof(length), &length);
368 char * buf = new char[length + 1];
369 stream.Read(length, buf);
371 str = new Password(buf);
378 template <typename T>
379 static void Deserialize(IStream& stream, std::list<T>& list)
382 stream.Read(sizeof(length), &length);
383 for (int i = 0; i < length; ++i) {
385 Deserialize(stream, obj);
386 list.push_back(std::move(obj));
389 template <typename T>
390 static void Deserialize(IStream& stream, std::list<T>*& list)
392 list = new std::list<T>;
393 Deserialize(stream, *list);
397 template <typename T>
398 static void Deserialize(IStream& stream, std::vector<T>& vec)
401 stream.Read(sizeof(length), &length);
402 for (int i = 0; i < length; ++i) {
404 Deserialize(stream, obj);
405 vec.push_back(std::move(obj));
408 template <typename T>
409 static void Deserialize(IStream& stream, std::vector<T>*& vec)
411 vec = new std::vector<T>;
412 Deserialize(stream, *vec);
416 template <typename A, typename B>
417 static void Deserialize(IStream& stream, std::pair<A, B>& p)
419 Deserialize(stream, p.first);
420 Deserialize(stream, p.second);
422 template <typename A, typename B>
423 static void Deserialize(IStream& stream, std::pair<A, B>*& p)
425 p = new std::pair<A, B>;
426 Deserialize(stream, *p);
430 template <typename K, typename T>
431 static void Deserialize(IStream& stream, std::map<K, T>& map)
434 stream.Read(sizeof(length), &length);
435 for (int i = 0; i < length; ++i) {
438 Deserialize(stream, key);
439 Deserialize(stream, obj);
440 map[key] = std::move(obj);
443 template <typename K, typename T>
444 static void Deserialize(IStream& stream, std::map<K, T>*& map)
446 map = new std::map<K, T>;
447 Deserialize(stream, *map);
450 static void Deserialize(IStream& stream, RawBuffer& vec)
453 stream.Read(sizeof(length), &length);
455 stream.Read(length, vec.data());
458 static void Deserialize(IStream& stream, RawBuffer*& vec)
461 Deserialize(stream, *vec);
464 }; // struct Deserialization
467 #endif // CENT_KEY_SERIALIZATION_H