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