:3
This commit is contained in:
parent
a743cdabe5
commit
bd402f57f5
276 changed files with 37936 additions and 22932 deletions
|
@ -6,41 +6,51 @@
|
|||
#include "Writer.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
namespace parsing {
|
||||
|
||||
/// msgpack-c requires us to explicitly set the number of fields in advance.
|
||||
/// Because of that, we require all of the fields and then set them to nullptr,
|
||||
/// if necessary.
|
||||
template <class ProcessorsType, class... FieldTypes>
|
||||
requires AreReaderAndWriter<msgpack::Reader, msgpack::Writer,
|
||||
NamedTuple<FieldTypes...>>
|
||||
struct Parser<msgpack::Reader, msgpack::Writer, NamedTuple<FieldTypes...>,
|
||||
ProcessorsType>
|
||||
: public NamedTupleParser<msgpack::Reader, msgpack::Writer,
|
||||
/*_ignore_empty_containers=*/false,
|
||||
/*_all_required=*/true, ProcessorsType,
|
||||
FieldTypes...> {
|
||||
};
|
||||
/// msgpack-c requires us to explicitly set the number of fields in advance.
|
||||
/// Because of that, we require all of the fields and then set them to
|
||||
/// nullptr, if necessary.
|
||||
template <class ProcessorsType, class... FieldTypes>
|
||||
requires AreReaderAndWriter<msgpack::Reader,
|
||||
msgpack::Writer,
|
||||
NamedTuple<FieldTypes...>>
|
||||
struct Parser<msgpack::Reader,
|
||||
msgpack::Writer,
|
||||
NamedTuple<FieldTypes...>,
|
||||
ProcessorsType>
|
||||
: public NamedTupleParser<msgpack::Reader,
|
||||
msgpack::Writer,
|
||||
/*_ignore_empty_containers=*/false,
|
||||
/*_all_required=*/true,
|
||||
ProcessorsType,
|
||||
FieldTypes...> {};
|
||||
|
||||
template <class ProcessorsType, class... Ts>
|
||||
requires AreReaderAndWriter<msgpack::Reader, msgpack::Writer, std::tuple<Ts...>>
|
||||
struct Parser<msgpack::Reader, msgpack::Writer, std::tuple<Ts...>,
|
||||
ProcessorsType>
|
||||
: public TupleParser<msgpack::Reader, msgpack::Writer,
|
||||
/*_ignore_empty_containers=*/false,
|
||||
/*_all_required=*/true, ProcessorsType, Ts...> {
|
||||
};
|
||||
template <class ProcessorsType, class... Ts>
|
||||
requires AreReaderAndWriter<msgpack::Reader,
|
||||
msgpack::Writer,
|
||||
std::tuple<Ts...>>
|
||||
struct Parser<msgpack::Reader,
|
||||
msgpack::Writer,
|
||||
std::tuple<Ts...>,
|
||||
ProcessorsType>
|
||||
: public TupleParser<msgpack::Reader,
|
||||
msgpack::Writer,
|
||||
/*_ignore_empty_containers=*/false,
|
||||
/*_all_required=*/true,
|
||||
ProcessorsType,
|
||||
Ts...> {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
namespace rfl {
|
||||
namespace msgpack {
|
||||
namespace msgpack {
|
||||
|
||||
template <class T, class ProcessorsType>
|
||||
using Parser = parsing::Parser<Reader, Writer, T, ProcessorsType>;
|
||||
template <class T, class ProcessorsType>
|
||||
using Parser = parsing::Parser<Reader, Writer, T, ProcessorsType>;
|
||||
|
||||
}
|
||||
} // namespace rfl
|
||||
}
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
#ifndef RFL_MSGPACK_READER_HPP_
|
||||
#define RFL_MSGPACK_READER_HPP_
|
||||
|
||||
#include <msgpack.h>
|
||||
|
||||
#include <array>
|
||||
#include <concepts>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <msgpack.h>
|
||||
#include <source_location>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
@ -22,127 +21,124 @@
|
|||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace msgpack {
|
||||
namespace msgpack {
|
||||
|
||||
struct Reader {
|
||||
using InputArrayType = msgpack_object_array;
|
||||
using InputObjectType = msgpack_object_map;
|
||||
using InputVarType = msgpack_object;
|
||||
struct Reader {
|
||||
using InputArrayType = msgpack_object_array;
|
||||
using InputObjectType = msgpack_object_map;
|
||||
using InputVarType = msgpack_object;
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
|
||||
T::from_msgpack_obj(var);
|
||||
});
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor =
|
||||
(requires(InputVarType var) { T::from_msgpack_obj(var); });
|
||||
|
||||
rfl::Result<InputVarType> get_field(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
for (uint32_t i = 0; i < _obj.size; ++i) {
|
||||
const auto& key = _obj.ptr[i].key;
|
||||
if (key.type != MSGPACK_OBJECT_STR) {
|
||||
return Error("Key in element " + std::to_string(i) +
|
||||
" was not a string.");
|
||||
rfl::Result<InputVarType> get_field(
|
||||
const std::string& _name,
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
for (uint32_t i = 0; i < _obj.size; ++i) {
|
||||
const auto& key = _obj.ptr[i].key;
|
||||
if (key.type != MSGPACK_OBJECT_STR) {
|
||||
return Error("Key in element " + std::to_string(i) +
|
||||
" was not a string.");
|
||||
}
|
||||
const auto current_name =
|
||||
std::string_view(key.via.str.ptr, key.via.str.size);
|
||||
if (_name == current_name) { return _obj.ptr[i].val; }
|
||||
}
|
||||
return Error("No field named '" + _name + "' was found.");
|
||||
}
|
||||
const auto current_name =
|
||||
std::string_view(key.via.str.ptr, key.via.str.size);
|
||||
if (_name == current_name) {
|
||||
return _obj.ptr[i].val;
|
||||
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return _var.type == MSGPACK_OBJECT_NIL;
|
||||
}
|
||||
}
|
||||
return Error("No field named '" + _name + "' was found.");
|
||||
}
|
||||
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return _var.type == MSGPACK_OBJECT_NIL;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
const auto type = _var.type;
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
if (type != MSGPACK_OBJECT_STR) {
|
||||
return Error("Could not cast to string.");
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
const auto type = _var.type;
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
if (type != MSGPACK_OBJECT_STR) {
|
||||
return Error("Could not cast to string.");
|
||||
}
|
||||
const auto str = _var.via.str;
|
||||
return std::string(str.ptr, str.size);
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (type != MSGPACK_OBJECT_BOOLEAN) {
|
||||
return Error("Could not cast to boolean.");
|
||||
}
|
||||
return _var.via.boolean;
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
|
||||
std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
if (type == MSGPACK_OBJECT_FLOAT32 ||
|
||||
type == MSGPACK_OBJECT_FLOAT64 || type == MSGPACK_OBJECT_FLOAT) {
|
||||
return static_cast<T>(_var.via.f64);
|
||||
} else if (type == MSGPACK_OBJECT_POSITIVE_INTEGER) {
|
||||
return static_cast<T>(_var.via.u64);
|
||||
} else if (type == MSGPACK_OBJECT_NEGATIVE_INTEGER) {
|
||||
return static_cast<T>(_var.via.i64);
|
||||
}
|
||||
return rfl::Error(
|
||||
"Could not cast to numeric value. The type must be integral, "
|
||||
"float "
|
||||
"or double.");
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
const auto str = _var.via.str;
|
||||
return std::string(str.ptr, str.size);
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (type != MSGPACK_OBJECT_BOOLEAN) {
|
||||
return Error("Could not cast to boolean.");
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (_var.type != MSGPACK_OBJECT_ARRAY) {
|
||||
return Error("Could not cast to an array.");
|
||||
}
|
||||
return _var.via.array;
|
||||
}
|
||||
return _var.via.boolean;
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
|
||||
std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
if (type == MSGPACK_OBJECT_FLOAT32 || type == MSGPACK_OBJECT_FLOAT64 ||
|
||||
type == MSGPACK_OBJECT_FLOAT) {
|
||||
return static_cast<T>(_var.via.f64);
|
||||
} else if (type == MSGPACK_OBJECT_POSITIVE_INTEGER) {
|
||||
return static_cast<T>(_var.via.u64);
|
||||
} else if (type == MSGPACK_OBJECT_NEGATIVE_INTEGER) {
|
||||
return static_cast<T>(_var.via.i64);
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (_var.type != MSGPACK_OBJECT_MAP) {
|
||||
return Error("Could not cast to a map.");
|
||||
}
|
||||
return _var.via.map;
|
||||
}
|
||||
return rfl::Error(
|
||||
"Could not cast to numeric value. The type must be integral, float "
|
||||
"or double.");
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (_var.type != MSGPACK_OBJECT_ARRAY) {
|
||||
return Error("Could not cast to an array.");
|
||||
}
|
||||
return _var.via.array;
|
||||
}
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (_var.type != MSGPACK_OBJECT_MAP) {
|
||||
return Error("Could not cast to a map.");
|
||||
}
|
||||
return _var.via.map;
|
||||
}
|
||||
|
||||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(const ArrayReader& _array_reader,
|
||||
const InputArrayType& _arr) const noexcept {
|
||||
for (uint32_t i = 0; i < _arr.size; ++i) {
|
||||
const auto err = _array_reader.read(_arr.ptr[i]);
|
||||
if (err) {
|
||||
return err;
|
||||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(
|
||||
const ArrayReader& _array_reader,
|
||||
const InputArrayType& _arr) const noexcept {
|
||||
for (uint32_t i = 0; i < _arr.size; ++i) {
|
||||
const auto err = _array_reader.read(_arr.ptr[i]);
|
||||
if (err) { return err; }
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
template <class ObjectReader>
|
||||
std::optional<Error> read_object(const ObjectReader& _object_reader,
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
for (uint32_t i = 0; i < _obj.size; ++i) {
|
||||
const auto& key = _obj.ptr[i].key;
|
||||
const auto& val = _obj.ptr[i].val;
|
||||
if (key.type != MSGPACK_OBJECT_STR) {
|
||||
return Error("Key in element " + std::to_string(i) +
|
||||
" was not a string.");
|
||||
template <class ObjectReader>
|
||||
std::optional<Error> read_object(
|
||||
const ObjectReader& _object_reader,
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
for (uint32_t i = 0; i < _obj.size; ++i) {
|
||||
const auto& key = _obj.ptr[i].key;
|
||||
const auto& val = _obj.ptr[i].val;
|
||||
if (key.type != MSGPACK_OBJECT_STR) {
|
||||
return Error("Key in element " + std::to_string(i) +
|
||||
" was not a string.");
|
||||
}
|
||||
const auto name = std::string_view(key.via.str.ptr, key.via.str.size);
|
||||
_object_reader.read(name, val);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto name = std::string_view(key.via.str.ptr, key.via.str.size);
|
||||
_object_reader.read(name, val);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> use_custom_constructor(
|
||||
const InputVarType& _var) const noexcept {
|
||||
try {
|
||||
return T::from_msgpack_obj(_var);
|
||||
} catch (std::exception& e) {
|
||||
return rfl::Error(e.what());
|
||||
}
|
||||
}
|
||||
};
|
||||
template <class T>
|
||||
rfl::Result<T> use_custom_constructor(
|
||||
const InputVarType& _var) const noexcept {
|
||||
try {
|
||||
return T::from_msgpack_obj(_var);
|
||||
} catch (std::exception& e) { return rfl::Error(e.what()); }
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace msgpack
|
||||
} // namespace rfl
|
||||
} // namespace msgpack
|
||||
} // namespace rfl
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
#endif // JSON_PARSER_HPP_
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
#ifndef RFL_MSGPACK_WRITER_HPP_
|
||||
#define RFL_MSGPACK_WRITER_HPP_
|
||||
|
||||
#include <msgpack.h>
|
||||
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <msgpack.h>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
@ -20,135 +19,140 @@
|
|||
|
||||
namespace rfl::msgpack {
|
||||
|
||||
class Writer {
|
||||
public:
|
||||
struct MsgpackOutputArray {};
|
||||
class Writer {
|
||||
public:
|
||||
struct MsgpackOutputArray {};
|
||||
|
||||
struct MsgpackOutputObject {};
|
||||
struct MsgpackOutputObject {};
|
||||
|
||||
struct MsgpackOutputVar {};
|
||||
struct MsgpackOutputVar {};
|
||||
|
||||
using OutputArrayType = MsgpackOutputArray;
|
||||
using OutputObjectType = MsgpackOutputObject;
|
||||
using OutputVarType = MsgpackOutputVar;
|
||||
using OutputArrayType = MsgpackOutputArray;
|
||||
using OutputObjectType = MsgpackOutputObject;
|
||||
using OutputVarType = MsgpackOutputVar;
|
||||
|
||||
Writer(msgpack_packer* _pk) : pk_(_pk) {}
|
||||
Writer(msgpack_packer* _pk) : pk_(_pk) {}
|
||||
|
||||
~Writer() = default;
|
||||
~Writer() = default;
|
||||
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
return new_array(_size);
|
||||
}
|
||||
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return new_object(_size);
|
||||
}
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
msgpack_pack_nil(pk_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
return new_value(_var);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_array(_size);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
return new_array(_size);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_object(_size);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
return new_object(_size);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_value(_var);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(const std::string_view& _name,
|
||||
const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
return new_value(_var);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
msgpack_pack_nil(pk_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_object(const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
msgpack_pack_nil(pk_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {}
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {}
|
||||
|
||||
private:
|
||||
OutputArrayType new_array(const size_t _size) const noexcept {
|
||||
msgpack_pack_array(pk_, _size);
|
||||
return OutputArrayType{};
|
||||
}
|
||||
|
||||
OutputObjectType new_object(const size_t _size) const noexcept {
|
||||
msgpack_pack_map(pk_, _size);
|
||||
return OutputObjectType{};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType new_value(const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
msgpack_pack_str(pk_, _var.size());
|
||||
msgpack_pack_str_body(pk_, _var.c_str(), _var.size());
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (_var) {
|
||||
msgpack_pack_true(pk_);
|
||||
} else {
|
||||
msgpack_pack_false(pk_);
|
||||
}
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
msgpack_pack_double(pk_, static_cast<double>(_var));
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
msgpack_pack_int64(pk_, static_cast<std::int64_t>(_var));
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
return new_array(_size);
|
||||
}
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
private:
|
||||
/// The underlying packer.
|
||||
msgpack_packer* pk_;
|
||||
};
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return new_object(_size);
|
||||
}
|
||||
|
||||
} // namespace rfl::msgpack
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
msgpack_pack_nil(pk_);
|
||||
return OutputVarType {};
|
||||
}
|
||||
|
||||
#endif // MSGPACK_PARSER_HPP_
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
return new_value(_var);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(
|
||||
const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_array(_size);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
return new_array(_size);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_object(_size);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
return new_object(_size);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_value(_var);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(
|
||||
const std::string_view& _name,
|
||||
const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
return new_value(_var);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
msgpack_pack_nil(pk_);
|
||||
return OutputVarType {};
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_object(const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
msgpack_pack_nil(pk_);
|
||||
return OutputVarType {};
|
||||
}
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {}
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {}
|
||||
|
||||
private:
|
||||
OutputArrayType new_array(const size_t _size) const noexcept {
|
||||
msgpack_pack_array(pk_, _size);
|
||||
return OutputArrayType {};
|
||||
}
|
||||
|
||||
OutputObjectType new_object(const size_t _size) const noexcept {
|
||||
msgpack_pack_map(pk_, _size);
|
||||
return OutputObjectType {};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType new_value(const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
msgpack_pack_str(pk_, _var.size());
|
||||
msgpack_pack_str_body(pk_, _var.c_str(), _var.size());
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (_var) {
|
||||
msgpack_pack_true(pk_);
|
||||
} else {
|
||||
msgpack_pack_false(pk_);
|
||||
}
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
msgpack_pack_double(pk_, static_cast<double>(_var));
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
msgpack_pack_int64(pk_, static_cast<std::int64_t>(_var));
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
return OutputVarType {};
|
||||
}
|
||||
|
||||
private:
|
||||
/// The underlying packer.
|
||||
msgpack_packer* pk_;
|
||||
};
|
||||
|
||||
} // namespace rfl::msgpack
|
||||
|
||||
#endif // MSGPACK_PARSER_HPP_
|
||||
|
|
|
@ -6,17 +6,17 @@
|
|||
#include "read.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace msgpack {
|
||||
namespace msgpack {
|
||||
|
||||
template <class T, class... Ps>
|
||||
Result<T> load(const std::string& _fname) {
|
||||
const auto read_bytes = [](const auto& _bytes) {
|
||||
return read<T, Ps...>(_bytes);
|
||||
};
|
||||
return rfl::io::load_bytes(_fname).and_then(read_bytes);
|
||||
}
|
||||
template <class T, class... Ps>
|
||||
Result<T> load(const std::string& _fname) {
|
||||
const auto read_bytes = [](const auto& _bytes) {
|
||||
return read<T, Ps...>(_bytes);
|
||||
};
|
||||
return rfl::io::load_bytes(_fname).and_then(read_bytes);
|
||||
}
|
||||
|
||||
} // namespace msgpack
|
||||
} // namespace rfl
|
||||
} // namespace msgpack
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
#ifndef RFL_MSGPACK_READ_HPP_
|
||||
#define RFL_MSGPACK_READ_HPP_
|
||||
|
||||
#include <msgpack.h>
|
||||
|
||||
#include <istream>
|
||||
#include <msgpack.h>
|
||||
#include <string>
|
||||
|
||||
#include "../Processors.hpp"
|
||||
|
@ -12,46 +11,46 @@
|
|||
#include "Reader.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace msgpack {
|
||||
namespace msgpack {
|
||||
|
||||
using InputObjectType = typename Reader::InputObjectType;
|
||||
using InputVarType = typename Reader::InputVarType;
|
||||
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 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 char* _bytes,
|
||||
const size_t _size) {
|
||||
msgpack_zone mempool;
|
||||
msgpack_zone_init(&mempool, 2048);
|
||||
msgpack_object deserialized;
|
||||
msgpack_unpack(_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>
|
||||
Result<internal::wrap_in_rfl_array_t<T>> read(const char* _bytes,
|
||||
const size_t _size) {
|
||||
msgpack_zone mempool;
|
||||
msgpack_zone_init(&mempool, 2048);
|
||||
msgpack_object deserialized;
|
||||
msgpack_unpack(_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 std::vector<char>& _bytes) {
|
||||
return read<T, Ps...>(_bytes.data(), _bytes.size());
|
||||
}
|
||||
/// Parses an object from MSGPACK 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());
|
||||
}
|
||||
/// 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
|
||||
} // namespace msgpack
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
|
|
@ -10,17 +10,17 @@
|
|||
#include "write.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace msgpack {
|
||||
namespace msgpack {
|
||||
|
||||
template <class... Ps>
|
||||
Result<Nothing> save(const std::string& _fname, const auto& _obj) {
|
||||
const auto write_func = [](const auto& _obj, auto& _stream) -> auto& {
|
||||
return write<Ps...>(_obj, _stream);
|
||||
};
|
||||
return rfl::io::save_bytes(_fname, _obj, write_func);
|
||||
}
|
||||
template <class... Ps>
|
||||
Result<Nothing> save(const std::string& _fname, const auto& _obj) {
|
||||
const auto write_func = [](const auto& _obj, auto& _stream) -> auto& {
|
||||
return write<Ps...>(_obj, _stream);
|
||||
};
|
||||
return rfl::io::save_bytes(_fname, _obj, write_func);
|
||||
}
|
||||
|
||||
} // namespace msgpack
|
||||
} // namespace rfl
|
||||
} // namespace msgpack
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
#ifndef RFL_MSGPACK_WRITE_HPP_
|
||||
#define RFL_MSGPACK_WRITE_HPP_
|
||||
|
||||
#include <msgpack.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <msgpack.h>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
@ -15,30 +14,30 @@
|
|||
|
||||
namespace rfl::msgpack {
|
||||
|
||||
/// Returns msgpack bytes.
|
||||
template <class... Ps>
|
||||
std::vector<char> write(const auto& _obj) noexcept {
|
||||
using T = std::remove_cvref_t<decltype(_obj)>;
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
msgpack_sbuffer sbuf;
|
||||
msgpack_sbuffer_init(&sbuf);
|
||||
msgpack_packer pk;
|
||||
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
|
||||
auto w = Writer(&pk);
|
||||
Parser<T, Processors<Ps...>>::write(w, _obj, typename ParentType::Root{});
|
||||
auto bytes = std::vector<char>(sbuf.data, sbuf.data + sbuf.size);
|
||||
msgpack_sbuffer_destroy(&sbuf);
|
||||
return bytes;
|
||||
}
|
||||
/// Returns msgpack bytes.
|
||||
template <class... Ps>
|
||||
std::vector<char> write(const auto& _obj) noexcept {
|
||||
using T = std::remove_cvref_t<decltype(_obj)>;
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
msgpack_sbuffer sbuf;
|
||||
msgpack_sbuffer_init(&sbuf);
|
||||
msgpack_packer pk;
|
||||
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
|
||||
auto w = Writer(&pk);
|
||||
Parser<T, Processors<Ps...>>::write(w, _obj, typename ParentType::Root {});
|
||||
auto bytes = std::vector<char>(sbuf.data, sbuf.data + sbuf.size);
|
||||
msgpack_sbuffer_destroy(&sbuf);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/// Writes a MSGPACK into an ostream.
|
||||
template <class... Ps>
|
||||
std::ostream& write(const auto& _obj, std::ostream& _stream) noexcept {
|
||||
auto buffer = write<Ps...>(_obj);
|
||||
_stream.write(buffer.data(), buffer.size());
|
||||
return _stream;
|
||||
}
|
||||
/// Writes a MSGPACK into an ostream.
|
||||
template <class... Ps>
|
||||
std::ostream& write(const auto& _obj, std::ostream& _stream) noexcept {
|
||||
auto buffer = write<Ps...>(_obj);
|
||||
_stream.write(buffer.data(), buffer.size());
|
||||
return _stream;
|
||||
}
|
||||
|
||||
} // namespace rfl::msgpack
|
||||
} // namespace rfl::msgpack
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue