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
32 // Abstract data stream buffer
35 virtual void Read(size_t num, void * bytes) = 0;
36 virtual void Write(size_t num, const void * bytes) = 0;
40 // Serializable interface
44 ISerializable(IStream&){}; */
45 virtual void Serialize(IStream &) const = 0;
46 virtual ~ISerializable(){};
49 struct Serialization {
53 // ISerializable objects
54 static void Serialize(IStream& stream, const ISerializable& object){
55 object.Serialize(stream);
57 static void Serialize(IStream& stream, const ISerializable* const object){
58 object->Serialize(stream);
62 static void Serialize(IStream& stream, const unsigned value){
63 stream.Write(sizeof(value),&value);
65 static void Serialize(IStream& stream, const unsigned* const value){
66 stream.Write(sizeof(*value),value);
70 static void Serialize(IStream& stream, const int value){
71 stream.Write(sizeof(value),&value);
73 static void Serialize(IStream& stream, const int* const value){
74 stream.Write(sizeof(*value),value);
78 static void Serialize(IStream& stream, const bool value){
79 stream.Write(sizeof(value),&value);
81 static void Serialize(IStream& stream, const bool* const value){
82 stream.Write(sizeof(*value),value);
86 static void Serialize(IStream& stream, const std::string& str){
87 int length = str.size();
88 stream.Write(sizeof(length),&length);
89 stream.Write(length,str.c_str());
91 static void Serialize(IStream& stream, const std::string* const str){
92 int length = str->size();
93 stream.Write(sizeof(length),&length);
94 stream.Write(length,str->c_str());
100 template <typename T>
101 static void Serialize(IStream& stream, const std::list<T>& list){
102 int length = list.size();
103 stream.Write(sizeof(length),&length);
104 for(typename std::list<T>::const_iterator list_iter = list.begin();
105 list_iter != list.end(); list_iter++)
107 Serialize(stream, *list_iter);
110 template <typename T>
111 static void Serialize(IStream& stream, const std::list<T>* const list){
112 Serialize(stream,*list);
116 template <typename T>
117 static void Serialize(IStream& stream, const std::vector<T>& vec){
118 int length = vec.size();
119 stream.Write(sizeof(length),&length);
120 for(typename std::vector<T>::const_iterator vec_iter = vec.begin();
121 vec_iter != vec.end(); vec_iter ++)
123 Serialize(stream, *vec_iter);
126 template <typename T>
127 static void Serialize(IStream& stream, const std::vector<T>* const vec){
128 Serialize(stream,*vec);
132 template <typename A, typename B>
133 static void Serialize(IStream& stream, const std::pair<A,B>& p){
134 Serialize(stream, p.first);
135 Serialize(stream, p.second);
137 template <typename A, typename B>
138 static void Serialize(IStream& stream, const std::pair<A,B>* const p){
139 Serialize(stream,*p);
143 template <typename K, typename T>
144 static void Serialize(IStream& stream, const std::map<K,T>& map){
145 int length = map.size();
146 stream.Write(sizeof(length),&length);
147 typename std::map<K,T>::const_iterator it;
148 for (it = map.begin(); it != map.end(); ++it) {
149 Serialize(stream,(*it).first);
150 Serialize(stream,(*it).second);
153 template <typename K, typename T>
154 static void Serialize(IStream& stream, const std::map<K,T>* const map){
155 Serialize(stream,*map);
157 }; // struct Serialization
159 struct Deserialization {
163 // ISerializable objects
164 // T instead of ISerializable is needed to call proper constructor
165 template <typename T>
166 static void Deserialize(IStream& stream, T& object){
169 template <typename T>
170 static void Deserialize(IStream& stream, T*& object){
171 object = new T(stream);
175 static void Deserialize(IStream& stream, unsigned& value){
176 stream.Read(sizeof(value),&value);
178 static void Deserialize(IStream& stream, unsigned*& value){
179 value = new unsigned;
180 stream.Read(sizeof(*value),value);
184 static void Deserialize(IStream& stream, int& value){
185 stream.Read(sizeof(value),&value);
187 static void Deserialize(IStream& stream, int*& value){
189 stream.Read(sizeof(*value),value);
193 static void Deserialize(IStream& stream, bool& value){
194 stream.Read(sizeof(value),&value);
196 static void Deserialize(IStream& stream, bool*& value){
198 stream.Read(sizeof(*value),value);
202 static void Deserialize(IStream& stream, std::string& str){
204 stream.Read(sizeof(length),&length);
205 char * buf = new char[length+1];
206 stream.Read(length,buf);
208 str = std::string(buf);
211 static void Deserialize(IStream& stream, std::string*& str){
213 stream.Read(sizeof(length),&length);
214 char * buf = new char[length+1];
215 stream.Read(length,buf);
217 str = new std::string(buf);
224 template <typename T>
225 static void Deserialize(IStream& stream, std::list<T>& list){
227 stream.Read(sizeof(length),&length);
228 for (int i = 0; i < length; ++i) {
230 Deserialize(stream, obj);
234 template <typename T>
235 static void Deserialize(IStream& stream, std::list<T>*& list){
236 list = new std::list<T>;
237 Deserialize(stream,*list);
241 template <typename T>
242 static void Deserialize(IStream& stream, std::vector<T>& vec){
244 stream.Read(sizeof(length),&length);
245 for (int i = 0; i < length; ++i) {
247 Deserialize(stream, obj);
251 template <typename T>
252 static void Deserialize(IStream& stream, std::vector<T>*& vec){
253 vec = new std::vector<T>;
254 Deserialize(stream,*vec);
258 template <typename A, typename B>
259 static void Deserialize(IStream& stream, std::pair<A,B>& p){
260 Deserialize(stream, p.first);
261 Deserialize(stream, p.second);
263 template <typename A, typename B>
264 static void Deserialize(IStream& stream, std::pair<A,B>*& p){
265 p = new std::pair<A,B>;
266 Deserialize(stream,*p);
270 template <typename K, typename T>
271 static void Deserialize(IStream& stream, std::map<K,T>& map){
273 stream.Read(sizeof(length),&length);
274 for (int i = 0; i < length; ++i) {
277 Deserialize(stream,key);
278 Deserialize(stream,obj);
282 template <typename K, typename T>
283 static void Deserialize(IStream& stream, std::map<K,T>*& map){
284 map = new std::map<K,T>;
285 Deserialize(stream,*map);
287 }; // struct Deserialization
291 #endif // SERIALIZATION_H