mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-11-28 13:56:18 +08:00
60 lines
1.6 KiB
C++
60 lines
1.6 KiB
C++
#ifndef RFL_MSGPACK_READ_HPP_
|
|
#define RFL_MSGPACK_READ_HPP_
|
|
|
|
#include <msgpack.h>
|
|
|
|
#include <istream>
|
|
|
|
#include "../Processors.hpp"
|
|
#include "../concepts.hpp"
|
|
#include "../internal/ptr_cast.hpp"
|
|
#include "../internal/wrap_in_rfl_array_t.hpp"
|
|
#include "Parser.hpp"
|
|
#include "Reader.hpp"
|
|
|
|
namespace rfl {
|
|
namespace msgpack {
|
|
|
|
using InputObjectType = typename Reader::InputObjectType;
|
|
using InputVarType = typename Reader::InputVarType;
|
|
|
|
/// Parses an object from a MSGPACK 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 MSGPACK using reflection.
|
|
template <class T, class... Ps>
|
|
Result<internal::wrap_in_rfl_array_t<T>> read(
|
|
const concepts::ByteLike auto* _bytes, const size_t _size) {
|
|
msgpack_zone mempool;
|
|
msgpack_zone_init(&mempool, 2048);
|
|
msgpack_object deserialized;
|
|
msgpack_unpack(internal::ptr_cast<const char*>(_bytes), _size, NULL, &mempool,
|
|
&deserialized);
|
|
auto r = read<T, Ps...>(deserialized);
|
|
msgpack_zone_destroy(&mempool);
|
|
return r;
|
|
}
|
|
|
|
/// Parses an object from MSGPACK using reflection.
|
|
template <class T, class... Ps>
|
|
auto read(const concepts::ContiguousByteContainer auto& _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 msgpack
|
|
} // namespace rfl
|
|
|
|
#endif
|