101 lines
2.6 KiB
C++
101 lines
2.6 KiB
C++
/*!
|
|
* Copyright (c) 2014 by Contributors
|
|
* \file serializable.h
|
|
* \brief defines serializable interface of rabit
|
|
* \author Tianqi Chen
|
|
*/
|
|
#ifndef RABIT_RABIT_SERIALIZABLE_H_
|
|
#define RABIT_RABIT_SERIALIZABLE_H_
|
|
#include <vector>
|
|
#include <string>
|
|
#include "./rabit/utils.h"
|
|
namespace rabit {
|
|
/*!
|
|
* \brief interface of stream I/O, used by ISerializable
|
|
* \sa ISerializable
|
|
*/
|
|
class IStream {
|
|
public:
|
|
/*!
|
|
* \brief read data from stream
|
|
* \param ptr pointer to memory buffer
|
|
* \param size size of block
|
|
* \return usually is the size of data readed
|
|
*/
|
|
virtual size_t Read(void *ptr, size_t size) = 0;
|
|
/*!
|
|
* \brief write data to stream
|
|
* \param ptr pointer to memory buffer
|
|
* \param size size of block
|
|
*/
|
|
virtual void Write(const void *ptr, size_t size) = 0;
|
|
/*! \brief virtual destructor */
|
|
virtual ~IStream(void) {}
|
|
|
|
public:
|
|
// helper functions to write various of data structures
|
|
/*!
|
|
* \brief binary serialize a vector
|
|
* \param vec vector to be serialized
|
|
*/
|
|
template<typename T>
|
|
inline void Write(const std::vector<T> &vec) {
|
|
uint64_t sz = static_cast<uint64_t>(vec.size());
|
|
this->Write(&sz, sizeof(sz));
|
|
if (sz != 0) {
|
|
this->Write(&vec[0], sizeof(T) * sz);
|
|
}
|
|
}
|
|
/*!
|
|
* \brief binary load a vector
|
|
* \param out_vec vector to be loaded
|
|
* \return whether load is successfull
|
|
*/
|
|
template<typename T>
|
|
inline bool Read(std::vector<T> *out_vec) {
|
|
uint64_t sz;
|
|
if (this->Read(&sz, sizeof(sz)) == 0) return false;
|
|
out_vec->resize(sz);
|
|
if (sz != 0) {
|
|
if (this->Read(&(*out_vec)[0], sizeof(T) * sz) == 0) return false;
|
|
}
|
|
return true;
|
|
}
|
|
/*!
|
|
* \brief binary serialize a string
|
|
* \param str the string to be serialized
|
|
*/
|
|
inline void Write(const std::string &str) {
|
|
uint64_t sz = static_cast<uint64_t>(str.length());
|
|
this->Write(&sz, sizeof(sz));
|
|
if (sz != 0) {
|
|
this->Write(&str[0], sizeof(char) * sz);
|
|
}
|
|
}
|
|
/*!
|
|
* \brief binary load a string
|
|
* \param out_str string to be loaded
|
|
* \return whether load is successful
|
|
*/
|
|
inline bool Read(std::string *out_str) {
|
|
uint64_t sz;
|
|
if (this->Read(&sz, sizeof(sz)) == 0) return false;
|
|
out_str->resize(sz);
|
|
if (sz != 0) {
|
|
if (this->Read(&(*out_str)[0], sizeof(char) * sz) == 0) return false;
|
|
}
|
|
return true;
|
|
}
|
|
};
|
|
|
|
/*! \brief interface of se*/
|
|
class ISerializable {
|
|
public:
|
|
/*! \brief load the model from file */
|
|
virtual void Load(IStream &fi) = 0;
|
|
/*! \brief save the model to the stream*/
|
|
virtual void Save(IStream &fo) const = 0;
|
|
};
|
|
} // namespace rabit
|
|
#endif // RABIT_RABIT_SERIALIZABLE_H_
|