1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#ifndef FLEXBUF_READ_HPP_
#define FLEXBUF_READ_HPP_
#include <flatbuffers/flexbuffers.h>
#include <istream>
#include <vector>
#include "../Processors.hpp"
#include "../Result.hpp"
#include "../internal/ptr_cast.hpp"
#include "Parser.hpp"
namespace rfl {
namespace flexbuf {
using InputVarType = typename Reader::InputVarType;
/// Parses an object from flexbuf var.
template <class T, class... Ps>
auto read(const InputVarType& _obj) {
const auto r = Reader();
return Parser<T, Processors<Ps...>>::read(r, _obj);
}
/// Parses an object from flexbuf using reflection.
template <class T, class... Ps>
auto read(const char* _bytes, const size_t _size) {
const InputVarType root =
flexbuffers::GetRoot(internal::ptr_cast<const uint8_t*>(_bytes), _size);
return read<T, Ps...>(root);
}
/// Parses an object from flexbuf using reflection.
template <class T, class... Ps>
auto read(const std::vector<char>& _bytes) {
return read<T, Ps...>(_bytes.data(), _bytes.size());
}
/// Parses an object directly from a stream.
template <class T, class... Ps>
auto read(std::istream& _stream) {
std::istreambuf_iterator<char> begin(_stream), end;
const auto bytes = std::vector<char>(begin, end);
return read<T, Ps...>(bytes.data(), bytes.size());
}
} // namespace flexbuf
} // namespace rfl
#endif
|