Use exceptions inside the writers (#535)

Adds error handling using exceptions in the write processes.
This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2025-10-27 23:38:20 +01:00
committed by GitHub
parent 3ae683387e
commit 2b1cd83677
31 changed files with 867 additions and 673 deletions
+29 -17
View File
@@ -4,11 +4,11 @@
#include <avro.h>
#include <ostream>
#include <stdexcept>
#include <type_traits>
#include "../parsing/Parent.hpp"
#include "Parser.hpp"
//#include "Schema.hpp"
#include "Writer.hpp"
#include "to_schema.hpp"
@@ -26,25 +26,37 @@ std::vector<char> write(const auto& _obj, const auto& _schema) {
int result = avro_generic_value_new(_schema.iface(), &root);
if (result != 0) {
avro_value_decref(&root);
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
const auto writer = Writer(&root);
Parser<T, Processors<Ps...>>::write(writer, _obj,
typename ParentType::Root{});
size_t size = 0;
result = avro_value_sizeof(&root, &size);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
}
std::vector<char> buffer(size);
avro_writer_t avro_writer = avro_writer_memory(buffer.data(), buffer.size());
result = avro_value_write(avro_writer, &root);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
}
const auto buffer = [&]() -> Result<std::vector<char>> {
try {
Parser<T, Processors<Ps...>>::write(writer, _obj,
typename ParentType::Root{});
} catch (std::exception& e) {
return error(e.what());
}
size_t size = 0;
result = avro_value_sizeof(&root, &size);
if (result != 0) {
return error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
std::vector<char> buffer(size);
avro_writer_t avro_writer =
avro_writer_memory(buffer.data(), buffer.size());
result = avro_value_write(avro_writer, &root);
if (result != 0) {
avro_writer_free(avro_writer);
return error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
avro_writer_free(avro_writer);
return buffer;
}();
avro_value_decref(&root);
avro_writer_free(avro_writer);
return buffer;
return buffer.value();
}
/// Returns AVRO bytes.
+7 -20
View File
@@ -9,10 +9,9 @@
#include <string>
#include <type_traits>
#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../concepts.hpp"
#include "../internal/ptr_cast.hpp"
namespace rfl {
@@ -102,29 +101,17 @@ struct Reader {
"Could not cast to string. The type must be UTF8 or symbol.");
}
} 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 (btype != BSON_TYPE_BINARY) {
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.");
}
if (value.v_binary.subtype != BSON_SUBTYPE_BINARY) {
if constexpr (std::is_same<std::remove_cvref_t<T>, rfl::Bytestring>()) {
return error(
"The BSON subtype must be a binary in order to read into a "
"bytestring.");
} else {
return error(
"The BSON subtype must be a binary in order to read into a "
"vectorstring.");
}
return error(
"The BSON subtype must be a binary in order to read into a "
"bytestring.");
}
const auto data =
internal::ptr_cast<const ValueType*>(value.v_binary.data);
+99 -50
View File
@@ -4,6 +4,7 @@
#include <bson/bson.h>
#include <cstddef>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
@@ -11,13 +12,11 @@
#include <vector>
#include "../Box.hpp"
#include "../Bytestring.hpp"
#include "../Ref.hpp"
//#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../internal/ptr_cast.hpp"
#include "../common.hpp"
#include "../concepts.hpp"
#include "../internal/ptr_cast.hpp"
namespace rfl {
namespace bson {
@@ -79,43 +78,65 @@ class RFL_API Writer {
}
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 {
OutputArrayType* _parent) const {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
bson_array_builder_append_utf8(_parent->val_, _var.c_str(),
static_cast<int>(_var.size()));
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
bson_array_builder_append_binary(
const bool ok = bson_array_builder_append_utf8(
_parent->val_, _var.c_str(), static_cast<int>(_var.size()));
if (!ok) {
throw std::runtime_error("Could not append utf-8 to array.");
}
} else if constexpr (concepts::MutableContiguousByteContainer<
std::remove_cvref_t<T>>) {
const bool ok = bson_array_builder_append_binary(
_parent->val_, BSON_SUBTYPE_BINARY,
internal::ptr_cast<const uint8_t*>(_var.data()),
static_cast<uint32_t>(_var.size()));
if (!ok) {
throw std::runtime_error("Could not append binary to array.");
}
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
bson_array_builder_append_bool(_parent->val_, _var);
const bool ok = bson_array_builder_append_bool(_parent->val_, _var);
if (!ok) {
throw std::runtime_error("Could not append bool to array.");
}
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
bson_array_builder_append_double(_parent->val_,
static_cast<double>(_var));
const bool ok = bson_array_builder_append_double(
_parent->val_, static_cast<double>(_var));
if (!ok) {
throw std::runtime_error("Could not append float to array.");
}
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
bson_array_builder_append_int64(_parent->val_,
static_cast<std::int64_t>(_var));
const bool ok = bson_array_builder_append_int64(
_parent->val_, static_cast<std::int64_t>(_var));
if (!ok) {
throw std::runtime_error("Could not append integer to array.");
}
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bson_oid_t>()) {
bson_array_builder_append_oid(_parent->val_, &_var);
const bool ok = bson_array_builder_append_oid(_parent->val_, &_var);
if (!ok) {
throw std::runtime_error("Could not append OID to array.");
}
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
@@ -125,47 +146,75 @@ class RFL_API Writer {
template <class T>
OutputVarType add_value_to_object(const std::string_view& _name,
const T& _var,
OutputObjectType* _parent) const noexcept {
OutputObjectType* _parent) const {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
bson_append_utf8(_parent->val_, _name.data(),
static_cast<int>(_name.size()), _var.c_str(),
static_cast<int>(_var.size()));
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
bson_append_binary(_parent->val_, _name.data(),
static_cast<int>(_name.size()), BSON_SUBTYPE_BINARY,
internal::ptr_cast<const uint8_t*>(_var.data()),
static_cast<uint32_t>(_var.size()));
const bool ok = bson_append_utf8(
_parent->val_, _name.data(), static_cast<int>(_name.size()),
_var.c_str(), static_cast<int>(_var.size()));
if (!ok) {
throw std::runtime_error("Could not utf-8 field '" +
std::string(_name) + "' to object.");
}
} else if constexpr (concepts::MutableContiguousByteContainer<
std::remove_cvref_t<T>>) {
const bool ok = bson_append_binary(
_parent->val_, _name.data(), static_cast<int>(_name.size()),
BSON_SUBTYPE_BINARY, internal::ptr_cast<const uint8_t*>(_var.data()),
static_cast<uint32_t>(_var.size()));
if (!ok) {
throw std::runtime_error("Could not binary field '" +
std::string(_name) + "' to object.");
}
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
bson_append_bool(_parent->val_, _name.data(),
static_cast<int>(_name.size()), _var);
const bool ok = bson_append_bool(_parent->val_, _name.data(),
static_cast<int>(_name.size()), _var);
if (!ok) {
throw std::runtime_error("Could not boolean field '" +
std::string(_name) + "' to object.");
}
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
bson_append_double(_parent->val_, _name.data(),
static_cast<int>(_name.size()),
static_cast<double>(_var));
const bool ok = bson_append_double(_parent->val_, _name.data(),
static_cast<int>(_name.size()),
static_cast<double>(_var));
if (!ok) {
throw std::runtime_error("Could not floating point field '" +
std::string(_name) + "' to object.");
}
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
bson_append_int64(_parent->val_, _name.data(),
static_cast<int>(_name.size()),
static_cast<std::int64_t>(_var));
const bool ok = bson_append_int64(_parent->val_, _name.data(),
static_cast<int>(_name.size()),
static_cast<std::int64_t>(_var));
if (!ok) {
throw std::runtime_error("Could not int field '" + std::string(_name) +
"' to object.");
}
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bson_oid_t>()) {
bson_append_oid(_parent->val_, _name.data(),
static_cast<int>(_name.size()), &_var);
const bool ok = bson_append_oid(_parent->val_, _name.data(),
static_cast<int>(_name.size()), &_var);
if (!ok) {
throw std::runtime_error("Could not oid field '" + std::string(_name) +
"' to object.");
}
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
return OutputVarType{};
}
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_array(OutputArrayType* _arr) const;
void end_object(OutputObjectType* _obj) const noexcept;
void end_object(OutputObjectType* _obj) const;
private:
/// Pointer to the main document. In BSON, documents are what are usually
+43 -20
View File
@@ -3,10 +3,13 @@
#include <bson/bson.h>
#include <memory>
#include <ostream>
#include <stdexcept>
#include <utility>
#include "../Processors.hpp"
#include "../Result.hpp"
#include "../internal/ptr_cast.hpp"
#include "../parsing/Parent.hpp"
#include "Parser.hpp"
@@ -17,44 +20,64 @@ namespace bson {
/// Returns BSON bytes. Careful: It is the responsibility of the caller to call
/// bson_free on the returned pointer.
template <class... Ps>
std::pair<uint8_t*, size_t> to_buffer(const auto& _obj) noexcept {
Result<std::pair<uint8_t*, size_t>> to_buffer(const auto& _obj) noexcept {
using T = std::remove_cvref_t<decltype(_obj)>;
using ParentType = parsing::Parent<Writer>;
bson_t* doc = nullptr;
uint8_t* buf = nullptr;
size_t buflen = 0;
bson_writer_t* bson_writer =
bson_writer_new(&buf, &buflen, 0, bson_realloc_ctx, NULL);
bson_writer_begin(bson_writer, &doc);
auto bson_writer = std::shared_ptr<bson_writer_t>(
bson_writer_new(&buf, &buflen, 0, bson_realloc_ctx, NULL),
bson_writer_destroy);
bson_writer_begin(bson_writer.get(), &doc);
const auto rfl_writer = Writer(doc);
using ProcessorsType = Processors<Ps...>;
static_assert(!ProcessorsType::no_field_names_,
"The NoFieldNames processor is not supported for BSON, XML, "
"TOML, or YAML.");
Parser<T, ProcessorsType>::write(rfl_writer, _obj,
typename ParentType::Root{});
bson_writer_end(bson_writer);
const auto len = bson_writer_get_length(bson_writer);
bson_writer_destroy(bson_writer);
return std::make_pair(buf, len);
const auto nothing = [&]() -> Result<Nothing> {
try {
Parser<T, ProcessorsType>::write(rfl_writer, _obj,
typename ParentType::Root{});
return Nothing{};
} catch (const std::exception& e) {
return error(e.what());
}
}();
bson_writer_end(bson_writer.get());
const auto len = bson_writer_get_length(bson_writer.get());
return nothing
.transform([&](const auto&) { return std::make_pair(buf, len); })
.or_else([&](auto&& _err) {
bson_free(buf);
return error(_err.what());
});
}
/// Returns BSON bytes.
template <class... Ps>
std::vector<char> write(const auto& _obj) noexcept {
auto [buf, len] = to_buffer<Ps...>(_obj);
const auto result = std::vector<char>(internal::ptr_cast<char*>(buf),
internal::ptr_cast<char*>(buf) + len);
bson_free(buf);
return result;
std::vector<char> write(const auto& _obj) {
return to_buffer<Ps...>(_obj)
.transform([](const auto& _p) {
const auto vec =
std::vector<char>(internal::ptr_cast<char*>(_p.first),
internal::ptr_cast<char*>(_p.first) + _p.second);
bson_free(_p.first);
return vec;
})
.value();
}
/// Writes a BSON into an ostream.
template <class... Ps>
std::ostream& write(const auto& _obj, std::ostream& _stream) noexcept {
auto [buf, len] = to_buffer<Ps...>(_obj);
_stream.write(internal::ptr_cast<const char*>(buf), len);
bson_free(buf);
std::ostream& write(const auto& _obj, std::ostream& _stream) {
to_buffer<Ps...>(_obj)
.transform([&](const auto& _p) {
_stream.write(internal::ptr_cast<const char*>(_p.first), _p.second);
bson_free(_p.first);
return Nothing{};
})
.value();
return _stream;
}
+39 -39
View File
@@ -10,14 +10,14 @@
#include <string_view>
#include <type_traits>
//#include "../Box.hpp"
// #include "../Box.hpp"
#include "../Bytestring.hpp"
//#include "../Ref.hpp"
//#include "../Result.hpp"
// #include "../Ref.hpp"
// #include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../internal/is_literal.hpp"
//#include "../internal/ptr_cast.hpp"
// #include "../internal/ptr_cast.hpp"
#include "../common.hpp"
namespace rfl::capnproto {
@@ -57,109 +57,109 @@ class RFL_API Writer {
~Writer();
template <class T>
OutputArrayType array_as_root(const T /*_size*/) const noexcept {
OutputArrayType array_as_root(const T /*_size*/) const {
static_assert(always_false_v<T>,
"In Cap'n Proto, root values must always be structs.");
throw std::runtime_error("Unsupported.");
}
template <class T>
OutputMapType map_as_root(const T /*_size*/) const noexcept {
OutputMapType map_as_root(const T /*_size*/) const {
static_assert(always_false_v<T>,
"In Cap'n Proto, root values must always be structs.");
throw std::runtime_error("Unsupported.");
}
Writer::OutputObjectType object_as_root(const size_t _size) const noexcept;
Writer::OutputObjectType object_as_root(const size_t _size) const;
template <class T = int>
OutputVarType null_as_root() const noexcept {
OutputVarType null_as_root() const {
static_assert(always_false_v<T>,
"In Cap'n Proto, root values must always be structs.");
throw std::runtime_error("Unsupported.");
}
template <class T = int>
OutputUnionType union_as_root() const noexcept {
OutputUnionType union_as_root() const {
static_assert(always_false_v<T>,
"In Cap'n Proto, root values must always be structs.");
throw std::runtime_error("Unsupported.");
}
template <class T>
OutputVarType value_as_root(const T& /*_var*/) const noexcept {
OutputVarType value_as_root(const T& /*_var*/) const {
static_assert(always_false_v<T>,
"In Cap'n Proto, root values must always be structs.");
throw std::runtime_error("Unsupported.");
}
OutputArrayType add_array_to_array(const size_t _size,
OutputArrayType* _parent) const noexcept;
OutputArrayType* _parent) const;
OutputArrayType add_array_to_map(const std::string_view& _name,
const size_t _size,
OutputMapType* _parent) const noexcept;
OutputMapType* _parent) const;
OutputArrayType add_array_to_object(const std::string_view& _name,
const size_t _size,
OutputObjectType* _parent) const noexcept;
OutputObjectType* _parent) const;
OutputArrayType add_array_to_union(const size_t _index, const size_t _size,
OutputUnionType* _parent) const noexcept;
OutputUnionType* _parent) const;
OutputMapType add_map_to_array(const size_t _size,
OutputArrayType* _parent) const noexcept;
OutputArrayType* _parent) const;
OutputMapType add_map_to_map(const std::string_view& _name,
const size_t _size,
OutputMapType* _parent) const noexcept;
OutputMapType* _parent) const;
OutputMapType add_map_to_object(const std::string_view& _name,
const size_t _size,
OutputObjectType* _parent) const noexcept;
OutputObjectType* _parent) const;
OutputMapType add_map_to_union(const size_t _index, const size_t _size,
OutputUnionType* _parent) const noexcept;
OutputUnionType* _parent) const;
OutputObjectType add_object_to_array(const size_t _size,
OutputArrayType* _parent) const noexcept;
OutputArrayType* _parent) const;
OutputObjectType add_object_to_map(const std::string_view& _name,
const size_t _size,
OutputMapType* _parent) const noexcept;
OutputMapType* _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;
OutputObjectType add_object_to_union(const size_t _index, const size_t _size,
OutputUnionType* _parent) const noexcept;
OutputUnionType* _parent) const;
OutputUnionType add_union_to_array(OutputArrayType* _parent) const noexcept;
OutputUnionType add_union_to_array(OutputArrayType* _parent) const;
OutputUnionType add_union_to_map(const std::string_view& _name,
OutputMapType* _parent) const noexcept;
OutputMapType* _parent) const;
OutputUnionType add_union_to_object(const std::string_view& _name,
OutputObjectType* _parent) const noexcept;
OutputObjectType* _parent) const;
OutputUnionType add_union_to_union(const size_t _index,
OutputUnionType* _parent) const noexcept;
OutputUnionType* _parent) const;
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
OutputVarType add_null_to_array(OutputArrayType* _parent) const;
OutputVarType add_null_to_map(const std::string_view& _name,
OutputMapType* _parent) const noexcept;
OutputMapType* _parent) const;
OutputVarType add_null_to_object(const std::string_view& _name,
OutputObjectType* _parent) const noexcept;
OutputObjectType* _parent) const;
OutputVarType add_null_to_union(const size_t _index,
OutputUnionType* _parent) const noexcept;
OutputUnionType* _parent) const;
template <class T>
OutputVarType add_value_to_array(const T& _var,
OutputArrayType* _parent) const noexcept {
OutputArrayType* _parent) const {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
_parent->val_.set(_parent->ix_++, _var.c_str());
@@ -189,7 +189,7 @@ class RFL_API Writer {
template <class T>
OutputVarType add_value_to_map(const std::string_view& _name, const T& _var,
OutputMapType* _parent) const noexcept {
OutputMapType* _parent) const {
auto entries =
OutputArrayType{_parent->val_.get("entries").as<capnp::DynamicList>()};
auto new_entry = add_object_to_array(2, &entries);
@@ -200,7 +200,7 @@ class RFL_API Writer {
template <class T>
OutputVarType add_value_to_object(const std::string_view& _name,
const T& _var,
OutputObjectType* _parent) const noexcept {
OutputObjectType* _parent) const {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
_parent->val_.set(_name.data(), _var.c_str());
@@ -228,7 +228,7 @@ class RFL_API Writer {
template <class T>
OutputVarType add_value_to_union(const size_t _index, const T& _var,
OutputUnionType* _parent) const noexcept {
OutputUnionType* _parent) const {
const auto field = _parent->val_.getSchema().getFields()[_index];
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
@@ -256,11 +256,11 @@ class RFL_API Writer {
return OutputVarType{};
}
void end_array(OutputArrayType* /*_arr*/) const noexcept {}
void end_array(OutputArrayType* /*_arr*/) const {}
void end_map(OutputMapType* /*_obj*/) const noexcept {}
void end_map(OutputMapType* /*_obj*/) const {}
void end_object(OutputObjectType* /*_obj*/) const noexcept {}
void end_object(OutputObjectType* /*_obj*/) const {}
private:
capnp::DynamicStruct::Builder* root_;
+20 -22
View File
@@ -2,14 +2,12 @@
#define RFL_CBOR_WRITER_HPP_
#include <jsoncons_ext/cbor/cbor_encoder.hpp>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
//#include "../Box.hpp"
#include "../Bytestring.hpp"
//#include "../Ref.hpp"
//#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../common.hpp"
@@ -34,61 +32,61 @@ 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) const noexcept;
OutputObjectType object_as_root(const size_t) 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,
OutputArrayType* _parent) const noexcept;
OutputArrayType* _parent) const;
OutputObjectType add_object_to_object(
const std::string_view& _name, const size_t,
OutputObjectType* _parent) const noexcept;
OutputObjectType add_object_to_object(const std::string_view& _name,
const size_t,
OutputObjectType* _parent) const;
template <class T>
OutputVarType add_value_to_array(const T& _var,
OutputArrayType* /*_parent*/) const noexcept {
OutputArrayType* /*_parent*/) 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 {
OutputObjectType* /*_parent*/) const {
encoder_->key(_name);
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_array(OutputArrayType* _arr) const;
void end_object(OutputObjectType* _obj) const noexcept;
void end_object(OutputObjectType* _obj) const;
private:
OutputArrayType new_array(const size_t _size) const noexcept;
OutputArrayType new_array(const size_t _size) const;
OutputObjectType new_object() const noexcept;
OutputObjectType new_object() const;
template <class T>
OutputVarType new_value(const T& _var) const noexcept {
OutputVarType new_value(const T& _var) const {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
encoder_->string_value(_var);
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
+2 -2
View File
@@ -13,7 +13,7 @@ namespace rfl::cbor {
/// Returns CBOR 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>;
std::vector<uint8_t> buffer;
@@ -28,7 +28,7 @@ std::vector<char> write(const auto& _obj) noexcept {
/// Writes a CBOR 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;
+25 -25
View File
@@ -36,57 +36,57 @@ struct 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 insert_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* /*_parent*/) const {
return insert_value(_var);
}
template <class T>
OutputVarType add_value_to_object(
const std::string_view& _name, const T& _var,
OutputObjectType* /*_parent*/) const noexcept {
OutputVarType add_value_to_object(const std::string_view& _name,
const T& _var,
OutputObjectType* /*_parent*/) const {
return insert_value(_name, _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_array(OutputArrayType* _arr) const;
void end_object(OutputObjectType* _obj) const noexcept;
void end_object(OutputObjectType* _obj) const;
private:
template <class T>
OutputVarType insert_value(const std::string_view& _name,
const T& _var) const noexcept {
const T& _var) const {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
fbb_->String(_name.data(), _var);
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
@@ -107,7 +107,7 @@ struct RFL_API Writer {
}
template <class T>
OutputVarType insert_value(const T& _var) const noexcept {
OutputVarType insert_value(const T& _var) const {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
fbb_->String(_var);
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
@@ -125,13 +125,13 @@ struct RFL_API Writer {
return OutputVarType{};
}
OutputArrayType new_array(const std::string_view& _name) const noexcept;
OutputArrayType new_array(const std::string_view& _name) const;
OutputArrayType new_array() const noexcept;
OutputArrayType new_array() const;
OutputObjectType new_object(const std::string_view& _name) const noexcept;
OutputObjectType new_object(const std::string_view& _name) const;
OutputObjectType new_object() const noexcept;
OutputObjectType new_object() const;
private:
Ref<flexbuffers::Builder> fbb_;
+41 -23
View File
@@ -7,11 +7,13 @@
#include "../thirdparty/yyjson.h"
#endif
#include <memory>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
//#include "../Result.hpp"
// #include "../Result.hpp"
#include "../always_false.hpp"
#include "../common.hpp"
@@ -44,7 +46,11 @@ class RFL_API Writer {
using OutputObjectType = YYJSONOutputObject;
using OutputVarType = YYJSONOutputVar;
Writer(yyjson_mut_doc* _doc);
Writer();
~Writer() = default;
yyjson_mut_doc* doc() const { return doc_.get(); }
OutputArrayType array_as_root(const size_t) const noexcept;
@@ -55,46 +61,53 @@ class RFL_API Writer {
template <class T>
OutputVarType value_as_root(const T& _var) const noexcept {
const auto val = from_basic_type(_var);
yyjson_mut_doc_set_root(doc_, val.val_);
yyjson_mut_doc_set_root(doc(), val.val_);
return OutputVarType(val);
}
OutputArrayType add_array_to_array(const size_t,
OutputArrayType* _parent) const noexcept;
OutputArrayType* _parent) const;
OutputArrayType add_array_to_object(const std::string_view& _name,
const size_t,
OutputObjectType* _parent) const noexcept;
OutputObjectType* _parent) const;
OutputObjectType add_object_to_array(const size_t,
OutputArrayType* _parent) const noexcept;
OutputArrayType* _parent) const;
OutputObjectType add_object_to_object(
const std::string_view& _name, const size_t,
OutputObjectType* _parent) const noexcept;
OutputObjectType add_object_to_object(const std::string_view& _name,
const size_t,
OutputObjectType* _parent) const;
template <class T>
OutputVarType add_value_to_array(const T& _var,
OutputArrayType* _parent) const noexcept {
OutputArrayType* _parent) const {
const auto val = from_basic_type(_var);
yyjson_mut_arr_add_val(_parent->val_, val.val_);
const bool ok = yyjson_mut_arr_add_val(_parent->val_, val.val_);
if (!ok) {
throw std::runtime_error("Could not add value to array.");
}
return OutputVarType(val);
}
template <class T>
OutputVarType add_value_to_object(const std::string_view& _name,
const T& _var,
OutputObjectType* _parent) const noexcept {
OutputObjectType* _parent) const {
const auto val = from_basic_type(_var);
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.data()),
val.val_);
const bool ok = yyjson_mut_obj_add(
_parent->val_, yyjson_mut_strcpy(doc(), _name.data()), val.val_);
if (!ok) {
throw std::runtime_error("Could not add field '" + std::string(_name) +
"' to object.");
}
return OutputVarType(val);
}
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*) const noexcept;
@@ -104,22 +117,27 @@ class RFL_API Writer {
template <class T>
OutputVarType from_basic_type(const T& _var) const noexcept {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
return OutputVarType(yyjson_mut_strcpy(doc_, _var.c_str()));
return OutputVarType(yyjson_mut_strcpy(doc(), _var.c_str()));
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
return OutputVarType(yyjson_mut_bool(doc_, _var));
return OutputVarType(yyjson_mut_bool(doc(), _var));
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
return OutputVarType(yyjson_mut_real(doc_, static_cast<double>(_var)));
return OutputVarType(yyjson_mut_real(doc(), static_cast<double>(_var)));
} else if constexpr (std::is_unsigned<std::remove_cvref_t<T>>()) {
return OutputVarType(yyjson_mut_uint(doc_, static_cast<uint64_t>(_var)));
return OutputVarType(yyjson_mut_uint(doc(), static_cast<uint64_t>(_var)));
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
return OutputVarType(yyjson_mut_int(doc_, static_cast<int64_t>(_var)));
return OutputVarType(yyjson_mut_int(doc(), static_cast<int64_t>(_var)));
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
}
public:
yyjson_mut_doc* doc_;
private:
std::shared_ptr<yyjson_mut_doc> doc_;
};
} // namespace json
+5 -6
View File
@@ -9,6 +9,7 @@
#endif
#include <ostream>
#include <stdexcept>
#include <string>
#include "../Processors.hpp"
@@ -26,18 +27,17 @@ template <class... Ps>
std::string write(const auto& _obj, const yyjson_write_flag _flag = 0) {
using T = std::remove_cvref_t<decltype(_obj)>;
using ParentType = parsing::Parent<Writer>;
auto w = Writer(yyjson_mut_doc_new(NULL));
auto w = Writer();
Parser<T, Processors<Ps...>>::write(w, _obj, typename ParentType::Root{});
yyjson_write_err err;
const char* json_c_str =
yyjson_mut_write_opts(w.doc_, _flag, NULL, NULL, &err);
yyjson_mut_write_opts(w.doc(), _flag, NULL, NULL, &err);
if (!json_c_str) {
throw std::runtime_error("An error occured while writing to JSON: " +
std::string(err.msg));
}
const auto json_str = std::string(json_c_str);
free((void*)json_c_str);
yyjson_mut_doc_free(w.doc_);
return json_str;
}
@@ -47,18 +47,17 @@ std::ostream& write(const auto& _obj, std::ostream& _stream,
const yyjson_write_flag _flag = 0) {
using T = std::remove_cvref_t<decltype(_obj)>;
using ParentType = parsing::Parent<Writer>;
auto w = Writer(yyjson_mut_doc_new(NULL));
auto w = Writer();
Parser<T, Processors<Ps...>>::write(w, _obj, typename ParentType::Root{});
yyjson_write_err err;
const char* json_c_str =
yyjson_mut_write_opts(w.doc_, _flag, NULL, NULL, &err);
yyjson_mut_write_opts(w.doc(), _flag, NULL, NULL, &err);
if (!json_c_str) {
throw std::runtime_error("An error occured while writing to JSON: " +
std::string(err.msg));
}
_stream << json_c_str;
free((void*)json_c_str);
yyjson_mut_doc_free(w.doc_);
return _stream;
}
+4 -11
View File
@@ -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);
+66 -36
View File
@@ -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.");
}
+12 -5
View File
@@ -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;
+3 -4
View File
@@ -127,7 +127,7 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>,
const std::string& _disc_value,
const InputVarType& _var,
ResultType* _result,
bool* _match_found) noexcept {
bool* _match_found) {
using AlternativeType = std::remove_cvref_t<
std::variant_alternative_t<_i, std::variant<AlternativeTypes...>>>;
@@ -206,8 +206,7 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>,
/// Writes a wrapped version of the original object, which contains the tag.
template <class T, class P>
static void write_wrapped(const W& _w, const T& _val,
const P& _parent) noexcept {
static void write_wrapped(const W& _w, const T& _val, const P& _parent) {
const auto wrapped = wrap_if_necessary<T>(_val);
Parser<R, W, std::remove_cvref_t<decltype(wrapped)>, ProcessorsType>::write(
_w, wrapped, _parent);
@@ -216,7 +215,7 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>,
/// Generates a wrapped version of the original object, which contains the
/// tag, if the object doesn't already contain the wrap.
template <class T>
static auto wrap_if_necessary(const T& _val) noexcept {
static auto wrap_if_necessary(const T& _val) {
if constexpr (named_tuple_t<T>::Names::template contains<
_discriminator>()) {
return _val;
+16 -16
View File
@@ -36,36 +36,36 @@ class RFL_API Writer {
~Writer();
template <class T>
OutputArrayType array_as_root(const T _size) const noexcept;
OutputArrayType array_as_root(const 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 {
static_assert(rfl::always_false_v<T>,
"TOML only allows tables as the root element.");
return OutputVarType{};
}
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 {
OutputArrayType* _parent) const {
_parent->val_->push_back(::toml::value(_var));
return OutputVarType{};
}
@@ -73,19 +73,19 @@ class RFL_API Writer {
template <class T>
OutputVarType add_value_to_object(const std::string_view& _name,
const T& _var,
OutputObjectType* _parent) const noexcept {
OutputObjectType* _parent) const {
_parent->val_->emplace(_name, ::toml::value(_var));
return OutputVarType{};
}
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_array(OutputArrayType* _arr) const;
void end_object(OutputObjectType* _obj) const noexcept;
void end_object(OutputObjectType* _obj) const;
private:
::toml::table* root_;
+19 -19
View File
@@ -38,61 +38,61 @@ 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 {
OutputArrayType* _parent) 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 {
OutputObjectType* _parent) const {
encoder_->key(_name);
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_array(OutputArrayType* _arr) const;
void end_object(OutputObjectType* _obj) const noexcept;
void end_object(OutputObjectType* _obj) const;
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 {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
encoder_->string_value(_var);
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
+2 -2
View File
@@ -16,7 +16,7 @@ namespace rfl::ubjson {
/// Returns UBJSON 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>;
std::vector<uint8_t> buffer;
@@ -31,7 +31,7 @@ std::vector<char> write(const auto& _obj) noexcept {
/// Writes a UBJSON 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;
+25 -26
View File
@@ -40,60 +40,60 @@ struct 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 {
const auto str = to_string(_var);
return value_as_root_impl(str);
}
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 {
OutputArrayType* _parent) const {
const auto str = to_string(_var);
return add_value_to_array_impl(str, _parent);
}
template <class T>
OutputVarType add_value_to_object(
const std::string_view& _name, const T& _var, OutputObjectType* _parent,
const bool _is_attribute = false) const noexcept {
OutputVarType add_value_to_object(const std::string_view& _name,
const T& _var, OutputObjectType* _parent,
const bool _is_attribute = false) const {
const auto str = to_string(_var);
return add_value_to_object_impl(_name, str, _parent, _is_attribute);
}
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 bool _is_attribute = false) const noexcept;
OutputVarType add_null_to_object(const std::string_view& _name,
OutputObjectType* _parent,
const bool _is_attribute = false) const;
void end_array(OutputArrayType* _arr) const noexcept;
void end_array(OutputArrayType* _arr) const;
void end_object(OutputObjectType* _obj) const noexcept;
void end_object(OutputObjectType* _obj) const;
private:
template <class T>
std::string to_string(const T& _val) const noexcept {
std::string to_string(const T& _val) const {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
return _val;
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
@@ -106,15 +106,14 @@ struct RFL_API Writer {
}
}
OutputVarType value_as_root_impl(const std::string& _str) const noexcept;
OutputVarType value_as_root_impl(const std::string& _str) const;
OutputVarType add_value_to_array_impl(
const std::string& _str, OutputArrayType* _parent) const noexcept;
OutputVarType add_value_to_array_impl(const std::string& _str,
OutputArrayType* _parent) const;
OutputVarType add_value_to_object_impl(
const std::string_view& _name, const std::string& _str,
OutputObjectType* _parent,
const bool _is_attribute = false) const noexcept;
OutputObjectType* _parent, const bool _is_attribute = false) const;
public:
Ref<pugi::xml_node> root_;
+22 -22
View File
@@ -30,57 +30,57 @@ 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 insert_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 {
OutputArrayType* _parent) const {
return insert_value(_var);
}
template <class T>
OutputVarType add_value_to_object(const std::string_view& _name,
const T& _var,
OutputObjectType* _parent) const noexcept {
OutputObjectType* _parent) const {
return insert_value(_name, _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_array(OutputArrayType* _arr) const;
void end_object(OutputObjectType* _obj) const noexcept;
void end_object(OutputObjectType* _obj) const;
private:
template <class T>
OutputVarType insert_value(const std::string_view& _name,
const T& _var) const noexcept {
const T& _var) const {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>() ||
std::is_same<std::remove_cvref_t<T>, bool>() ||
std::is_same<std::remove_cvref_t<T>,
@@ -101,7 +101,7 @@ class RFL_API Writer {
}
template <class T>
OutputVarType insert_value(const T& _var) const noexcept {
OutputVarType insert_value(const T& _var) const {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>() ||
std::is_same<std::remove_cvref_t<T>, bool>() ||
std::is_same<std::remove_cvref_t<T>,
@@ -119,13 +119,13 @@ class RFL_API Writer {
return OutputVarType{};
}
OutputArrayType new_array(const std::string_view& _name) const noexcept;
OutputArrayType new_array(const std::string_view& _name) const;
OutputArrayType new_array() const noexcept;
OutputArrayType new_array() const;
OutputObjectType new_object(const std::string_view& _name) const noexcept;
OutputObjectType new_object(const std::string_view& _name) const;
OutputObjectType new_object() const noexcept;
OutputObjectType new_object() const;
public:
const Ref<YAML::Emitter> out_;
+65 -23
View File
@@ -23,60 +23,93 @@ Writer::OutputVarType Writer::null_as_root() const noexcept {
}
Writer::OutputArrayType Writer::add_array_to_array(
const size_t /*_size*/, OutputArrayType* _parent) const noexcept {
const size_t /*_size*/, OutputArrayType* _parent) const {
bson_array_builder_t* val;
bson_array_builder_append_array_builder_begin(_parent->val_, &val);
const bool ok =
bson_array_builder_append_array_builder_begin(_parent->val_, &val);
if (!ok) {
throw std::runtime_error("Could not array to array.");
}
return OutputArrayType(val, IsArray{_parent->val_});
}
Writer::OutputArrayType Writer::add_array_to_object(
const std::string_view& _name, const size_t /*_size*/,
OutputObjectType* _parent) const noexcept {
OutputObjectType* _parent) const {
bson_array_builder_t* val;
bson_append_array_builder_begin(_parent->val_, _name.data(),
static_cast<int>(_name.size()), &val);
const bool ok = bson_append_array_builder_begin(
_parent->val_, _name.data(), static_cast<int>(_name.size()), &val);
if (!ok) {
throw std::runtime_error("Could not array to array.");
}
return OutputArrayType(val, IsObject{_parent->val_});
}
Writer::OutputObjectType Writer::add_object_to_array(
const size_t /*_size*/, OutputArrayType* _parent) const noexcept {
const size_t /*_size*/, OutputArrayType* _parent) const {
subdocs_->emplace_back(rfl::Box<BSONType>());
bson_array_builder_append_document_begin(_parent->val_,
&(subdocs_->back()->val_));
const bool ok = bson_array_builder_append_document_begin(
_parent->val_, &(subdocs_->back()->val_));
if (!ok) {
throw std::runtime_error("Could not object to array.");
}
return OutputObjectType(&subdocs_->back()->val_, IsArray{_parent->val_});
}
Writer::OutputObjectType Writer::add_object_to_object(
const std::string_view& _name, const size_t /*_size*/,
OutputObjectType* _parent) const noexcept {
OutputObjectType* _parent) const {
subdocs_->emplace_back(rfl::Box<BSONType>());
bson_append_document_begin(_parent->val_, _name.data(),
static_cast<int>(_name.size()),
&(subdocs_->back()->val_));
const bool ok = bson_append_document_begin(_parent->val_, _name.data(),
static_cast<int>(_name.size()),
&(subdocs_->back()->val_));
if (!ok) {
throw std::runtime_error("Could not add object to object.");
}
return OutputObjectType(&subdocs_->back()->val_, IsObject{_parent->val_});
}
Writer::OutputVarType Writer::add_null_to_array(
OutputArrayType* _parent) const noexcept {
bson_array_builder_append_null(_parent->val_);
OutputArrayType* _parent) const {
const bool ok = bson_array_builder_append_null(_parent->val_);
if (!ok) {
throw std::runtime_error("Could not add null to array.");
}
return OutputVarType{};
}
Writer::OutputVarType Writer::add_null_to_object(
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
bson_append_null(_parent->val_, _name.data(), static_cast<int>(_name.size()));
const std::string_view& _name, OutputObjectType* _parent) const {
const bool ok = bson_append_null(_parent->val_, _name.data(),
static_cast<int>(_name.size()));
if (!ok) {
throw std::runtime_error("Could not add null to object.");
}
return OutputVarType{};
}
void Writer::end_array(OutputArrayType* _arr) const noexcept {
void Writer::end_array(OutputArrayType* _arr) const {
const auto handle = [&](const auto _parent) {
using Type = std::remove_cvref_t<decltype(_parent)>;
if constexpr (std::is_same<Type, IsArray>()) {
bson_array_builder_append_array_builder_end(_parent.ptr_, _arr->val_);
const bool ok =
bson_array_builder_append_array_builder_end(_parent.ptr_, _arr->val_);
if (!ok) {
throw std::runtime_error("Could not end array.");
}
} else if constexpr (std::is_same<Type, IsObject>()) {
bson_append_array_builder_end(_parent.ptr_, _arr->val_);
const bool ok = bson_append_array_builder_end(_parent.ptr_, _arr->val_);
if (!ok) {
throw std::runtime_error("Could not end array.");
}
} else if constexpr (std::is_same<Type, IsRoot>()) {
bson_array_builder_build(_arr->val_, doc_);
const bool ok = bson_array_builder_build(_arr->val_, doc_);
if (!ok) {
throw std::runtime_error("Could not end array.");
}
} else {
static_assert(rfl::always_false_v<Type>, "Unsupported type.");
}
@@ -84,13 +117,22 @@ void Writer::end_array(OutputArrayType* _arr) const noexcept {
std::visit(handle, _arr->parent_);
}
void Writer::end_object(OutputObjectType* _obj) const noexcept {
void Writer::end_object(OutputObjectType* _obj) const {
const auto handle = [&](const auto _parent) {
using Type = std::remove_cvref_t<decltype(_parent)>;
if constexpr (std::is_same<Type, IsArray>()) {
bson_array_builder_append_document_end(_parent.ptr_, _obj->val_);
const bool ok =
bson_array_builder_append_document_end(_parent.ptr_, _obj->val_);
if (!ok) {
throw std::runtime_error("Could not end object.");
}
} else if constexpr (std::is_same<Type, IsObject>()) {
bson_append_document_end(_parent.ptr_, _obj->val_);
const bool ok = bson_append_document_end(_parent.ptr_, _obj->val_);
if (!ok) {
throw std::runtime_error("Could not end object.");
}
} else if constexpr (std::is_same<Type, IsRoot>()) {
} else {
static_assert(rfl::always_false_v<Type>, "Unsupported type.");
+48 -45
View File
@@ -9,10 +9,11 @@ namespace rfl::capnproto {
static_assert(parsing::schemaful::IsSchemafulWriter<Writer>,
"This must be a schemaful writer.");
inline unsigned int SafeSizeToUInt(size_t sizeValue) {
if (sizeValue > static_cast<size_t>(std::numeric_limits<unsigned int>::max())) {
throw std::overflow_error("size_t value is too big to fit into an unsigned int.");
if (sizeValue >
static_cast<size_t>(std::numeric_limits<unsigned int>::max())) {
throw std::overflow_error(
"size_t value is too big to fit into an unsigned int.");
}
return static_cast<unsigned int>(sizeValue);
}
@@ -21,20 +22,20 @@ Writer::Writer(capnp::DynamicStruct::Builder* _root) : root_(_root){};
Writer::~Writer() = default;
Writer::OutputObjectType Writer::object_as_root(
const size_t /*_size*/) const noexcept {
Writer::OutputObjectType Writer::object_as_root(const size_t /*_size*/) const {
return OutputObjectType{root_->as<capnp::DynamicStruct>()};
}
Writer::OutputArrayType Writer::add_array_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
const size_t _size, OutputArrayType* _parent) const {
return OutputArrayType{
_parent->val_.init(SafeSizeToUInt(_parent->ix_++), SafeSizeToUInt(_size)).as<capnp::DynamicList>()};
_parent->val_.init(SafeSizeToUInt(_parent->ix_++), SafeSizeToUInt(_size))
.as<capnp::DynamicList>()};
}
Writer::OutputArrayType Writer::add_array_to_map(
const std::string_view& _name, const size_t _size,
OutputMapType* _parent) const noexcept {
Writer::OutputArrayType Writer::add_array_to_map(const std::string_view& _name,
const size_t _size,
OutputMapType* _parent) const {
auto entries = OutputArrayType{
.val_ = _parent->val_.get("entries").as<capnp::DynamicList>(),
.ix_ = _parent->ix_++};
@@ -45,30 +46,29 @@ Writer::OutputArrayType Writer::add_array_to_map(
Writer::OutputArrayType Writer::add_array_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
return OutputArrayType{
_parent->val_.init(_name.data(), SafeSizeToUInt(_size)).as<capnp::DynamicList>()};
OutputObjectType* _parent) const {
return OutputArrayType{_parent->val_.init(_name.data(), SafeSizeToUInt(_size))
.as<capnp::DynamicList>()};
}
Writer::OutputArrayType Writer::add_array_to_union(
const size_t _index, const size_t _size,
OutputUnionType* _parent) const noexcept {
const auto field = _parent->val_.getSchema().getFields()[SafeSizeToUInt(_index)];
return OutputArrayType{
_parent->val_.init(field, SafeSizeToUInt(_size)).template as<capnp::DynamicList>()};
const size_t _index, const size_t _size, OutputUnionType* _parent) const {
const auto field =
_parent->val_.getSchema().getFields()[SafeSizeToUInt(_index)];
return OutputArrayType{_parent->val_.init(field, SafeSizeToUInt(_size))
.template as<capnp::DynamicList>()};
}
Writer::OutputMapType Writer::add_map_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
Writer::OutputMapType Writer::add_map_to_array(const size_t _size,
OutputArrayType* _parent) const {
auto new_map = add_object_to_array(1, _parent);
add_array_to_object("entries", _size, &new_map);
return OutputMapType{new_map.val_};
}
Writer::OutputMapType Writer::add_map_to_map(
const std::string_view& _name, const size_t _size,
OutputMapType* _parent) const noexcept {
Writer::OutputMapType Writer::add_map_to_map(const std::string_view& _name,
const size_t _size,
OutputMapType* _parent) const {
auto new_map = add_object_to_map(_name, 1, _parent);
add_array_to_object("entries", _size, &new_map);
return OutputMapType{new_map.val_};
@@ -76,29 +76,29 @@ Writer::OutputMapType Writer::add_map_to_map(
Writer::OutputMapType Writer::add_map_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
OutputObjectType* _parent) const {
auto new_map = add_object_to_object(_name, 1, _parent);
add_array_to_object("entries", _size, &new_map);
return OutputMapType{new_map.val_};
}
Writer::OutputMapType Writer::add_map_to_union(
const size_t _index, const size_t _size,
OutputUnionType* _parent) const noexcept {
Writer::OutputMapType Writer::add_map_to_union(const size_t _index,
const size_t _size,
OutputUnionType* _parent) const {
auto new_map = add_object_to_union(_index, 1, _parent);
add_array_to_object("entries", _size, &new_map);
return OutputMapType{new_map.val_};
}
Writer::OutputObjectType Writer::add_object_to_array(
const size_t /*_size*/, OutputArrayType* _parent) const noexcept {
const size_t /*_size*/, OutputArrayType* _parent) const {
return OutputObjectType{
_parent->val_[SafeSizeToUInt(_parent->ix_++)].as<capnp::DynamicStruct>()};
}
Writer::OutputObjectType Writer::add_object_to_map(
const std::string_view& _name, const size_t _size,
OutputMapType* _parent) const noexcept {
OutputMapType* _parent) const {
auto entries = OutputArrayType{
.val_ = _parent->val_.get("entries").as<capnp::DynamicList>(),
.ix_ = _parent->ix_++};
@@ -109,27 +109,28 @@ Writer::OutputObjectType Writer::add_object_to_map(
Writer::OutputObjectType Writer::add_object_to_object(
const std::string_view& _name, const size_t /*_size*/,
OutputObjectType* _parent) const noexcept {
OutputObjectType* _parent) const {
return OutputObjectType{
_parent->val_.get(_name.data()).as<capnp::DynamicStruct>()};
}
Writer::OutputObjectType Writer::add_object_to_union(
const size_t _index, const size_t /*_size*/,
OutputUnionType* _parent) const noexcept {
const auto field = _parent->val_.getSchema().getFields()[SafeSizeToUInt(_index)];
OutputUnionType* _parent) const {
const auto field =
_parent->val_.getSchema().getFields()[SafeSizeToUInt(_index)];
return OutputObjectType{
_parent->val_.init(field).template as<capnp::DynamicStruct>()};
}
Writer::OutputUnionType Writer::add_union_to_array(
OutputArrayType* _parent) const noexcept {
OutputArrayType* _parent) const {
return OutputUnionType{
_parent->val_[SafeSizeToUInt(_parent->ix_++)].as<capnp::DynamicStruct>()};
}
Writer::OutputUnionType Writer::add_union_to_map(
const std::string_view& _name, OutputMapType* _parent) const noexcept {
Writer::OutputUnionType Writer::add_union_to_map(const std::string_view& _name,
OutputMapType* _parent) const {
auto entries = OutputArrayType{
.val_ = _parent->val_.get("entries").as<capnp::DynamicList>(),
.ix_ = _parent->ix_++};
@@ -139,26 +140,27 @@ Writer::OutputUnionType Writer::add_union_to_map(
}
Writer::OutputUnionType Writer::add_union_to_object(
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
const std::string_view& _name, OutputObjectType* _parent) const {
return OutputUnionType{
_parent->val_.get(_name.data()).as<capnp::DynamicStruct>()};
}
Writer::OutputUnionType Writer::add_union_to_union(
const size_t _index, OutputUnionType* _parent) const noexcept {
const auto field = _parent->val_.getSchema().getFields()[SafeSizeToUInt(_index)];
const size_t _index, OutputUnionType* _parent) const {
const auto field =
_parent->val_.getSchema().getFields()[SafeSizeToUInt(_index)];
return OutputUnionType{
_parent->val_.init(field).template as<capnp::DynamicStruct>()};
}
Writer::OutputVarType Writer::add_null_to_array(
OutputArrayType* _parent) const noexcept {
OutputArrayType* _parent) const {
_parent->val_.set(SafeSizeToUInt(_parent->ix_++), capnp::VOID);
return OutputVarType{};
}
Writer::OutputVarType Writer::add_null_to_map(
const std::string_view& _name, OutputMapType* _parent) const noexcept {
Writer::OutputVarType Writer::add_null_to_map(const std::string_view& _name,
OutputMapType* _parent) const {
auto entries = OutputArrayType{
.val_ = _parent->val_.get("entries").as<capnp::DynamicList>(),
.ix_ = _parent->ix_++};
@@ -168,14 +170,15 @@ Writer::OutputVarType Writer::add_null_to_map(
}
Writer::OutputVarType Writer::add_null_to_object(
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
const std::string_view& _name, OutputObjectType* _parent) const {
_parent->val_.set(_name.data(), capnp::VOID);
return OutputVarType{};
}
Writer::OutputVarType Writer::add_null_to_union(
const size_t _index, OutputUnionType* _parent) const noexcept {
const auto field = _parent->val_.getSchema().getFields()[SafeSizeToUInt(_index)];
const size_t _index, OutputUnionType* _parent) const {
const auto field =
_parent->val_.getSchema().getFields()[SafeSizeToUInt(_index)];
_parent->val_.set(field, capnp::VOID);
return OutputVarType{};
}
+13 -15
View File
@@ -6,72 +6,70 @@ Writer::Writer(Encoder* _encoder) : encoder_(_encoder) {}
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) const noexcept {
Writer::OutputObjectType Writer::object_as_root(const size_t) const {
return new_object();
}
Writer::OutputVarType Writer::null_as_root() const noexcept {
Writer::OutputVarType Writer::null_as_root() const {
encoder_->null_value();
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 {
OutputObjectType* /*_parent*/) const {
encoder_->key(_name);
return new_array(_size);
}
Writer::OutputObjectType Writer::add_object_to_array(
const size_t, OutputArrayType* /*_parent*/) const noexcept {
const size_t, OutputArrayType* /*_parent*/) const {
return new_object();
}
Writer::OutputObjectType Writer::add_object_to_object(
const std::string_view& _name, const size_t,
OutputObjectType* /*_parent*/) const noexcept {
OutputObjectType* /*_parent*/) const {
encoder_->key(_name);
return new_object();
}
Writer::OutputVarType Writer::add_null_to_array(
OutputArrayType* /*_parent*/) const noexcept {
OutputArrayType* /*_parent*/) const {
encoder_->null_value();
return OutputVarType{};
}
Writer::OutputVarType Writer::add_null_to_object(
const std::string_view& _name, OutputObjectType* /*_parent*/) const noexcept {
const std::string_view& _name, OutputObjectType* /*_parent*/) const {
encoder_->key(_name);
encoder_->null_value();
return OutputVarType{};
}
void Writer::end_array(OutputArrayType* /*_arr*/) const noexcept {
void Writer::end_array(OutputArrayType* /*_arr*/) const {
encoder_->end_array();
}
void Writer::end_object(OutputObjectType* /*_obj*/) const noexcept {
void Writer::end_object(OutputObjectType* /*_obj*/) const {
encoder_->end_object();
}
Writer::OutputArrayType Writer::new_array(const size_t _size) const noexcept {
Writer::OutputArrayType Writer::new_array(const size_t _size) const {
encoder_->begin_array(_size);
return OutputArrayType{};
}
Writer::OutputObjectType Writer::new_object() const noexcept {
Writer::OutputObjectType Writer::new_object() const {
encoder_->begin_object();
return OutputObjectType{};
}
+17 -18
View File
@@ -1,86 +1,85 @@
#include "rfl/flexbuf/Writer.hpp"
#include <stdexcept>
namespace rfl::flexbuf {
Writer::Writer(const Ref<flexbuffers::Builder>& _fbb) : fbb_(_fbb) {}
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();
}
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();
}
Writer::OutputVarType Writer::null_as_root() const noexcept {
Writer::OutputVarType Writer::null_as_root() const {
fbb_->Null();
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();
}
Writer::OutputArrayType Writer::add_array_to_object(
const std::string_view& _name, const size_t /*_size*/,
OutputObjectType* /*_parent*/) const noexcept {
OutputObjectType* /*_parent*/) const {
return new_array(_name);
}
Writer::OutputObjectType Writer::add_object_to_array(
const size_t /*_size*/, OutputArrayType* /*_parent*/) const noexcept {
const size_t /*_size*/, OutputArrayType* /*_parent*/) const {
return new_object();
}
Writer::OutputObjectType Writer::add_object_to_object(
const std::string_view& _name, const size_t /*_size*/,
OutputObjectType* /*_parent*/) const noexcept {
OutputObjectType* /*_parent*/) const {
return new_object(_name);
}
Writer::OutputVarType Writer::add_null_to_array(
OutputArrayType* /*_parent*/) const noexcept {
OutputArrayType* /*_parent*/) const {
fbb_->Null();
return OutputVarType{};
}
Writer::OutputVarType Writer::add_null_to_object(
const std::string_view& _name, OutputObjectType* /*_parent*/) const noexcept {
const std::string_view& _name, OutputObjectType* /*_parent*/) const {
fbb_->Null(_name.data());
return OutputVarType{};
}
void Writer::end_array(OutputArrayType* _arr) const noexcept {
void Writer::end_array(OutputArrayType* _arr) const {
fbb_->EndVector(_arr->start_, false, false);
}
void Writer::end_object(OutputObjectType* _obj) const noexcept {
void Writer::end_object(OutputObjectType* _obj) const {
fbb_->EndMap(_obj->start_);
}
Writer::OutputArrayType Writer::new_array(
const std::string_view& _name) const noexcept {
Writer::OutputArrayType Writer::new_array(const std::string_view& _name) const {
const auto start = fbb_->StartVector(_name.data());
return OutputArrayType{start};
}
Writer::OutputArrayType Writer::new_array() const noexcept {
Writer::OutputArrayType Writer::new_array() const {
const auto start = fbb_->StartVector();
return OutputArrayType{start};
}
Writer::OutputObjectType Writer::new_object(
const std::string_view& _name) const noexcept {
const std::string_view& _name) const {
const auto start = fbb_->StartMap(_name.data());
return OutputObjectType{start};
}
Writer::OutputObjectType Writer::new_object() const noexcept {
Writer::OutputObjectType Writer::new_object() const {
const auto start = fbb_->StartMap();
return OutputObjectType{start};
}
+51 -26
View File
@@ -28,68 +28,93 @@ SOFTWARE.
namespace rfl::json {
Writer::Writer(yyjson_mut_doc* _doc) : doc_(_doc) {}
Writer::Writer()
: doc_(std::shared_ptr<yyjson_mut_doc>(yyjson_mut_doc_new(NULL),
yyjson_mut_doc_free)) {}
Writer::OutputArrayType Writer::array_as_root(const size_t) const noexcept {
const auto arr = yyjson_mut_arr(doc_);
yyjson_mut_doc_set_root(doc_, arr);
const auto arr = yyjson_mut_arr(doc());
yyjson_mut_doc_set_root(doc(), arr);
return OutputArrayType(arr);
}
Writer::OutputObjectType Writer::object_as_root(const size_t) const noexcept {
const auto obj = yyjson_mut_obj(doc_);
yyjson_mut_doc_set_root(doc_, obj);
const auto obj = yyjson_mut_obj(doc());
yyjson_mut_doc_set_root(doc(), obj);
return OutputObjectType(obj);
}
Writer::OutputVarType Writer::null_as_root() const noexcept {
const auto null = yyjson_mut_null(doc_);
yyjson_mut_doc_set_root(doc_, null);
const auto null = yyjson_mut_null(doc());
yyjson_mut_doc_set_root(doc(), null);
return OutputVarType(null);
}
Writer::OutputArrayType Writer::add_array_to_array(
const size_t, OutputArrayType* _parent) const noexcept {
const auto arr = yyjson_mut_arr(doc_);
yyjson_mut_arr_add_val(_parent->val_, arr);
const size_t, OutputArrayType* _parent) const {
const auto arr = yyjson_mut_arr(doc());
const bool ok = yyjson_mut_arr_add_val(_parent->val_, arr);
if (!ok) {
throw std::runtime_error("Adding an array to an array failed.");
}
return OutputArrayType(arr);
}
Writer::OutputArrayType Writer::add_array_to_object(
const std::string_view& _name, const size_t,
OutputObjectType* _parent) const noexcept {
const auto arr = yyjson_mut_arr(doc_);
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.data()), arr);
OutputObjectType* _parent) const {
const auto arr = yyjson_mut_arr(doc());
const bool ok = yyjson_mut_obj_add(
_parent->val_, yyjson_mut_strcpy(doc(), _name.data()), arr);
if (!ok) {
throw std::runtime_error("Could not add field '" + std::string(_name) +
"' to object.");
}
return OutputArrayType(arr);
}
Writer::OutputObjectType Writer::add_object_to_array(
const size_t, OutputArrayType* _parent) const noexcept {
const auto obj = yyjson_mut_obj(doc_);
yyjson_mut_arr_add_val(_parent->val_, obj);
const size_t, OutputArrayType* _parent) const {
const auto obj = yyjson_mut_obj(doc());
const bool ok = yyjson_mut_arr_add_val(_parent->val_, obj);
if (!ok) {
throw std::runtime_error("Adding an object to an array failed.");
}
return OutputObjectType(obj);
}
Writer::OutputObjectType Writer::add_object_to_object(
const std::string_view& _name, const size_t,
OutputObjectType* _parent) const noexcept {
const auto obj = yyjson_mut_obj(doc_);
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.data()), obj);
OutputObjectType* _parent) const {
const auto obj = yyjson_mut_obj(doc());
const auto ok = yyjson_mut_obj_add(
_parent->val_, yyjson_mut_strcpy(doc(), _name.data()), obj);
if (!ok) {
throw std::runtime_error("Could not add field '" + std::string(_name) +
"' to object.");
}
return OutputObjectType(obj);
}
Writer::OutputVarType Writer::add_null_to_array(
OutputArrayType* _parent) const noexcept {
const auto null = yyjson_mut_null(doc_);
yyjson_mut_arr_add_val(_parent->val_, null);
OutputArrayType* _parent) const {
const auto null = yyjson_mut_null(doc());
const bool ok = yyjson_mut_arr_add_val(_parent->val_, null);
if (!ok) {
throw std::runtime_error("Adding null to an array failed.");
}
return OutputVarType(null);
}
Writer::OutputVarType Writer::add_null_to_object(
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
const auto null = yyjson_mut_null(doc_);
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.data()),
null);
const std::string_view& _name, OutputObjectType* _parent) const {
const auto null = yyjson_mut_null(doc());
const bool ok = yyjson_mut_obj_add(
_parent->val_, yyjson_mut_strcpy(doc(), _name.data()), null);
if (!ok) {
throw std::runtime_error("Could not add field '" + std::string(_name) +
"' to object.");
}
return OutputVarType(null);
}
+66 -26
View File
@@ -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{};
}
+10 -13
View File
@@ -6,17 +6,14 @@ Writer::Writer(::toml::table* _root) : root_(_root) {}
Writer::~Writer() = default;
Writer::OutputObjectType Writer::object_as_root(
const size_t /*_size*/) const noexcept {
Writer::OutputObjectType Writer::object_as_root(const size_t /*_size*/) const {
return OutputObjectType{root_};
}
Writer::OutputVarType Writer::null_as_root() const noexcept {
return OutputVarType{};
}
Writer::OutputVarType Writer::null_as_root() const { return OutputVarType{}; }
Writer::OutputArrayType Writer::add_array_to_array(
const size_t /*_size*/, OutputArrayType* _parent) const noexcept {
const size_t /*_size*/, OutputArrayType* _parent) const {
const auto i = _parent->val_->size();
_parent->val_->push_back(::toml::array());
return OutputArrayType{_parent->val_->at(i).as_array()};
@@ -24,13 +21,13 @@ Writer::OutputArrayType Writer::add_array_to_array(
Writer::OutputArrayType Writer::add_array_to_object(
const std::string_view& _name, const size_t /*_size*/,
OutputObjectType* _parent) const noexcept {
OutputObjectType* _parent) const {
_parent->val_->emplace(_name, ::toml::array());
return OutputArrayType{_parent->val_->at_path(_name).as_array()};
}
Writer::OutputObjectType Writer::add_object_to_array(
const size_t /*_size*/, OutputArrayType* _parent) const noexcept {
const size_t /*_size*/, OutputArrayType* _parent) const {
const auto i = _parent->val_->size();
_parent->val_->push_back(::toml::table());
return OutputObjectType{_parent->val_->at(i).as_table()};
@@ -38,25 +35,25 @@ Writer::OutputObjectType Writer::add_object_to_array(
Writer::OutputObjectType Writer::add_object_to_object(
const std::string_view& _name, const size_t /*_size*/,
OutputObjectType* _parent) const noexcept {
OutputObjectType* _parent) const {
_parent->val_->emplace(_name, ::toml::table());
return OutputObjectType{_parent->val_->at_path(_name).as_table()};
}
Writer::OutputVarType Writer::add_null_to_array(
OutputArrayType* _parent) const noexcept {
OutputArrayType* _parent) const {
_parent->val_->push_back(std::string(""));
return OutputVarType{};
}
Writer::OutputVarType Writer::add_null_to_object(
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
const std::string_view& _name, OutputObjectType* _parent) const {
_parent->val_->emplace(_name, ::toml::value(std::string("")));
return OutputVarType{};
}
void Writer::end_array(OutputArrayType* /*_arr*/) const noexcept {}
void Writer::end_array(OutputArrayType* /*_arr*/) const {}
void Writer::end_object(OutputObjectType* /*_obj*/) const noexcept {}
void Writer::end_object(OutputObjectType* /*_obj*/) const {}
} // namespace rfl::toml
+13 -15
View File
@@ -6,72 +6,70 @@ Writer::Writer(Encoder* _encoder) : encoder_(_encoder) {}
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 {
Writer::OutputVarType Writer::null_as_root() const {
encoder_->null_value();
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 {
OutputObjectType* /*_parent*/) const {
encoder_->key(_name);
return new_array(_size);
}
Writer::OutputObjectType Writer::add_object_to_array(
const size_t _size, OutputArrayType* /*_parent*/) const noexcept {
const size_t _size, OutputArrayType* /*_parent*/) 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 {
OutputObjectType* /*_parent*/) const {
encoder_->key(_name);
return new_object(_size);
}
Writer::OutputVarType Writer::add_null_to_array(
OutputArrayType* /*_parent*/) const noexcept {
OutputArrayType* /*_parent*/) const {
encoder_->null_value();
return OutputVarType{};
}
Writer::OutputVarType Writer::add_null_to_object(
const std::string_view& _name, OutputObjectType* /*_parent*/) const noexcept {
const std::string_view& _name, OutputObjectType* /*_parent*/) const {
encoder_->key(_name);
encoder_->null_value();
return OutputVarType{};
}
void Writer::end_array(OutputArrayType* /*_arr*/) const noexcept {
void Writer::end_array(OutputArrayType* /*_arr*/) const {
encoder_->end_array();
}
void Writer::end_object(OutputObjectType* /*_obj*/) const noexcept {
void Writer::end_object(OutputObjectType* /*_obj*/) const {
encoder_->end_object();
}
Writer::OutputArrayType Writer::new_array(const size_t _size) const noexcept {
Writer::OutputArrayType Writer::new_array(const size_t _size) const {
encoder_->begin_array(_size);
return OutputArrayType{};
}
Writer::OutputObjectType Writer::new_object(const size_t _size) const noexcept {
Writer::OutputObjectType Writer::new_object(const size_t _size) const {
encoder_->begin_object(_size);
return OutputObjectType{};
}
+14 -16
View File
@@ -37,28 +37,26 @@ Writer::Writer(const Ref<pugi::xml_node>& _root, const std::string& _root_name)
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 {
auto node_child =
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
return OutputArrayType(root_name_, node_child);
}
Writer::OutputObjectType Writer::object_as_root(
const size_t /*_size*/) const noexcept {
Writer::OutputObjectType Writer::object_as_root(const size_t /*_size*/) const {
auto node_child =
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
return OutputObjectType(node_child);
}
Writer::OutputVarType Writer::null_as_root() const noexcept {
Writer::OutputVarType Writer::null_as_root() const {
auto node_child =
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
return OutputVarType(node_child);
}
Writer::OutputVarType Writer::value_as_root_impl(
const std::string& _str) const noexcept {
const std::string& _str) const {
auto node_child =
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
node_child->append_child(pugi::node_pcdata).set_value(_str.c_str());
@@ -66,18 +64,18 @@ Writer::OutputVarType Writer::value_as_root_impl(
}
Writer::OutputArrayType Writer::add_array_to_array(
const size_t /*_size*/, OutputArrayType* _parent) const noexcept {
const size_t /*_size*/, OutputArrayType* _parent) const {
return *_parent;
}
Writer::OutputArrayType Writer::add_array_to_object(
const std::string_view& _name, const size_t /*_size*/,
OutputObjectType* _parent) const noexcept {
OutputObjectType* _parent) const {
return OutputArrayType(_name, _parent->node_);
}
Writer::OutputVarType Writer::add_value_to_array_impl(
const std::string& _str, OutputArrayType* _parent) const noexcept {
const std::string& _str, OutputArrayType* _parent) const {
auto node_child =
Ref<pugi::xml_node>::make(_parent->node_->append_child(_parent->name_));
node_child->append_child(pugi::node_pcdata).set_value(_str.c_str());
@@ -86,7 +84,7 @@ Writer::OutputVarType Writer::add_value_to_array_impl(
Writer::OutputVarType Writer::add_value_to_object_impl(
const std::string_view& _name, const std::string& _str,
OutputObjectType* _parent, const bool _is_attribute) const noexcept {
OutputObjectType* _parent, const bool _is_attribute) const {
if (_is_attribute) {
_parent->node_->append_attribute(_name) = _str.c_str();
return OutputVarType(_parent->node_);
@@ -102,7 +100,7 @@ Writer::OutputVarType Writer::add_value_to_object_impl(
}
Writer::OutputObjectType Writer::add_object_to_array(
const size_t /*_size*/, OutputArrayType* _parent) const noexcept {
const size_t /*_size*/, OutputArrayType* _parent) const {
auto node_child =
Ref<pugi::xml_node>::make(_parent->node_->append_child(_parent->name_));
return OutputObjectType(node_child);
@@ -110,14 +108,14 @@ Writer::OutputObjectType Writer::add_object_to_array(
Writer::OutputObjectType Writer::add_object_to_object(
const std::string_view& _name, const size_t /*_size*/,
OutputObjectType* _parent) const noexcept {
OutputObjectType* _parent) const {
auto node_child =
Ref<pugi::xml_node>::make(_parent->node_->append_child(_name));
return OutputObjectType(node_child);
}
Writer::OutputVarType Writer::add_null_to_array(
OutputArrayType* _parent) const noexcept {
OutputArrayType* _parent) const {
auto node_child =
Ref<pugi::xml_node>::make(_parent->node_->append_child(_parent->name_));
return OutputVarType(node_child);
@@ -125,7 +123,7 @@ Writer::OutputVarType Writer::add_null_to_array(
Writer::OutputVarType Writer::add_null_to_object(
const std::string_view& _name, OutputObjectType* _parent,
const bool _is_attribute) const noexcept {
const bool _is_attribute) const {
if (_is_attribute) {
return OutputVarType(_parent->node_);
} else if (_name == XML_CONTENT) {
@@ -137,8 +135,8 @@ Writer::OutputVarType Writer::add_null_to_object(
}
}
void Writer::end_array(OutputArrayType* /*_arr*/) const noexcept {}
void Writer::end_array(OutputArrayType* /*_arr*/) const {}
void Writer::end_object(OutputObjectType* /*_obj*/) const noexcept {}
void Writer::end_object(OutputObjectType* /*_obj*/) const {}
} // namespace rfl::xml
+15 -18
View File
@@ -6,78 +6,75 @@ Writer::Writer(const Ref<YAML::Emitter>& _out) : out_(_out) {}
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();
}
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();
}
Writer::OutputVarType Writer::null_as_root() const noexcept {
Writer::OutputVarType Writer::null_as_root() const {
return insert_value(YAML::Null);
}
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();
}
Writer::OutputArrayType Writer::add_array_to_object(
const std::string_view& _name, const size_t /*_size*/,
OutputObjectType* /*_parent*/) const noexcept {
OutputObjectType* /*_parent*/) const {
return new_array(_name);
}
Writer::OutputObjectType Writer::add_object_to_array(
const size_t /*_size*/, OutputArrayType* /*_parent*/) const noexcept {
const size_t /*_size*/, OutputArrayType* /*_parent*/) const {
return new_object();
}
Writer::OutputObjectType Writer::add_object_to_object(
const std::string_view& _name, const size_t /*_size*/,
OutputObjectType* /*_parent*/) const noexcept {
OutputObjectType* /*_parent*/) const {
return new_object(_name);
}
Writer::OutputVarType Writer::add_null_to_array(
OutputArrayType* /*_parent*/) const noexcept {
OutputArrayType* /*_parent*/) const {
return insert_value(YAML::Null);
}
Writer::OutputVarType Writer::add_null_to_object(
const std::string_view& _name, OutputObjectType* /*_parent*/) const noexcept {
const std::string_view& _name, OutputObjectType* /*_parent*/) const {
return insert_value(_name, YAML::Null);
}
void Writer::end_array(OutputArrayType* /*_arr*/) const noexcept {
void Writer::end_array(OutputArrayType* /*_arr*/) const {
(*out_) << YAML::EndSeq;
}
void Writer::end_object(OutputObjectType* /*_obj*/) const noexcept {
void Writer::end_object(OutputObjectType* /*_obj*/) const {
(*out_) << YAML::EndMap;
}
Writer::OutputArrayType Writer::new_array(
const std::string_view& _name) const noexcept {
Writer::OutputArrayType Writer::new_array(const std::string_view& _name) const {
(*out_) << YAML::Key << _name.data() << YAML::Value << YAML::BeginSeq;
return OutputArrayType{};
}
Writer::OutputArrayType Writer::new_array() const noexcept {
Writer::OutputArrayType Writer::new_array() const {
(*out_) << YAML::BeginSeq;
return OutputArrayType{};
}
Writer::OutputObjectType Writer::new_object(
const std::string_view& _name) const noexcept {
const std::string_view& _name) const {
(*out_) << YAML::Key << _name.data() << YAML::Value << YAML::BeginMap;
return OutputObjectType{};
}
Writer::OutputObjectType Writer::new_object() const noexcept {
Writer::OutputObjectType Writer::new_object() const {
(*out_) << YAML::BeginMap;
return OutputObjectType{};
}
+45 -69
View File
@@ -1,90 +1,66 @@
#include <gtest/gtest.h>
#include <algorithm>
#include <array>
#include <rfl/cbor.hpp>
#include <gtest/gtest.h>
// Make sure things still compile when
// rfl.hpp is included after rfl/cbor.hpp.
#include <rfl.hpp>
namespace test_read_byte_containers
{
namespace test_read_byte_containers {
struct TestBox
{
int length;
int width;
int height;
struct TestBox {
int length;
int width;
int height;
};
// TODO: Apparently the jsoncons trait is_byte_sequence<T> is not working for std::span.
// TEST(cbor, test_read_from_char_span)
// {
TEST(cbor, test_read_from_byte_view) {
TestBox b = {
.length = 1,
.width = 2,
.height = 3,
};
// TestBox b = {
// .length = 1,
// .width = 2,
// .height = 3,
// };
// TODO: Write directly into desired container, once rfl::cbor::write supports
// it.
std::vector<char> rfl_buffer = rfl::cbor::write(b);
std::array<std::byte, 64> my_buffer;
std::transform(rfl_buffer.begin(), rfl_buffer.end(), my_buffer.begin(),
[](char c) { return static_cast<std::byte>(c); });
// std::vector<char> rfl_buffer = rfl::cbor::write(s);
std::basic_string_view<std::byte> byte_view(my_buffer.data(),
rfl_buffer.size());
// // I don't want to be forced to copy to a std::vector<char> if that's not what data strucure I use.
// // So, allow reading from a std::span
// std::span<char> span(rfl_buffer.data(), rfl_buffer.size());
// auto result = rfl::cbor::read<TestBox>(span);
// EXPECT_TRUE(result);
// EXPECT_EQ(result->one, 1);
// EXPECT_EQ(result->two, 2);
// }
TEST(cbor, test_read_from_byte_view)
{
TestBox b = {
.length = 1,
.width = 2,
.height = 3,
};
// TODO: Write directly into desired container, once rfl::cbor::write supports it.
std::vector<char> rfl_buffer = rfl::cbor::write(b);
std::array<std::byte, 64> my_buffer;
std::transform(rfl_buffer.begin(), rfl_buffer.end(), my_buffer.begin(),
[](char c) { return static_cast<std::byte>(c); });
std::basic_string_view<std::byte> byte_view(my_buffer.data(), rfl_buffer.size());
auto result = rfl::cbor::read<TestBox>(byte_view);
EXPECT_TRUE(result);
EXPECT_EQ(result->length, 1);
EXPECT_EQ(result->width, 2);
EXPECT_EQ(result->height, 3);
auto result = rfl::cbor::read<TestBox>(byte_view);
EXPECT_TRUE(result);
EXPECT_EQ(result->length, 1);
EXPECT_EQ(result->width, 2);
EXPECT_EQ(result->height, 3);
}
TEST(cbor, test_read_from_uint8_array)
{
TestBox b = {
.length = 4,
.width = 5,
.height = 6,
};
TEST(cbor, test_read_from_uint8_array) {
TestBox b = {
.length = 4,
.width = 5,
.height = 6,
};
// TODO: Write directly into desired container, once rfl::cbor::write supports it.
std::vector<char> rfl_buffer = rfl::cbor::write(b);
std::array<std::uint8_t, 64> my_buffer;
std::transform(rfl_buffer.begin(), rfl_buffer.end(), my_buffer.begin(),
[](char c) { return static_cast<std::uint8_t>(c); });
// TODO: Write directly into desired container, once rfl::cbor::write supports
// it.
std::vector<char> rfl_buffer = rfl::cbor::write(b);
auto result = rfl::cbor::read<TestBox>(my_buffer);
EXPECT_TRUE(result);
EXPECT_EQ(result->length, 4);
EXPECT_EQ(result->width, 5);
EXPECT_EQ(result->height, 6);
std::array<std::uint8_t, 64> my_buffer;
std::transform(rfl_buffer.begin(), rfl_buffer.end(), my_buffer.begin(),
[](char c) { return static_cast<std::uint8_t>(c); });
auto result = rfl::cbor::read<TestBox>(my_buffer);
EXPECT_TRUE(result);
EXPECT_EQ(result->length, 4);
EXPECT_EQ(result->width, 5);
EXPECT_EQ(result->height, 6);
}
} // namespace test_read_byte_containers
} // namespace test_read_byte_containers
+31 -24
View File
@@ -6,8 +6,8 @@
#include "write_and_read.hpp"
// NOTE TO MAINTAINERS:
// These tests are very similar to the others in test_add_*tags_to_*variants.cpp, so please keep them
// in sync.
// These tests are very similar to the others in
// test_add_*tags_to_*variants.cpp, so please keep them in sync.
namespace test_variant_auto_tagging {
// test 1 -> normal behaviour
@@ -34,10 +34,12 @@ TEST(cbor, test_add_tags_to_rfl_variants) {
vec.emplace_back(key_pressed_t{'c'});
vec.emplace_back(3);
write_and_read<rfl::AddTagsToVariants>(
vec,
// Diagnostic notation: [{_ "button_pressed_t": {_ }}, {_ "button_released_t": {_ "button": 4}}, {_ "key_pressed": {_ "key": 99}}, {_ "int": 3}]
// See: https://cbor.me/?diag=[{_%20%22button_pressed_t%22:%20{_%20}},%20{_%20%22button_released_t%22:%20{_%20%22button%22:%204}},%20{_%20%22key_pressed%22:%20{_%20%22key%22:%2099}},%20{_%20%22int%22:%203}]
R"(
vec,
// Diagnostic notation: [{_ "button_pressed_t": {_ }}, {_
// "button_released_t": {_ "button": 4}}, {_ "key_pressed": {_ "key":
// 99}}, {_ "int": 3}] See:
// https://cbor.me/?diag=[{_%20%22button_pressed_t%22:%20{_%20}},%20{_%20%22button_released_t%22:%20{_%20%22button%22:%204}},%20{_%20%22key_pressed%22:%20{_%20%22key%22:%2099}},%20{_%20%22int%22:%203}]
R"(
84 # array(4)
BF # map(*)
70 # text(16)
@@ -72,9 +74,10 @@ TEST(cbor, test_add_tags_to_rfl_variants) {
}
// test 2 -> 'Generic' within a struct like this can be read/written with
// rfl::AddNamespacedTagsToVariants so that the underlying `std::variant` holds two 'Generic' fields
// with different namespaces. See the similar test in test_add_tags_to_variants.cpp.
// A hypothetical alternative would be adding an optional manual tag to rfl::Generic
// rfl::AddNamespacedTagsToVariants so that the underlying `std::variant` holds
// two 'Generic' fields with different namespaces. See the similar test in
// test_add_tags_to_variants.cpp. A hypothetical alternative would be adding an
// optional manual tag to rfl::Generic
struct APIResult {
rfl::Generic result;
@@ -86,15 +89,15 @@ struct APIError {
using APICallOutput = std::variant<APIResult, APIError>;
TEST(cbor, test_add_namespaced_tags_to_variants_with_generic) {
std::vector<APICallOutput> output = {
APIResult{"200"},
APIError{"an error"}
};
std::vector<APICallOutput> output = {APIResult{"200"}, APIError{"an error"}};
write_and_read<rfl::AddNamespacedTagsToVariants>(
output,
// Diagnostic notation: [{_ "test_variant_auto_tagging::APIResult": {_ "result": {_ "std::string": "200"}}}, {_ "test_variant_auto_tagging::APIError": {_ "error": {_ "std::string": "an error"}}}]
// See: https://cbor.me/?diag=[{_%20%22test_variant_auto_tagging::APIResult%22:%20{_%20%22result%22:%20{_%20%22std::string%22:%20%22200%22}}},%20{_%20%22test_variant_auto_tagging::APIError%22:%20{_%20%22error%22:%20{_%20%22std::string%22:%20%22an%20error%22}}}]
R"(
output,
// Diagnostic notation: [{_ "test_variant_auto_tagging::APIResult": {_
// "result": {_ "std::string": "200"}}}, {_
// "test_variant_auto_tagging::APIError": {_ "error": {_ "std::string":
// "an error"}}}] See:
// https://cbor.me/?diag=[{_%20%22test_variant_auto_tagging::APIResult%22:%20{_%20%22result%22:%20{_%20%22std::string%22:%20%22200%22}}},%20{_%20%22test_variant_auto_tagging::APIError%22:%20{_%20%22error%22:%20{_%20%22std::string%22:%20%22an%20error%22}}}]
R"(
82 # array(2)
BF # map(*)
78 24 # text(36)
@@ -145,15 +148,19 @@ struct Message {
using Messages = std::variant<Result::Message, Error::Message>;
TEST(json, test_add_namespaced_tags_to_variants_different_namespaces) {
TEST(cbor, test_add_namespaced_tags_to_variants_different_namespaces) {
std::vector<Messages> msgs{
Result::Message{.result="a result"},
Error::Message{.error="an error", .error_id=2},
Result::Message{.result = "a result"},
Error::Message{.error = "an error", .error_id = 2},
};
write_and_read<rfl::AddNamespacedTagsToVariants>(msgs,
// Diagnostic notation: [{_ "test_variant_auto_tagging::Result::Message":{_ "result":"a result"}},{_ "test_variant_auto_tagging::Error::Message":{_ "error":"an error", "error_id":2}}]
// See: https://cbor.me/?diag=[{_%20%22test_variant_auto_tagging::Result::Message%22:{_%20%22result%22:%22a%20result%22}},{_%20%22test_variant_auto_tagging::Error::Message%22:{_%20%22error%22:%22an%20error%22,%20%22error_id%22:2}}]
R"(
write_and_read<rfl::AddNamespacedTagsToVariants>(
msgs,
// Diagnostic notation: [{_
// "test_variant_auto_tagging::Result::Message":{_ "result":"a
// result"}},{_ "test_variant_auto_tagging::Error::Message":{_ "error":"an
// error", "error_id":2}}] See:
// https://cbor.me/?diag=[{_%20%22test_variant_auto_tagging::Result::Message%22:{_%20%22result%22:%22a%20result%22}},{_%20%22test_variant_auto_tagging::Error::Message%22:{_%20%22error%22:%22an%20error%22,%20%22error_id%22:2}}]
R"(
82 # array(2)
BF # map(*)
78 2A # text(42)