mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-11-29 22:36:19 +08:00
Use exceptions inside the writers (#535)
Adds error handling using exceptions in the write processes.
This commit is contained in:
+66
-26
@@ -1,63 +1,97 @@
|
||||
#include "rfl/msgpack/Writer.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace rfl::msgpack {
|
||||
|
||||
Writer::Writer(msgpack_packer* _pk) : pk_(_pk) {}
|
||||
|
||||
Writer::~Writer() = default;
|
||||
|
||||
Writer::OutputArrayType Writer::array_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
Writer::OutputArrayType Writer::array_as_root(const size_t _size) const {
|
||||
return new_array(_size);
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::object_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
Writer::OutputObjectType Writer::object_as_root(const size_t _size) const {
|
||||
return new_object(_size);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::null_as_root() const noexcept {
|
||||
msgpack_pack_nil(pk_);
|
||||
Writer::OutputVarType Writer::null_as_root() const {
|
||||
const auto err = msgpack_pack_nil(pk_);
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack null as root.");
|
||||
}
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_array(
|
||||
const size_t _size, OutputArrayType* /*_parent*/) const noexcept {
|
||||
const size_t _size, OutputArrayType* /*_parent*/) const {
|
||||
return new_array(_size);
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::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());
|
||||
OutputObjectType* /*_parent*/) const {
|
||||
auto err = msgpack_pack_str(pk_, _name.size());
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack field name: '" +
|
||||
std::string(_name) + "'");
|
||||
}
|
||||
err = msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack field name body: '" +
|
||||
std::string(_name) + "'");
|
||||
}
|
||||
return new_array(_size);
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_array(
|
||||
const size_t _size, OutputArrayType* /*_parent*/) const noexcept {
|
||||
Writer::OutputObjectType Writer::add_object_to_array(const size_t _size,
|
||||
OutputArrayType*) const {
|
||||
return new_object(_size);
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::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());
|
||||
OutputObjectType*) const {
|
||||
auto err = msgpack_pack_str(pk_, _name.size());
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack field name: '" +
|
||||
std::string(_name) + "'");
|
||||
}
|
||||
err = msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack field name body: '" +
|
||||
std::string(_name) + "'");
|
||||
}
|
||||
return new_object(_size);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_array(
|
||||
OutputArrayType* /*_parent*/) const noexcept {
|
||||
msgpack_pack_nil(pk_);
|
||||
Writer::OutputVarType Writer::add_null_to_array(OutputArrayType*) const {
|
||||
const auto err = msgpack_pack_nil(pk_);
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not add nil to array.");
|
||||
}
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::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_);
|
||||
const std::string_view& _name, OutputObjectType* /*_parent*/) const {
|
||||
auto err = msgpack_pack_str(pk_, _name.size());
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack field name: '" +
|
||||
std::string(_name) + "'");
|
||||
}
|
||||
err = msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack field name body: '" +
|
||||
std::string(_name) + "'");
|
||||
}
|
||||
err = msgpack_pack_nil(pk_);
|
||||
if (err) {
|
||||
throw std::runtime_error(
|
||||
"Could not pack null value for the following field: '" +
|
||||
std::string(_name) + "'");
|
||||
}
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
@@ -65,13 +99,19 @@ void Writer::end_array(OutputArrayType* /*_arr*/) const noexcept {}
|
||||
|
||||
void Writer::end_object(OutputObjectType* /*_obj*/) const noexcept {}
|
||||
|
||||
Writer::OutputArrayType Writer::new_array(const size_t _size) const noexcept {
|
||||
msgpack_pack_array(pk_, _size);
|
||||
Writer::OutputArrayType Writer::new_array(const size_t _size) const {
|
||||
const auto err = msgpack_pack_array(pk_, _size);
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack array.");
|
||||
}
|
||||
return OutputArrayType{};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::new_object(const size_t _size) const noexcept {
|
||||
msgpack_pack_map(pk_, _size);
|
||||
Writer::OutputObjectType Writer::new_object(const size_t _size) const {
|
||||
const auto err = msgpack_pack_map(pk_, _size);
|
||||
if (err) {
|
||||
throw std::runtime_error("Could not pack array.");
|
||||
}
|
||||
return OutputObjectType{};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user