c++20 serialization for binary(maybe json and xml will be support in future)
elastic is the serialization library for c++ with c++20. support multiple types to serialize, it depens on [reflect] to reflect member varibales. the library is header-only, so it is easy to use, only include header files.
these types are currently supported!
type | note |
---|---|
char | scalar type |
bool | scalar type |
intN | int8, int16, int32, int64 |
uintN | uint8_t, uint16_t. uint32_t. uint64_t |
double | double precision |
float | single precision |
enum/enum class | enum |
string | characters |
vector | sequence |
- inhert type need to add
ELASTIC_ACCESS_IF
to regist serialize limit. - no-pod type need to add
ELASTIC_ACCESS
to regist serialize limit.
the core of elastic is flex_buffer, flex_buffer is used for carry data by archive.flex_buffer define at below:
flex_buffer<_Element, _Traits>::flex_buffer
flex_buffer(); //defalut construction, construct a flex_buffer that capacity is 4096
flex_buffer(size_type number); // construct a flex_buffer that capacity is [number]
template <typename _Iter>
flex_buffer(_Iter begin, _Iter end); // construct a flex_buffer by iterator
template <typename _Ty, std::size_t N,
typename = std::is_convertible<_Ty, _Elem>>
flex_buffer(std::span<_Ty, N> data); // construct a flex_buffer by span
flex_buffer(const void* buffer, size_type sz); //construct a flex_buffer by C-Way
elastic provides two interfaces,that are to_binary
and from_binary
;
template <typename _Ty, typename _Elem, typename _Traits = std::char_traits<_Elem>>
bool to_binary(_Ty&& t, flex_buffer<_Elem, _Traits>& buffer); // convert t to binary buffers
template <typename _Ty, typename _Elem, typename _Traits = std::char_traits<_Elem>>
bool from_binary(_Ty& t, flex_buffer<_Elem, _Traits>& buffer); //convert binary iostream to t
elastic::flex_buffer_t buf;
int8_t a_in = (std::numeric_limits<int8_t>::max)();
elastic::to_binary(a_in, buf);
int8_t a_out{};
elastic::from_binary(a_out, buf);
a_in
will be equal to a_out
in the above code!
more examples has represented on unit test