mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 02:36:18 +08:00
Use exceptions inside the writers (#535)
Adds error handling using exceptions in the write processes.
This commit is contained in:
@@ -9,10 +9,9 @@
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include "../Bytestring.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../Vectorstring.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../concepts.hpp"
|
||||
#include "../internal/ptr_cast.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -66,18 +65,12 @@ struct Reader {
|
||||
const auto str = _var.via.str;
|
||||
return std::string(str.ptr, str.size);
|
||||
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Bytestring>() ||
|
||||
std::is_same<std::remove_cvref_t<T>,
|
||||
rfl::Vectorstring>()) {
|
||||
} else if constexpr (concepts::MutableContiguousByteContainer<
|
||||
std::remove_cvref_t<T>>) {
|
||||
using VectorType = std::remove_cvref_t<T>;
|
||||
using ValueType = typename VectorType::value_type;
|
||||
if (type != MSGPACK_OBJECT_BIN) {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, rfl::Bytestring>()) {
|
||||
return error("Could not cast to bytestring.");
|
||||
} else {
|
||||
return error("Could not cast to vectorstring.");
|
||||
}
|
||||
return error("Could not cast to bytestring.");
|
||||
}
|
||||
const auto bin = _var.via.bin;
|
||||
const auto data = internal::ptr_cast<const ValueType*>(bin.ptr);
|
||||
|
||||
@@ -3,16 +3,14 @@
|
||||
|
||||
#include <msgpack.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Bytestring.hpp"
|
||||
#include "../Vectorstring.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../common.hpp"
|
||||
#include "../concepts.hpp"
|
||||
|
||||
namespace rfl::msgpack {
|
||||
|
||||
@@ -32,80 +30,112 @@ class RFL_API Writer {
|
||||
|
||||
~Writer();
|
||||
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept;
|
||||
OutputArrayType array_as_root(const size_t _size) const;
|
||||
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept;
|
||||
OutputObjectType object_as_root(const size_t _size) const;
|
||||
|
||||
OutputVarType null_as_root() const noexcept;
|
||||
OutputVarType null_as_root() const;
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
OutputVarType value_as_root(const T& _var) const {
|
||||
return new_value(_var);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
OutputArrayType* _parent) const;
|
||||
|
||||
OutputArrayType add_array_to_object(const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
OutputObjectType* _parent) const;
|
||||
|
||||
OutputObjectType add_object_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
OutputArrayType* _parent) const;
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
OutputObjectType add_object_to_object(const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const;
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(
|
||||
const T& _var, OutputArrayType* /*_parent*/) const noexcept {
|
||||
OutputVarType add_value_to_array(const T& _var, OutputArrayType*) const {
|
||||
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());
|
||||
OutputVarType add_value_to_object(const std::string_view& _name,
|
||||
const T& _var, OutputObjectType*) const {
|
||||
auto err = msgpack_pack_str(pk_, _name.size());
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack string.");
|
||||
}
|
||||
err = msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack string body.");
|
||||
}
|
||||
return new_value(_var);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const;
|
||||
|
||||
OutputVarType add_null_to_object(const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
OutputObjectType* _parent) const;
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept;
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept;
|
||||
|
||||
private:
|
||||
OutputArrayType new_array(const size_t _size) const noexcept;
|
||||
OutputArrayType new_array(const size_t _size) const;
|
||||
|
||||
OutputObjectType new_object(const size_t _size) const noexcept;
|
||||
OutputObjectType new_object(const size_t _size) const;
|
||||
|
||||
template <class T>
|
||||
OutputVarType new_value(const T& _var) const noexcept {
|
||||
OutputVarType new_value(const T& _var) const {
|
||||
using Type = std::remove_cvref_t<T>;
|
||||
if constexpr (std::is_same<Type, std::string>()) {
|
||||
msgpack_pack_str(pk_, _var.size());
|
||||
msgpack_pack_str_body(pk_, _var.c_str(), _var.size());
|
||||
} else if constexpr (std::is_same<Type, rfl::Bytestring>() ||
|
||||
std::is_same<Type, rfl::Vectorstring>()) {
|
||||
msgpack_pack_bin(pk_, _var.size());
|
||||
msgpack_pack_bin_body(pk_, _var.data(), _var.size());
|
||||
auto err = msgpack_pack_str(pk_, _var.size());
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack string.");
|
||||
}
|
||||
err = msgpack_pack_str_body(pk_, _var.c_str(), _var.size());
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack string body.");
|
||||
}
|
||||
|
||||
} else if constexpr (concepts::MutableContiguousByteContainer<Type>) {
|
||||
auto err = msgpack_pack_bin(pk_, _var.size());
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack binary string.");
|
||||
}
|
||||
err = msgpack_pack_bin_body(pk_, _var.data(), _var.size());
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack binary string.");
|
||||
}
|
||||
|
||||
} else if constexpr (std::is_same<Type, bool>()) {
|
||||
if (_var) {
|
||||
msgpack_pack_true(pk_);
|
||||
const auto err = msgpack_pack_true(pk_);
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack boolean.");
|
||||
}
|
||||
} else {
|
||||
msgpack_pack_false(pk_);
|
||||
const auto err = msgpack_pack_false(pk_);
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack boolean.");
|
||||
}
|
||||
}
|
||||
|
||||
} else if constexpr (std::is_floating_point<Type>()) {
|
||||
msgpack_pack_double(pk_, static_cast<double>(_var));
|
||||
const auto err = msgpack_pack_double(pk_, static_cast<double>(_var));
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack double.");
|
||||
}
|
||||
|
||||
} else if constexpr (std::is_integral<Type>()) {
|
||||
msgpack_pack_int64(pk_, static_cast<std::int64_t>(_var));
|
||||
const auto err = msgpack_pack_int64(pk_, static_cast<std::int64_t>(_var));
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack int.");
|
||||
}
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <ostream>
|
||||
|
||||
#include "../Processors.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
@@ -13,7 +14,7 @@ namespace rfl::msgpack {
|
||||
|
||||
/// Returns msgpack bytes.
|
||||
template <class... Ps>
|
||||
std::vector<char> write(const auto& _obj) noexcept {
|
||||
std::vector<char> write(const auto& _obj) {
|
||||
using T = std::remove_cvref_t<decltype(_obj)>;
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
msgpack_sbuffer sbuf;
|
||||
@@ -21,15 +22,21 @@ std::vector<char> write(const auto& _obj) noexcept {
|
||||
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);
|
||||
const auto bytes = [&]() -> Result<std::vector<char>> {
|
||||
try {
|
||||
Parser<T, Processors<Ps...>>::write(w, _obj, typename ParentType::Root{});
|
||||
return std::vector<char>(sbuf.data, sbuf.data + sbuf.size);
|
||||
} catch (const std::exception& e) {
|
||||
return error(e.what());
|
||||
}
|
||||
}();
|
||||
msgpack_sbuffer_destroy(&sbuf);
|
||||
return bytes;
|
||||
return bytes.value();
|
||||
}
|
||||
|
||||
/// Writes a MSGPACK into an ostream.
|
||||
template <class... Ps>
|
||||
std::ostream& write(const auto& _obj, std::ostream& _stream) noexcept {
|
||||
std::ostream& write(const auto& _obj, std::ostream& _stream) {
|
||||
auto buffer = write<Ps...>(_obj);
|
||||
_stream.write(buffer.data(), buffer.size());
|
||||
return _stream;
|
||||
|
||||
Reference in New Issue
Block a user