7 #ifndef DMLC_SERIALIZER_H_ 8 #define DMLC_SERIALIZER_H_ 20 #include "./logging.h" 25 #include <unordered_map> 26 #include <unordered_set> 31 namespace serializer {
48 template<
bool cond,
typename Then,
typename Else,
typename Return>
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);
56 inline static bool Read(
Stream *strm, T *data) {
57 return Then::Read(strm, data);
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);
65 inline static bool Read(
Stream *strm, T *data) {
66 return Else::Read(strm, data);
72 struct NativePODHandler {
73 inline static void Write(
Stream *strm,
const T &data) {
74 strm->
Write(&data,
sizeof(T));
76 inline static bool Read(
Stream *strm, T *dptr) {
77 return strm->
Read((
void*)dptr,
sizeof(T)) ==
sizeof(T);
83 struct ArithmeticHandler {
84 inline static void Write(
Stream *strm,
const T &data) {
86 strm->
Write(&data,
sizeof(T));
90 strm->
Write(©,
sizeof(T));
93 inline static bool Read(
Stream *strm, T *dptr) {
94 bool ret = strm->
Read((
void*)dptr,
sizeof(T)) ==
sizeof(T);
104 struct SaveLoadClassHandler {
105 inline static void Write(
Stream *strm,
const T &data) {
108 inline static bool Read(
Stream *strm, T *data) {
109 return data->Load(strm);
120 struct UndefinedSerializerFor {
128 struct NativePODVectorHandler {
129 inline static void Write(
Stream *strm,
const std::vector<T> &vec) {
130 uint64_t sz =
static_cast<uint64_t
>(vec.size());
131 strm->
Write<uint64_t>(sz);
133 strm->
Write(&vec[0],
sizeof(T) * vec.size());
136 inline static bool Read(
Stream *strm, std::vector<T> *out_vec) {
138 if (!strm->
Read<uint64_t>(&sz))
return false;
139 size_t size =
static_cast<size_t>(sz);
140 out_vec->resize(size);
142 size_t nbytes =
sizeof(T) * size;
143 return strm->
Read(&(*out_vec)[0], nbytes) == nbytes;
154 struct ComposeVectorHandler {
155 inline static void Write(
Stream *strm,
const std::vector<T> &vec) {
156 uint64_t sz =
static_cast<uint64_t
>(vec.size());
157 strm->
Write<uint64_t>(sz);
160 inline static bool Read(
Stream *strm, std::vector<T> *out_vec) {
162 if (!strm->
Read<uint64_t>(&sz))
return false;
163 size_t size =
static_cast<size_t>(sz);
164 out_vec->resize(size);
174 struct NativePODStringHandler {
175 inline static void Write(
Stream *strm,
const std::basic_string<T> &vec) {
176 uint64_t sz =
static_cast<uint64_t
>(vec.length());
177 strm->
Write<uint64_t>(sz);
179 strm->
Write(&vec[0],
sizeof(T) * vec.length());
182 inline static bool Read(
Stream *strm, std::basic_string<T> *out_vec) {
184 if (!strm->
Read<uint64_t>(&sz))
return false;
185 size_t size =
static_cast<size_t>(sz);
186 out_vec->resize(size);
188 size_t nbytes =
sizeof(T) * size;
189 return strm->
Read(&(*out_vec)[0], nbytes) == nbytes;
196 template<
typename TA,
typename TB>
198 inline static void Write(
Stream *strm,
const std::pair<TA, TB> &data) {
202 inline static bool Read(
Stream *strm, std::pair<TA, TB> *data) {
209 template<
typename ContainerType,
typename ElemType>
210 struct CollectionHandler {
211 inline static void Write(
Stream *strm,
const ContainerType &data) {
213 std::vector<ElemType> vdata(data.begin(), data.end());
217 inline static bool Read(
Stream *strm, ContainerType *data) {
218 std::vector<ElemType> vdata;
219 if (!
Handler<std::vector<ElemType> >::Read(strm, &vdata))
return false;
221 data->insert(vdata.begin(), vdata.end());
229 template<
typename ListType>
231 inline static void Write(
Stream *strm,
const ListType &data) {
232 typedef typename ListType::value_type ElemType;
234 std::vector<ElemType> vdata(data.begin(), data.end());
238 inline static bool Read(
Stream *strm, ListType *data) {
239 typedef typename ListType::value_type ElemType;
240 std::vector<ElemType> vdata;
241 if (!
Handler<std::vector<ElemType> >::Read(strm, &vdata))
return false;
243 data->insert(data->begin(), vdata.begin(), vdata.end());
266 IfThenElse<dmlc::is_arithmetic<T>::value,
267 ArithmeticHandler<T>,
270 IfThenElse<dmlc::has_saveload<T>::value,
271 SaveLoadClassHandler<T>,
272 UndefinedSerializerFor<T>, T>,
285 IfThenElse<dmlc::is_arithmetic<T>::value,
286 ArithmeticHandler<T>,
289 IfThenElse<dmlc::has_saveload<T>::value,
290 SaveLoadClassHandler<T>,
291 UndefinedSerializerFor<T>, T>,
300 struct Handler<std::vector<T> > {
301 inline static void Write(
Stream *strm,
const std::vector<T> &data) {
303 NativePODVectorHandler<T>,
304 ComposeVectorHandler<T>, std::vector<T> >
307 inline static bool Read(
Stream *strm, std::vector<T> *data) {
309 NativePODVectorHandler<T>,
310 ComposeVectorHandler<T>,
317 struct Handler<std::basic_string<T> > {
318 inline static void Write(
Stream *strm,
const std::basic_string<T> &data) {
320 NativePODStringHandler<T>,
321 UndefinedSerializerFor<T>,
322 std::basic_string<T> >
325 inline static bool Read(
Stream *strm, std::basic_string<T> *data) {
327 NativePODStringHandler<T>,
328 UndefinedSerializerFor<T>,
329 std::basic_string<T> >
334 template<
typename TA,
typename TB>
335 struct Handler<std::pair<TA, TB> > {
336 inline static void Write(
Stream *strm,
const std::pair<TA, TB> &data) {
337 IfThenElse<dmlc::is_pod<TA>::value &&
340 NativePODHandler<std::pair<TA, TB> >,
345 inline static bool Read(
Stream *strm, std::pair<TA, TB> *data) {
346 return IfThenElse<dmlc::is_pod<TA>::value &&
349 NativePODHandler<std::pair<TA, TB> >,
356 template<
typename K,
typename V>
357 struct Handler<std::map<K, V> >
358 :
public CollectionHandler<std::map<K, V>, std::pair<K, V> > {
361 template<
typename K,
typename V>
362 struct Handler<std::multimap<K, V> >
363 :
public CollectionHandler<std::multimap<K, V>, std::pair<K, V> > {
368 :
public CollectionHandler<std::set<T>, T> {
372 struct Handler<std::multiset<T> >
373 :
public CollectionHandler<std::multiset<T>, T> {
378 :
public ListHandler<std::list<T> > {
383 :
public ListHandler<std::deque<T> > {
387 template<
typename K,
typename V>
388 struct Handler<std::unordered_map<K, V> >
389 :
public CollectionHandler<std::unordered_map<K, V>, std::pair<K, V> > {
392 template<
typename K,
typename V>
393 struct Handler<std::unordered_multimap<K, V> >
394 :
public CollectionHandler<std::unordered_multimap<K, V>, std::pair<K, V> > {
398 struct Handler<std::unordered_set<T> >
399 :
public CollectionHandler<std::unordered_set<T>, T> {
403 struct Handler<std::unordered_multiset<T> >
404 :
public CollectionHandler<std::unordered_multiset<T>, T> {
410 #endif // DMLC_SERIALIZER_H_ whether a type is pod type
Definition: type_traits.h:21
T * BeginPtr(std::vector< T > &vec)
safely get the beginning address of a vector
Definition: base.h:239
interface of stream I/O for serialization
Definition: io.h:30
static void Write(Stream *strm, const T &data)
write data to stream
Definition: serializer.h:265
void WriteArray(const T *data, size_t num_elems)
Endian aware write array of data.
Definition: io.h:460
Endian testing, need c++11.
generic serialization handler
Definition: serializer.h:38
namespace for dmlc
Definition: array_view.h:12
#define DMLC_IO_NO_ENDIAN_SWAP
whether serialize using little endian
Definition: endian.h:39
void ByteSwap(void *data, size_t elem_bytes, size_t num_elems)
A generic inplace byte swapping function.
Definition: endian.h:51
bool ReadArray(T *data, size_t num_elems)
Endian aware read array of data.
Definition: io.h:467
virtual size_t Read(void *ptr, size_t size)=0
reads data from a stream
virtual void Write(const void *ptr, size_t size)=0
writes data to a stream
static bool Read(Stream *strm, T *data)
read data to stream
Definition: serializer.h:283
type traits information header