Add support for msgpack; resolves #26 (#87)

This commit is contained in:
liuzicheng1987
2024-04-21 12:03:36 +02:00
committed by GitHub
parent 2f499714f8
commit 8a858d2cca
85 changed files with 2157 additions and 11 deletions
+43
View File
@@ -0,0 +1,43 @@
#ifndef RFL_MSGPACK_PARSER_HPP_
#define RFL_MSGPACK_PARSER_HPP_
#include "../parsing/Parser.hpp"
#include "Reader.hpp"
#include "Writer.hpp"
namespace rfl {
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... FieldTypes>
requires AreReaderAndWriter<msgpack::Reader, msgpack::Writer,
NamedTuple<FieldTypes...>>
struct Parser<msgpack::Reader, msgpack::Writer, NamedTuple<FieldTypes...>>
: public NamedTupleParser<msgpack::Reader, msgpack::Writer,
/*_ignore_empty_containers=*/false,
/*_all_required=*/true, FieldTypes...> {
};
template <class... Ts>
requires AreReaderAndWriter<msgpack::Reader, msgpack::Writer, std::tuple<Ts...>>
struct Parser<msgpack::Reader, msgpack::Writer, std::tuple<Ts...>>
: public TupleParser<msgpack::Reader, msgpack::Writer,
/*_ignore_empty_containers=*/false,
/*_all_required=*/true, Ts...> {
};
} // namespace parsing
} // namespace rfl
namespace rfl {
namespace msgpack {
template <class T>
using Parser = parsing::Parser<Reader, Writer, T>;
}
} // namespace rfl
#endif
+144
View File
@@ -0,0 +1,144 @@
#ifndef RFL_MSGPACK_READER_HPP_
#define RFL_MSGPACK_READER_HPP_
#include <msgpack.h>
#include <array>
#include <concepts>
#include <exception>
#include <map>
#include <memory>
#include <source_location>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <unordered_map>
#include <vector>
#include "../Box.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
namespace rfl {
namespace msgpack {
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);
});
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.");
}
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.");
}
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.");
}
}
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;
}
std::vector<InputVarType> to_vec(const InputArrayType& _arr) const noexcept {
std::vector<InputVarType> vec;
for (uint32_t i = 0; i < _arr.size; ++i) {
vec.push_back(_arr.ptr[i]);
}
return vec;
}
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;
}
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
#endif // JSON_PARSER_HPP_
+154
View File
@@ -0,0 +1,154 @@
#ifndef RFL_MSGPACK_WRITER_HPP_
#define RFL_MSGPACK_WRITER_HPP_
#include <msgpack.h>
#include <exception>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <variant>
#include <vector>
#include "../Box.hpp"
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
namespace rfl::msgpack {
class Writer {
public:
struct MsgpackOutputArray {};
struct MsgpackOutputObject {};
struct MsgpackOutputVar {};
using OutputArrayType = MsgpackOutputArray;
using OutputObjectType = MsgpackOutputObject;
using OutputVarType = MsgpackOutputVar;
Writer(msgpack_packer* _pk) : pk_(_pk) {}
~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.");
}
return OutputVarType{};
}
private:
/// The underlying packer.
msgpack_packer* pk_;
};
} // namespace rfl::msgpack
#endif // MSGPACK_PARSER_HPP_
+20
View File
@@ -0,0 +1,20 @@
#ifndef RFL_MSGPACK_LOAD_HPP_
#define RFL_MSGPACK_LOAD_HPP_
#include "../Result.hpp"
#include "../io/load_bytes.hpp"
#include "read.hpp"
namespace rfl {
namespace msgpack {
template <class T>
Result<T> load(const std::string& _fname) {
const auto read_bytes = [](const auto& _bytes) { return read<T>(_bytes); };
return rfl::io::load_bytes(_fname).and_then(read_bytes);
}
} // namespace msgpack
} // namespace rfl
#endif
+56
View File
@@ -0,0 +1,56 @@
#ifndef RFL_MSGPACK_READ_HPP_
#define RFL_MSGPACK_READ_HPP_
#include <msgpack.h>
#include <istream>
#include <string>
#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>
auto read(const InputVarType& _obj) {
const auto r = Reader();
return Parser<T>::read(r, _obj);
}
/// Parses an object from MSGPACK using reflection.
template <class T>
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>(deserialized);
msgpack_zone_destroy(&mempool);
return r;
}
/// Parses an object from MSGPACK using reflection.
template <class T>
auto read(const std::vector<char>& _bytes) {
return read<T>(_bytes.data(), _bytes.size());
}
/// Parses an object from a stream.
template <class T>
auto read(std::istream& _stream) {
std::istreambuf_iterator<char> begin(_stream), end;
auto bytes = std::vector<char>(begin, end);
return read<T>(bytes.data(), bytes.size());
}
} // namespace msgpack
} // namespace rfl
#endif
+26
View File
@@ -0,0 +1,26 @@
#ifndef RFL_MSGPACK_SAVE_HPP_
#define RFL_MSGPACK_SAVE_HPP_
#include <fstream>
#include <iostream>
#include <string>
#include "../Result.hpp"
#include "../io/save_bytes.hpp"
#include "write.hpp"
namespace rfl {
namespace msgpack {
template <class T>
Result<Nothing> save(const std::string& _fname, const T& _obj) {
const auto write_func = [](const auto& _obj, auto& _stream) -> auto& {
return write(_obj, _stream);
};
return rfl::io::save_bytes(_fname, _obj, write_func);
}
} // namespace msgpack
} // namespace rfl
#endif
+42
View File
@@ -0,0 +1,42 @@
#ifndef RFL_MSGPACK_WRITE_HPP_
#define RFL_MSGPACK_WRITE_HPP_
#include <msgpack.h>
#include <cstdint>
#include <ostream>
#include <sstream>
#include <string>
#include <utility>
#include "../parsing/Parent.hpp"
#include "Parser.hpp"
namespace rfl::msgpack {
/// Returns msgpack bytes.
template <class T>
std::vector<char> write(const T& _obj) noexcept {
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>::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 T>
std::ostream& write(const T& _obj, std::ostream& _stream) noexcept {
auto buffer = write(_obj);
_stream.write(buffer.data(), buffer.size());
return _stream;
}
} // namespace rfl::msgpack
#endif