58 lines
1.5 KiB
C++
58 lines
1.5 KiB
C++
|
#ifndef RFL_CBOR_READ_HPP_
|
||
|
#define RFL_CBOR_READ_HPP_
|
||
|
|
||
|
#include <cbor.h>
|
||
|
|
||
|
#include <istream>
|
||
|
#include <string>
|
||
|
|
||
|
#include "../Processors.hpp"
|
||
|
#include "../internal/wrap_in_rfl_array_t.hpp"
|
||
|
#include "Parser.hpp"
|
||
|
#include "Reader.hpp"
|
||
|
|
||
|
namespace rfl {
|
||
|
namespace cbor {
|
||
|
|
||
|
using InputObjectType = typename Reader::InputObjectType;
|
||
|
using InputVarType = typename Reader::InputVarType;
|
||
|
|
||
|
/// Parses an object from a CBOR 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 CBOR using reflection.
|
||
|
template <class T, class... Ps>
|
||
|
Result<internal::wrap_in_rfl_array_t<T>> read(const char* _bytes,
|
||
|
const size_t _size) {
|
||
|
CborParser parser;
|
||
|
CborValue value;
|
||
|
cbor_parser_init(reinterpret_cast<const uint8_t*>(_bytes), _size, 0, &parser,
|
||
|
&value);
|
||
|
auto doc = InputVarType{&value};
|
||
|
auto result = read<T, Ps...>(doc);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
/// Parses an object from CBOR 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 from a stream.
|
||
|
template <class T, class... Ps>
|
||
|
auto read(std::istream& _stream) {
|
||
|
std::istreambuf_iterator<char> begin(_stream), end;
|
||
|
auto bytes = std::vector<char>(begin, end);
|
||
|
return read<T, Ps...>(bytes.data(), bytes.size());
|
||
|
}
|
||
|
|
||
|
} // namespace cbor
|
||
|
} // namespace rfl
|
||
|
|
||
|
#endif
|