Debian Codegraph @

src/dmlc-core/include/dmlc/serializer.h

raw symbols SCIP
1 /*!
2 * Copyright (c) 2015 by Contributors
3 * \file serializer.h
4 * \brief serializer template class that helps serialization.
5 * This file do not need to be directly used by most user.
6 */
7 #ifndef DMLC_SERIALIZER_H_
8 #define DMLC_SERIALIZER_H_
9
10 #include <deque>
11 #include <list>
12 #include <map>
13 #include <set>
14 #include <string>
15 #include <utility>
16 #include <vector>
17
18 #include "./base.h"
19 #include "./endian.h"
20 #include "./io.h"
21 #include "./logging.h"
22 #include "./type_traits.h"
23
24 #if DMLC_USE_CXX11
25 #include <unordered_map>
26 #include <unordered_set>
27 #endif
28
29 namespace dmlc {
30 /*! \brief internal namespace for serializers */
31 namespace serializer {
32 /*!
33 * \brief generic serialization handler
34 * \tparam T the type to be serialized
35 * \tparam need_endian_swap Whether use little endian
36 */
37 template <typename T>
38 struct Handler;
39
40 //! \cond Doxygen_Suppress
41 /*!
42 * \brief Serializer that redirect calls by condition
43 * \tparam cond the condition
44 * \tparam Then the serializer used for then condition
45 * \tparam Else the serializer used for else condition
46 * \tparam Return the type of data the serializer handles
47 */
48 template <bool cond, typename Then, typename Else, typename Return>
49 struct IfThenElse;
50
51 template <typename Then, typename Else, typename T>
52 struct IfThenElse<true, Then, Else, T> {
53 inline static void Write(Stream *strm, const T &data) {
54 Then::Write(strm, data);
55 }
56 inline static bool Read(Stream *strm, T *data) {
57 return Then::Read(strm, data);
58 }
59 };
60 template <typename Then, typename Else, typename T>
61 struct IfThenElse<false, Then, Else, T> {
62 inline static void Write(Stream *strm, const T &data) {
63 Else::Write(strm, data);
64 }
65 inline static bool Read(Stream *strm, T *data) {
66 return Else::Read(strm, data);
67 }
68 };
69
70 /*! \brief Serializer for POD(plain-old-data) data */
71 template <typename T>
72 struct NativePODHandler {
73 inline static void Write(Stream *strm, const T &data) {
74 strm->Write(&data, sizeof(T));
75 }
76 inline static bool Read(Stream *strm, T *dptr) {
77 return strm->Read((void *)dptr, sizeof(T)) == sizeof(T); // NOLINT(*)
78 }
79 };
80
81 /*! \brief Serializer for arithmetic data, handle endianness */
82 template <typename T>
83 struct ArithmeticHandler {
84 inline static void Write(Stream *strm, const T &data) {
85 if (DMLC_IO_NO_ENDIAN_SWAP) {
86 strm->Write(&data, sizeof(T));
87 } else {
88 T copy = data;
89 ByteSwap(&copy, sizeof(T), 1);
90 strm->Write(&copy, sizeof(T));
91 }
92 }
93 inline static bool Read(Stream *strm, T *dptr) {
94 bool ret = strm->Read((void *)dptr, sizeof(T)) == sizeof(T); // NOLINT(*)
95 if (!DMLC_IO_NO_ENDIAN_SWAP) {
96 ByteSwap(dptr, sizeof(T), 1);
97 }
98 return ret;
99 }
100 };
101
102 // serializer for class that have save/load function
103 template <typename T>
104 struct SaveLoadClassHandler {
105 inline static void Write(Stream *strm, const T &data) {
106 data.Save(strm);
107 }
108 inline static bool Read(Stream *strm, T *data) {
109 return data->Load(strm);
110 }
111 };
112
113 /*!
114 * \brief dummy class for undefined serialization.
115 * This is used to generate error message when user tries to
116 * serialize something that is not supported.
117 * \tparam T the type to be serialized
118 */
119 template <typename T>
120 struct UndefinedSerializerFor {};
121
122 /*!
123 * \brief Serializer handler for std::vector<T> where T is POD type.
124 * \tparam T element type
125 */
126 template <typename T>
127 struct NativePODVectorHandler {
128 inline static void Write(Stream *strm, const std::vector<T> &vec) {
129 uint64_t sz = static_cast<uint64_t>(vec.size());
130 strm->Write<uint64_t>(sz);
131 if (sz != 0) {
132 strm->Write(&vec[0], sizeof(T) * vec.size());
133 }
134 }
135 inline static bool Read(Stream *strm, std::vector<T> *out_vec) {
136 uint64_t sz;
137 if (!strm->Read<uint64_t>(&sz)) {
138 return false;
139 }
140 size_t size = static_cast<size_t>(sz);
141 out_vec->resize(size);
142 if (sz != 0) {
143 size_t nbytes = sizeof(T) * size;
144 return strm->Read(&(*out_vec)[0], nbytes) == nbytes;
145 }
146 return true;
147 }
148 };
149
150 /*!
151 * \brief Serializer handler for std::vector<T> where T can be composed type
152 * \tparam T element type
153 */
154 template <typename T>
155 struct ComposeVectorHandler {
156 inline static void Write(Stream *strm, const std::vector<T> &vec) {
157 uint64_t sz = static_cast<uint64_t>(vec.size());
158 strm->Write<uint64_t>(sz);
159 strm->WriteArray(dmlc::BeginPtr(vec), vec.size());
160 }
161 inline static bool Read(Stream *strm, std::vector<T> *out_vec) {
162 uint64_t sz;
163 if (!strm->Read<uint64_t>(&sz)) {
164 return false;
165 }
166 size_t size = static_cast<size_t>(sz);
167 out_vec->resize(size);
168 return strm->ReadArray(dmlc::BeginPtr(*out_vec), size);
169 }
170 };
171
172 /*!
173 * \brief Serializer handler for std::basic_string<T> where T is POD type.
174 * \tparam T element type
175 */
176 template <typename T>
177 struct NativePODStringHandler {
178 inline static void Write(Stream *strm, const std::basic_string<T> &vec) {
179 uint64_t sz = static_cast<uint64_t>(vec.length());
180 strm->Write<uint64_t>(sz);
181 if (sz != 0) {
182 strm->Write(&vec[0], sizeof(T) * vec.length());
183 }
184 }
185 inline static bool Read(Stream *strm, std::basic_string<T> *out_vec) {
186 uint64_t sz;
187 if (!strm->Read<uint64_t>(&sz)) {
188 return false;
189 }
190 size_t size = static_cast<size_t>(sz);
191 out_vec->resize(size);
192 if (sz != 0) {
193 size_t nbytes = sizeof(T) * size;
194 return strm->Read(&(*out_vec)[0], nbytes) == nbytes;
195 }
196 return true;
197 }
198 };
199
200 /*! \brief Serializer for std::pair */
201 template <typename TA, typename TB>
202 struct PairHandler {
203 inline static void Write(Stream *strm, const std::pair<TA, TB> &data) {
204 Handler<TA>::Write(strm, data.first);
205 Handler<TB>::Write(strm, data.second);
206 }
207 inline static bool Read(Stream *strm, std::pair<TA, TB> *data) {
208 return Handler<TA>::Read(strm, &(data->first)) && Handler<TB>::Read(strm, &(data->second));
209 }
210 };
211
212 // set type handler that can handle most collection type case
213 template <typename ContainerType, typename ElemType>
214 struct CollectionHandler {
215 inline static void Write(Stream *strm, const ContainerType &data) {
216 // dump data to vector
217 std::vector<ElemType> vdata(data.begin(), data.end());
218 // serialize the vector
219 Handler<std::vector<ElemType>>::Write(strm, vdata);
220 }
221 inline static bool Read(Stream *strm, ContainerType *data) {
222 std::vector<ElemType> vdata;
223 if (!Handler<std::vector<ElemType>>::Read(strm, &vdata)) {
224 return false;
225 }
226 data->clear();
227 data->insert(vdata.begin(), vdata.end());
228 return true;
229 }
230 };
231
232 // handler that can handle most list type case
233 // this type insert function takes additional iterator
234 template <typename ListType>
235 struct ListHandler {
236 inline static void Write(Stream *strm, const ListType &data) {
237 typedef typename ListType::value_type ElemType;
238 // dump data to vector
239 std::vector<ElemType> vdata(data.begin(), data.end());
240 // serialize the vector
241 Handler<std::vector<ElemType>>::Write(strm, vdata);
242 }
243 inline static bool Read(Stream *strm, ListType *data) {
244 typedef typename ListType::value_type ElemType;
245 std::vector<ElemType> vdata;
246 if (!Handler<std::vector<ElemType>>::Read(strm, &vdata)) {
247 return false;
248 }
249 data->clear();
250 data->insert(data->begin(), vdata.begin(), vdata.end());
251 return true;
252 }
253 };
254
255 //! \endcond
256
257 /*!
258 * \brief generic serialization handler for type T
259 *
260 * User can define specialization of this class to support
261 * composite serialization of their own class.
262 *
263 * \tparam T the type to be serialized
264 */
265 template <typename T>
266 struct Handler {
267 /*!
268 * \brief write data to stream
269 * \param strm the stream we write the data.
270 * \param data the data obeject to be serialized
271 */
272 inline static void Write(Stream *strm, const T &data) {
273 IfThenElse<dmlc::is_arithmetic<T>::value, ArithmeticHandler<T>,
274 IfThenElse<dmlc::is_pod<T>::value && DMLC_IO_NO_ENDIAN_SWAP, NativePODHandler<T>,
275 IfThenElse<dmlc::has_saveload<T>::value, SaveLoadClassHandler<T>,
276 UndefinedSerializerFor<T>, T>,
277 T>,
278 T>::Write(strm, data);
279 }
280 /*!
281 * \brief read data to stream
282 * \param strm the stream to read the data.
283 * \param data the pointer to the data obeject to read
284 * \return whether the read is successful
285 */
286 inline static bool Read(Stream *strm, T *data) {
287 return IfThenElse<dmlc::is_arithmetic<T>::value, ArithmeticHandler<T>,
288 IfThenElse<dmlc::is_pod<T>::value && DMLC_IO_NO_ENDIAN_SWAP, NativePODHandler<T>,
289 IfThenElse<dmlc::has_saveload<T>::value, SaveLoadClassHandler<T>,
290 UndefinedSerializerFor<T>, T>,
291 T>,
292 T>::Read(strm, data);
293 }
294 };
295
296 //! \cond Doxygen_Suppress
297 template <typename T>
298 struct Handler<std::vector<T>> {
299 inline static void Write(Stream *strm, const std::vector<T> &data) {
300 IfThenElse<dmlc::is_pod<T>::value && DMLC_IO_NO_ENDIAN_SWAP, NativePODVectorHandler<T>,
301 ComposeVectorHandler<T>, std::vector<T>>::Write(strm, data);
302 }
303 inline static bool Read(Stream *strm, std::vector<T> *data) {
304 return IfThenElse < dmlc::is_pod<T>::value && DMLC_IO_NO_ENDIAN_SWAP, NativePODVectorHandler<T>,
305 ComposeVectorHandler<T>, std::vector<T> > ::Read(strm, data);
306 }
307 };
308
309 template <typename T>
310 struct Handler<std::basic_string<T>> {
311 inline static void Write(Stream *strm, const std::basic_string<T> &data) {
312 IfThenElse<dmlc::is_pod<T>::value && (DMLC_IO_NO_ENDIAN_SWAP || sizeof(T) == 1),
313 NativePODStringHandler<T>, UndefinedSerializerFor<T>, std::basic_string<T>>::Write(strm,
314 data);
315 }
316 inline static bool Read(Stream *strm, std::basic_string<T> *data) {
317 return IfThenElse < dmlc::is_pod<T>::value && (DMLC_IO_NO_ENDIAN_SWAP || sizeof(T) == 1),
318 NativePODStringHandler<T>, UndefinedSerializerFor<T>,
319 std::basic_string<T> > ::Read(strm, data);
320 }
321 };
322
323 template <typename TA, typename TB>
324 struct Handler<std::pair<TA, TB>> {
325 inline static void Write(Stream *strm, const std::pair<TA, TB> &data) {
326 IfThenElse<dmlc::is_pod<TA>::value && dmlc::is_pod<TB>::value && DMLC_IO_NO_ENDIAN_SWAP,
327 NativePODHandler<std::pair<TA, TB>>, PairHandler<TA, TB>, std::pair<TA, TB>>::Write(strm,
328 data);
329 }
330 inline static bool Read(Stream *strm, std::pair<TA, TB> *data) {
331 return IfThenElse < dmlc::is_pod<TA>::value && dmlc::is_pod<TB>::value
332 && DMLC_IO_NO_ENDIAN_SWAP,
333 NativePODHandler<std::pair<TA, TB>>, PairHandler<TA, TB>,
334 std::pair<TA, TB> > ::Read(strm, data);
335 }
336 };
337
338 template <typename K, typename V>
339 struct Handler<std::map<K, V>> : public CollectionHandler<std::map<K, V>, std::pair<K, V>> {};
340
341 template <typename K, typename V>
342 struct Handler<std::multimap<K, V>> :
343 public CollectionHandler<std::multimap<K, V>, std::pair<K, V>> {};
344
345 template <typename T>
346 struct Handler<std::set<T>> : public CollectionHandler<std::set<T>, T> {};
347
348 template <typename T>
349 struct Handler<std::multiset<T>> : public CollectionHandler<std::multiset<T>, T> {};
350
351 template <typename T>
352 struct Handler<std::list<T>> : public ListHandler<std::list<T>> {};
353
354 template <typename T>
355 struct Handler<std::deque<T>> : public ListHandler<std::deque<T>> {};
356
357 #if DMLC_USE_CXX11
358 template <typename K, typename V>
359 struct Handler<std::unordered_map<K, V>> :
360 public CollectionHandler<std::unordered_map<K, V>, std::pair<K, V>> {};
361
362 template <typename K, typename V>
363 struct Handler<std::unordered_multimap<K, V>> :
364 public CollectionHandler<std::unordered_multimap<K, V>, std::pair<K, V>> {};
365
366 template <typename T>
367 struct Handler<std::unordered_set<T>> : public CollectionHandler<std::unordered_set<T>, T> {};
368
369 template <typename T>
370 struct Handler<std::unordered_multiset<T>> :
371 public CollectionHandler<std::unordered_multiset<T>, T> {};
372 #endif
373 //! \endcond
374 } // namespace serializer
375 } // namespace dmlc
376 #endif // DMLC_SERIALIZER_H_