Move some heavy templates into cpp (#204)

This commit is contained in:
George Evmenov
2024-10-08 22:12:03 +02:00
committed by GitHub
parent 099211943d
commit 9dcd49a1e0
80 changed files with 2291 additions and 1330 deletions
+6 -364
View File
@@ -24,368 +24,10 @@ SOFTWARE.
*/
// ----------------------------------------------------------------------------
// This file include all other source files, so that the user of the library
// don't need to add multiple source files into their build.
// Also, this speeds up compile time, compared to multiple separate .cpp files
// compilation.
#include "rfl/Generic.hpp"
namespace rfl {
Generic::Generic() : value_(false) {}
Generic::Generic(Generic&& _other) noexcept = default;
Generic::Generic(const Generic& _other) = default;
Generic::Generic(const ReflectionType& _value) : value_(_value) {}
Generic::Generic(ReflectionType&& _value) noexcept
: value_(std::move(_value)) {}
Generic::~Generic() = default;
Generic& Generic::operator=(const ReflectionType& _value) {
value_ = _value;
return *this;
}
Generic& Generic::operator=(ReflectionType&& _value) noexcept {
value_ = std::move(_value);
return *this;
}
Generic& Generic::operator=(const Generic& _other) = default;
Generic& Generic::operator=(Generic&& _other) = default;
Result<Generic::Array> Generic::to_array() const noexcept {
if (const auto* ptr = std::get_if<Array>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to an "
"rfl::Generic::Array.");
}
}
Result<bool> Generic::to_bool() const noexcept {
if (const auto* ptr = std::get_if<bool>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to a boolean.");
}
}
Result<double> Generic::to_double() const noexcept {
if (const auto* ptr = std::get_if<double>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to a double.");
}
}
Result<int> Generic::to_int() const noexcept {
if (const auto* ptr = std::get_if<int>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to an integer.");
}
}
Result<Generic::Object> Generic::to_object() const noexcept {
if (const auto* ptr = std::get_if<Object>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to an "
"rfl::Generic::Object.");
}
}
Result<std::string> Generic::to_string() const noexcept {
if (const auto* ptr = std::get_if<std::string>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to a string.");
}
}
} // namespace rfl
// ----------------------------------------------------------------------------
#include "rfl/json/to_schema.hpp"
namespace rfl::json {
schema::Type type_to_json_schema_type(const parsing::schema::Type& _type);
bool is_optional(const parsing::schema::Type& _t) {
const auto handle = [](const auto& _v) -> bool {
using T = std::remove_cvref_t<decltype(_v)>;
return std::is_same<T, parsing::schema::Type::Optional>();
};
return rfl::visit(handle, _t.variant_);
}
std::string numeric_type_to_string(const parsing::schema::Type& _type) {
const auto handle_variant = [](const auto& _t) -> std::string {
using T = std::remove_cvref_t<decltype(_t)>;
using Type = parsing::schema::Type;
if constexpr (std::is_same<T, Type::Int32>() ||
std::is_same<T, Type::Int64>() ||
std::is_same<T, Type::UInt32>() ||
std::is_same<T, Type::UInt64>() ||
std::is_same<T, Type::Integer>()) {
return schema::Type::Integer{}.type.str();
} else {
return schema::Type::Number{}.type.str();
}
};
return rfl::visit(handle_variant, _type.variant_);
}
schema::Type handle_validation_type(
const parsing::schema::Type& _type,
const parsing::schema::ValidationType& _validation_type) {
auto handle_variant = [&](const auto& _v) -> schema::Type {
using T = std::remove_cvref_t<decltype(_v)>;
using ValidationType = parsing::schema::ValidationType;
if constexpr (std::is_same<T, ValidationType::AllOf>()) {
auto all_of = std::vector<schema::Type>();
for (const auto& t : _v.types_) {
all_of.emplace_back(handle_validation_type(_type, t));
}
return schema::Type{.value = schema::Type::AllOf{.allOf = all_of}};
} else if constexpr (std::is_same<T, ValidationType::AnyOf>()) {
auto any_of = std::vector<schema::Type>();
for (const auto& t : _v.types_) {
any_of.emplace_back(handle_validation_type(_type, t));
}
return schema::Type{.value = schema::Type::AnyOf{.anyOf = any_of}};
} else if constexpr (std::is_same<T, ValidationType::OneOf>()) {
auto one_of = std::vector<schema::Type>();
for (const auto& t : _v.types_) {
one_of.emplace_back(handle_validation_type(_type, t));
}
return schema::Type{.value = schema::Type::OneOf{.oneOf = one_of}};
} else if constexpr (std::is_same<T, ValidationType::Regex>()) {
return schema::Type{.value = schema::Type::Regex{.pattern = _v.pattern_}};
} else if constexpr (std::is_same<T, ValidationType::Size>()) {
auto t = type_to_json_schema_type(_type);
const auto to_size = [](const auto _v) {
return static_cast<size_t>(_v);
};
auto handle_size_variant = [&](auto& _t, const auto& _size_limit) {
using U = std::remove_cvref_t<decltype(_t)>;
using V = std::remove_cvref_t<decltype(_size_limit)>;
if constexpr (std::is_same<U, schema::Type::TypedArray>() ||
std::is_same<U, schema::Type::String>()) {
if constexpr (std::is_same<V, ValidationType::Minimum>()) {
_t.minSize = _size_limit.value_.visit(to_size);
return t;
} else if constexpr (std::is_same<V, ValidationType::Maximum>()) {
_t.maxSize = _size_limit.value_.visit(to_size);
return t;
} else if constexpr (std::is_same<V, ValidationType::EqualTo>()) {
_t.minSize = _size_limit.value_.visit(to_size);
_t.maxSize = _size_limit.value_.visit(to_size);
return t;
} else if constexpr (std::is_same<V, ValidationType::AnyOf>() ||
std::is_same<V, ValidationType::AllOf>()) {
V v;
for (const auto& limiter : _size_limit.types_) {
v.types_.push_back(ValidationType{ValidationType::Size{
.size_limit_ = rfl::Ref<ValidationType>::make(limiter)}});
}
return handle_validation_type(_type, ValidationType{.variant_ = v});
}
}
return t;
};
return rfl::visit(handle_size_variant, t.value, _v.size_limit_->variant_);
} else if constexpr (std::is_same<T, ValidationType::ExclusiveMaximum>()) {
return schema::Type{.value = schema::Type::ExclusiveMaximum{
.exclusiveMaximum = _v.value_,
.type = numeric_type_to_string(_type)}};
} else if constexpr (std::is_same<T, ValidationType::ExclusiveMinimum>()) {
return schema::Type{.value = schema::Type::ExclusiveMinimum{
.exclusiveMinimum = _v.value_,
.type = numeric_type_to_string(_type)}};
} else if constexpr (std::is_same<T, ValidationType::Maximum>()) {
return schema::Type{
.value = schema::Type::Maximum{
.maximum = _v.value_, .type = numeric_type_to_string(_type)}};
} else if constexpr (std::is_same<T, ValidationType::Minimum>()) {
return schema::Type{
.value = schema::Type::Minimum{
.minimum = _v.value_, .type = numeric_type_to_string(_type)}};
} else if constexpr (std::is_same<T, ValidationType::EqualTo>()) {
const auto maximum = schema::Type{
.value = schema::Type::Maximum{
.maximum = _v.value_, .type = numeric_type_to_string(_type)}};
const auto minimum = schema::Type{
.value = schema::Type::Minimum{
.minimum = _v.value_, .type = numeric_type_to_string(_type)}};
return schema::Type{.value =
schema::Type::AllOf{.allOf = {maximum, minimum}}};
} else if constexpr (std::is_same<T, ValidationType::NotEqualTo>()) {
const auto excl_maximum =
schema::Type{.value = schema::Type::ExclusiveMaximum{
.exclusiveMaximum = _v.value_,
.type = numeric_type_to_string(_type)}};
const auto excl_minimum =
schema::Type{.value = schema::Type::ExclusiveMinimum{
.exclusiveMinimum = _v.value_,
.type = numeric_type_to_string(_type)}};
return schema::Type{
.value = schema::Type::AnyOf{.anyOf = {excl_maximum, excl_minimum}}};
} else {
static_assert(rfl::always_false_v<T>, "Not all cases were covered.");
}
};
return rfl::visit(handle_variant, _validation_type.variant_);
}
schema::Type type_to_json_schema_type(const parsing::schema::Type& _type) {
auto handle_variant = [](const auto& _t) -> schema::Type {
using T = std::remove_cvref_t<decltype(_t)>;
using Type = parsing::schema::Type;
if constexpr (std::is_same<T, Type::Boolean>()) {
return schema::Type{.value = schema::Type::Boolean{}};
} else if constexpr (std::is_same<T, Type::Int32>() ||
std::is_same<T, Type::Int64>() ||
std::is_same<T, Type::UInt32>() ||
std::is_same<T, Type::UInt64>() ||
std::is_same<T, Type::Integer>()) {
return schema::Type{.value = schema::Type::Integer{}};
} else if constexpr (std::is_same<T, Type::Float>() ||
std::is_same<T, Type::Double>()) {
return schema::Type{.value = schema::Type::Number{}};
} else if constexpr (std::is_same<T, Type::String>()) {
return schema::Type{.value = schema::Type::String{}};
} else if constexpr (std::is_same<T, Type::AnyOf>()) {
auto any_of = std::vector<schema::Type>();
for (const auto& t : _t.types_) {
any_of.emplace_back(type_to_json_schema_type(t));
}
return schema::Type{.value = schema::Type::AnyOf{.anyOf = any_of}};
} else if constexpr (std::is_same<T, Type::Description>()) {
auto res = type_to_json_schema_type(*_t.type_);
const auto update_prediction = [&](auto _v) -> schema::Type {
_v.description = _t.description_;
return schema::Type{_v};
};
return rfl::visit(update_prediction, res.value);
} else if constexpr (std::is_same<T, Type::FixedSizeTypedArray>()) {
return schema::Type{.value = schema::Type::FixedSizeTypedArray{
.items = Ref<schema::Type>::make(
type_to_json_schema_type(*_t.type_)),
.minItems = _t.size_,
.maxItems = _t.size_}};
} else if constexpr (std::is_same<T, Type::Literal>()) {
return schema::Type{.value =
schema::Type::StringEnum{.values = _t.values_}};
} else if constexpr (std::is_same<T, Type::Object>()) {
auto properties = std::map<std::string, schema::Type>();
auto required = std::vector<std::string>();
for (const auto& [k, v] : _t.types_) {
properties[k] = type_to_json_schema_type(v);
if (!is_optional(v)) {
required.push_back(k);
}
}
auto additional_properties =
_t.additional_properties_
? std::make_shared<schema::Type>(
type_to_json_schema_type(*_t.additional_properties_))
: std::shared_ptr<schema::Type>();
return schema::Type{.value = schema::Type::Object{
.properties = properties,
.required = required,
.additionalProperties = additional_properties}};
} else if constexpr (std::is_same<T, Type::Optional>()) {
return schema::Type{.value = schema::Type::AnyOf{
.anyOf = {type_to_json_schema_type(*_t.type_),
schema::Type{schema::Type::Null{}}}}};
} else if constexpr (std::is_same<T, Type::Reference>()) {
return schema::Type{
.value = schema::Type::Reference{.ref = "#/definitions/" + _t.name_}};
} else if constexpr (std::is_same<T, Type::StringMap>()) {
return schema::Type{.value = schema::Type::StringMap{
.additionalProperties = Ref<schema::Type>::make(
type_to_json_schema_type(*_t.value_type_))}};
} else if constexpr (std::is_same<T, Type::Tuple>()) {
auto items = std::vector<schema::Type>();
for (const auto& t : _t.types_) {
items.emplace_back(type_to_json_schema_type(t));
}
return schema::Type{.value = schema::Type::Tuple{.prefixItems = items}};
} else if constexpr (std::is_same<T, Type::TypedArray>()) {
return schema::Type{.value = schema::Type::TypedArray{
.items = Ref<schema::Type>::make(
type_to_json_schema_type(*_t.type_))}};
} else if constexpr (std::is_same<T, Type::Validated>()) {
return handle_validation_type(*_t.type_, _t.validation_);
} else {
static_assert(rfl::always_false_v<T>, "Not all cases were covered.");
}
};
return rfl::visit(handle_variant, _type.variant_);
}
} // namespace rfl::json
// ----------------------------------------------------------------------------
#include "rfl/parsing/schema/Type.hpp"
namespace rfl::parsing::schema {
/// Requires a lot of template instantiation, so we do not want this to be
/// inlined.
Type::Type() : variant_() {}
Type::Type(const VariantType& _variant) : variant_(_variant) {}
Type::~Type() = default;
} // namespace rfl::parsing::schema
// ----------------------------------------------------------------------------
#include "rfl/Generic.cpp"
#include "rfl/parsing/schema/Type.cpp"
+33
View File
@@ -0,0 +1,33 @@
/*
MIT License
Copyright (c) 2023-2024 Code17 GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// This file include all other source files, so that the user of the library
// don't need to add multiple source files into their build.
// Also, this speeds up compile time, compared to multiple separate .cpp files
// compilation.
#include "rfl/bson/Reader.cpp"
#include "rfl/bson/Writer.cpp"
+33
View File
@@ -0,0 +1,33 @@
/*
MIT License
Copyright (c) 2023-2024 Code17 GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// This file include all other source files, so that the user of the library
// don't need to add multiple source files into their build.
// Also, this speeds up compile time, compared to multiple separate .cpp files
// compilation.
#include "rfl/cbor/Reader.cpp"
#include "rfl/cbor/Writer.cpp"
+33
View File
@@ -0,0 +1,33 @@
/*
MIT License
Copyright (c) 2023-2024 Code17 GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// This file include all other source files, so that the user of the library
// don't need to add multiple source files into their build.
// Also, this speeds up compile time, compared to multiple separate .cpp files
// compilation.
#include "rfl/flexbuf/Reader.cpp"
#include "rfl/flexbuf/Writer.cpp"
+34
View File
@@ -0,0 +1,34 @@
/*
MIT License
Copyright (c) 2023-2024 Code17 GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// This file include all other source files, so that the user of the library
// don't need to add multiple source files into their build.
// Also, this speeds up compile time, compared to multiple separate .cpp files
// compilation.
#include "rfl/json/Reader.cpp"
#include "rfl/json/Writer.cpp"
#include "rfl/json/to_schema.cpp"
+33
View File
@@ -0,0 +1,33 @@
/*
MIT License
Copyright (c) 2023-2024 Code17 GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// This file include all other source files, so that the user of the library
// don't need to add multiple source files into their build.
// Also, this speeds up compile time, compared to multiple separate .cpp files
// compilation.
#include "rfl/msgpack/Reader.cpp"
#include "rfl/msgpack/Writer.cpp"
+33
View File
@@ -0,0 +1,33 @@
/*
MIT License
Copyright (c) 2023-2024 Code17 GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// This file include all other source files, so that the user of the library
// don't need to add multiple source files into their build.
// Also, this speeds up compile time, compared to multiple separate .cpp files
// compilation.
#include "rfl/toml/Reader.cpp"
#include "rfl/toml/Writer.cpp"
+33
View File
@@ -0,0 +1,33 @@
/*
MIT License
Copyright (c) 2023-2024 Code17 GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// This file include all other source files, so that the user of the library
// don't need to add multiple source files into their build.
// Also, this speeds up compile time, compared to multiple separate .cpp files
// compilation.
#include "rfl/xml/Reader.cpp"
#include "rfl/xml/Writer.cpp"
+33
View File
@@ -0,0 +1,33 @@
/*
MIT License
Copyright (c) 2023-2024 Code17 GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// This file include all other source files, so that the user of the library
// don't need to add multiple source files into their build.
// Also, this speeds up compile time, compared to multiple separate .cpp files
// compilation.
#include "rfl/yaml/Reader.cpp"
#include "rfl/yaml/Writer.cpp"
+114
View File
@@ -0,0 +1,114 @@
/*
MIT License
Copyright (c) 2023-2024 Code17 GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "rfl/Generic.hpp"
namespace rfl {
Generic::Generic() : value_(false) {}
Generic::Generic(Generic&& _other) noexcept = default;
Generic::Generic(const Generic& _other) = default;
Generic::Generic(const ReflectionType& _value) : value_(_value) {}
Generic::Generic(ReflectionType&& _value) noexcept
: value_(std::move(_value)) {}
Generic::~Generic() = default;
Generic& Generic::operator=(const ReflectionType& _value) {
value_ = _value;
return *this;
}
Generic& Generic::operator=(ReflectionType&& _value) noexcept {
value_ = std::move(_value);
return *this;
}
Generic& Generic::operator=(const Generic& _other) = default;
Generic& Generic::operator=(Generic&& _other) = default;
Result<Generic::Array> Generic::to_array() const noexcept {
if (const auto* ptr = std::get_if<Array>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to an "
"rfl::Generic::Array.");
}
}
Result<bool> Generic::to_bool() const noexcept {
if (const auto* ptr = std::get_if<bool>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to a boolean.");
}
}
Result<double> Generic::to_double() const noexcept {
if (const auto* ptr = std::get_if<double>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to a double.");
}
}
Result<int> Generic::to_int() const noexcept {
if (const auto* ptr = std::get_if<int>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to an integer.");
}
}
Result<Generic::Object> Generic::to_object() const noexcept {
if (const auto* ptr = std::get_if<Object>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to an "
"rfl::Generic::Object.");
}
}
Result<std::string> Generic::to_string() const noexcept {
if (const auto* ptr = std::get_if<std::string>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to a string.");
}
}
} // namespace rfl
+72
View File
@@ -0,0 +1,72 @@
#include "rfl/bson/Reader.hpp"
namespace rfl::bson {
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
const size_t _idx, const InputArrayType& _arr) const noexcept {
bson_t b;
bson_iter_t iter;
const auto doc = _arr.val_->value.v_doc;
if (bson_init_static(&b, doc.data, doc.data_len)) {
if (bson_iter_init(&iter, &b)) {
size_t i = 0;
while (bson_iter_next(&iter)) {
if (i == _idx) {
return to_input_var(&iter);
}
++i;
}
} else {
return Error("Could not init the array iteration.");
}
} else {
return Error("Could not init array.");
}
return Error("Index " + std::to_string(_idx) + " of of bounds.");
}
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
const std::string& _name, const InputObjectType& _obj) const noexcept {
bson_t b;
bson_iter_t iter;
const auto doc = _obj.val_->value.v_doc;
if (bson_init_static(&b, doc.data, doc.data_len)) {
if (bson_iter_init(&iter, &b)) {
while (bson_iter_next(&iter)) {
auto key = std::string(bson_iter_key(&iter));
if (key == _name) {
return to_input_var(&iter);
}
}
}
}
return Error("No field named '" + _name + "' was found.");
}
bool Reader::is_empty(const InputVarType& _var) const noexcept {
return _var.val_->value_type == BSON_TYPE_NULL;
}
rfl::Result<Reader::InputArrayType> Reader::to_array(
const InputVarType& _var) const noexcept {
const auto btype = _var.val_->value_type;
if (btype != BSON_TYPE_ARRAY && btype != BSON_TYPE_DOCUMENT) {
return Error("Could not cast to an array.");
}
return InputArrayType{_var.val_};
}
rfl::Result<Reader::InputObjectType> Reader::to_object(
const InputVarType& _var) const noexcept {
const auto btype = _var.val_->value_type;
if (btype != BSON_TYPE_DOCUMENT) {
return Error("Could not cast to a document.");
}
return InputObjectType{_var.val_};
}
Reader::InputVarType Reader::to_input_var(bson_iter_t* _iter) const noexcept {
return InputVarType{bson_iter_value(_iter)};
}
} // namespace rfl::bson
+102
View File
@@ -0,0 +1,102 @@
#include "rfl/bson/Writer.hpp"
namespace rfl::bson {
Writer::Writer(bson_t* _doc) : doc_(_doc) {}
Writer::~Writer() = default;
Writer::OutputArrayType Writer::array_as_root(
const size_t _size) const noexcept {
bson_array_builder_t* val = bson_array_builder_new();
return OutputArrayType(val, IsRoot{});
}
Writer::OutputObjectType Writer::object_as_root(
const size_t _size) const noexcept {
return OutputObjectType(doc_, IsRoot{});
}
Writer::OutputVarType Writer::null_as_root() const noexcept {
// Appears to be unsupported by the BSON C API.
return OutputVarType{};
}
Writer::OutputArrayType Writer::add_array_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
bson_array_builder_t* val;
bson_array_builder_append_array_builder_begin(_parent->val_, &val);
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 {
bson_array_builder_t* val;
bson_append_array_builder_begin(_parent->val_, _name.data(),
static_cast<int>(_name.size()), &val);
return OutputArrayType(val, IsObject{_parent->val_});
}
Writer::OutputObjectType Writer::add_object_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
subdocs_->emplace_back(rfl::Box<BSONType>());
bson_array_builder_append_document_begin(_parent->val_,
&(subdocs_->back()->val_));
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 {
subdocs_->emplace_back(rfl::Box<BSONType>());
bson_append_document_begin(_parent->val_, _name.data(),
static_cast<int>(_name.size()),
&(subdocs_->back()->val_));
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_);
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()));
return OutputVarType{};
}
void Writer::end_array(OutputArrayType* _arr) const noexcept {
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_);
} else if constexpr (std::is_same<Type, IsObject>()) {
bson_append_array_builder_end(_parent.ptr_, _arr->val_);
} else if constexpr (std::is_same<Type, IsRoot>()) {
bson_array_builder_build(_arr->val_, doc_);
} else {
static_assert(rfl::always_false_v<Type>, "Unsupported type.");
}
};
std::visit(handle, _arr->parent_);
}
void Writer::end_object(OutputObjectType* _obj) const noexcept {
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_);
} else if constexpr (std::is_same<Type, IsObject>()) {
bson_append_document_end(_parent.ptr_, _obj->val_);
} else if constexpr (std::is_same<Type, IsRoot>()) {
} else {
static_assert(rfl::always_false_v<Type>, "Unsupported type.");
}
};
std::visit(handle, _obj->parent_);
}
} // namespace rfl::bson
+116
View File
@@ -0,0 +1,116 @@
#include "rfl/cbor/Reader.hpp"
namespace rfl::cbor {
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
const size_t _idx, const InputArrayType& _arr) const noexcept {
InputVarType var;
auto err = cbor_value_enter_container(&_arr.val_, &var.val_);
if (err != CborNoError && err != CborErrorOutOfMemory) {
return Error(cbor_error_string(err));
}
size_t length = 0;
err = cbor_value_get_array_length(&_arr.val_, &length);
if (err != CborNoError && err != CborErrorOutOfMemory) {
return Error(cbor_error_string(err));
}
if (_idx >= length) {
return Error("Index " + std::to_string(_idx) + " of of bounds.");
}
for (size_t i = 0; i < _idx; ++i) {
err = cbor_value_advance(&var.val_);
if (err != CborNoError && err != CborErrorOutOfMemory) {
return Error(cbor_error_string(err));
}
}
return var;
}
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
const std::string& _name, const InputObjectType& _obj) const noexcept {
InputVarType var;
auto buffer = std::string();
auto err = cbor_value_enter_container(&_obj.val_, &var.val_);
if (err != CborNoError) {
return Error(cbor_error_string(err));
}
size_t length = 0;
err = cbor_value_get_map_length(&_obj.val_, &length);
if (err != CborNoError) {
return Error(cbor_error_string(err));
}
for (size_t i = 0; i < length; ++i) {
if (!cbor_value_is_text_string(&var.val_)) {
return Error("Expected the key to be a string value.");
}
err = get_string(&var.val_, &buffer);
if (err != CborNoError) {
return Error(cbor_error_string(err));
}
err = cbor_value_advance(&var.val_);
if (err != CborNoError) {
return Error(cbor_error_string(err));
}
if (_name == buffer) {
return var;
}
err = cbor_value_advance(&var.val_);
if (err != CborNoError) {
return Error(cbor_error_string(err));
}
}
return Error("No field named '" + _name + "' was found.");
}
bool Reader::is_empty(const InputVarType& _var) const noexcept {
return cbor_value_is_null(&_var.val_);
}
rfl::Result<Reader::InputArrayType> Reader::to_array(
const InputVarType& _var) const noexcept {
if (!cbor_value_is_array(&_var.val_)) {
return Error("Could not cast to an array.");
}
return InputArrayType{_var.val_};
}
rfl::Result<Reader::InputObjectType> Reader::to_object(
const InputVarType& _var) const noexcept {
if (!cbor_value_is_map(&_var.val_)) {
return Error("Could not cast to an object.");
}
return InputObjectType{_var.val_};
}
CborError Reader::get_bytestring(const CborValue* _ptr,
rfl::Bytestring* _str) const noexcept {
size_t length = 0;
auto err = cbor_value_get_string_length(_ptr, &length);
if (err != CborNoError && err != CborErrorOutOfMemory) {
return err;
}
_str->resize(length);
if (length > 0) {
return cbor_value_copy_byte_string(
_ptr, reinterpret_cast<uint8_t*>(&((*_str)[0])), &length, NULL);
} else {
return CborNoError;
}
}
CborError Reader::get_string(const CborValue* _ptr,
std::string* _str) const noexcept {
size_t length = 0;
auto err = cbor_value_get_string_length(_ptr, &length);
if (err != CborNoError && err != CborErrorOutOfMemory) {
return err;
}
_str->resize(length);
if (length > 0) {
return cbor_value_copy_text_string(_ptr, &((*_str)[0]), &length, NULL);
} else {
return CborNoError;
}
}
} // namespace rfl::cbor
+83
View File
@@ -0,0 +1,83 @@
#include "rfl/cbor/Writer.hpp"
namespace rfl::cbor {
Writer::Writer(CborEncoder* _encoder) : encoder_(_encoder) {}
Writer::~Writer() = default;
Writer::OutputArrayType Writer::array_as_root(
const size_t _size) const noexcept {
return new_array(_size, encoder_);
}
Writer::OutputObjectType Writer::object_as_root(
const size_t _size) const noexcept {
return new_object(_size, encoder_);
}
Writer::OutputVarType Writer::null_as_root() const noexcept {
cbor_encode_null(encoder_);
return OutputVarType{};
}
Writer::OutputArrayType Writer::add_array_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
return new_array(_size, _parent->encoder_);
}
Writer::OutputArrayType Writer::add_array_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
return new_array(_size, _parent->encoder_);
}
Writer::OutputObjectType Writer::add_object_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
return new_object(_size, _parent->encoder_);
}
Writer::OutputObjectType Writer::add_object_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
return new_object(_size, _parent->encoder_);
}
Writer::OutputVarType Writer::add_null_to_array(
OutputArrayType* _parent) const noexcept {
cbor_encode_null(_parent->encoder_);
return OutputVarType{};
}
Writer::OutputVarType Writer::add_null_to_object(
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
cbor_encode_null(_parent->encoder_);
return OutputVarType{};
}
void Writer::end_array(OutputArrayType* _arr) const noexcept {
cbor_encoder_close_container(_arr->parent_, _arr->encoder_);
}
void Writer::end_object(OutputObjectType* _obj) const noexcept {
cbor_encoder_close_container(_obj->parent_, _obj->encoder_);
}
Writer::OutputArrayType Writer::new_array(const size_t _size,
CborEncoder* _parent) const noexcept {
subencoders_->emplace_back(rfl::Box<CborEncoder>::make());
cbor_encoder_create_array(_parent, subencoders_->back().get(), _size);
return OutputArrayType{subencoders_->back().get(), _parent};
}
Writer::OutputObjectType Writer::new_object(
const size_t _size, CborEncoder* _parent) const noexcept {
subencoders_->emplace_back(rfl::Box<CborEncoder>::make());
cbor_encoder_create_map(_parent, subencoders_->back().get(), _size);
return OutputObjectType{subencoders_->back().get(), _parent};
}
} // namespace rfl::cbor
+44
View File
@@ -0,0 +1,44 @@
#include "rfl/flexbuf/Reader.hpp"
namespace rfl::flexbuf {
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
const size_t _idx, const InputArrayType& _arr) const noexcept {
if (_idx >= _arr.size()) {
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
}
return _arr[_idx];
}
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
const std::string& _name, const InputObjectType& _obj) const noexcept {
const auto keys = _obj.Keys();
for (size_t i = 0; i < keys.size(); ++i) {
if (_name == keys[i].AsString().c_str()) {
return _obj.Values()[i];
}
}
return rfl::Error("Map does not contain any element called '" + _name + "'.");
}
bool Reader::is_empty(const InputVarType& _var) const noexcept {
return _var.IsNull();
}
rfl::Result<Reader::InputArrayType> Reader::to_array(
const InputVarType& _var) const noexcept {
if (!_var.IsVector()) {
return rfl::Error("Could not cast to Vector.");
}
return _var.AsVector();
}
rfl::Result<Reader::InputObjectType> Reader::to_object(
const InputVarType& _var) const noexcept {
if (!_var.IsMap()) {
return rfl::Error("Could not cast to Map!");
}
return _var.AsMap();
}
} // namespace rfl::flexbuf
+88
View File
@@ -0,0 +1,88 @@
#include "rfl/flexbuf/Writer.hpp"
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 {
return new_array();
}
Writer::OutputObjectType Writer::object_as_root(
const size_t _size) const noexcept {
return new_object();
}
Writer::OutputVarType Writer::null_as_root() const noexcept {
fbb_->Null();
return OutputVarType{};
}
Writer::OutputArrayType Writer::add_array_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
return new_array();
}
Writer::OutputArrayType Writer::add_array_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
return new_array(_name);
}
Writer::OutputObjectType Writer::add_object_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
return new_object();
}
Writer::OutputObjectType Writer::add_object_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
return new_object(_name);
}
Writer::OutputVarType Writer::add_null_to_array(
OutputArrayType* _parent) const noexcept {
fbb_->Null();
return OutputVarType{};
}
Writer::OutputVarType Writer::add_null_to_object(
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
fbb_->Null(_name.data());
return OutputVarType{};
}
void Writer::end_array(OutputArrayType* _arr) const noexcept {
fbb_->EndVector(_arr->start_, false, false);
}
void Writer::end_object(OutputObjectType* _obj) const noexcept {
fbb_->EndMap(_obj->start_);
}
Writer::OutputArrayType Writer::new_array(
const std::string_view& _name) const noexcept {
const auto start = fbb_->StartVector(_name.data());
return OutputArrayType{start};
}
Writer::OutputArrayType Writer::new_array() const noexcept {
const auto start = fbb_->StartVector();
return OutputArrayType{start};
}
Writer::OutputObjectType Writer::new_object(
const std::string_view& _name) const noexcept {
const auto start = fbb_->StartMap(_name.data());
return OutputObjectType{start};
}
Writer::OutputObjectType Writer::new_object() const noexcept {
const auto start = fbb_->StartMap();
return OutputObjectType{start};
}
} // namespace rfl::flexbuf
+69
View File
@@ -0,0 +1,69 @@
/*
MIT License
Copyright (c) 2023-2024 Code17 GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "rfl/json/Reader.hpp"
namespace rfl::json {
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
const size_t _idx, const InputArrayType _arr) const noexcept {
const auto var = InputVarType(yyjson_arr_get(_arr.val_, _idx));
if (!var.val_) {
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
}
return var;
}
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
const std::string& _name, const InputObjectType _obj) const noexcept {
const auto var = InputVarType(yyjson_obj_get(_obj.val_, _name.c_str()));
if (!var.val_) {
return rfl::Error("Object contains no field named '" + _name + "'.");
}
return var;
}
bool Reader::is_empty(const InputVarType _var) const noexcept {
return !_var.val_ || yyjson_is_null(_var.val_);
}
rfl::Result<Reader::InputArrayType> Reader::to_array(
const InputVarType _var) const noexcept {
if (!yyjson_is_arr(_var.val_)) {
return rfl::Error("Could not cast to array!");
}
return InputArrayType(_var.val_);
}
rfl::Result<Reader::InputObjectType> Reader::to_object(
const InputVarType _var) const noexcept {
if (!yyjson_is_obj(_var.val_)) {
return rfl::Error("Could not cast to object!");
}
return InputObjectType(_var.val_);
}
} // namespace rfl::json
+100
View File
@@ -0,0 +1,100 @@
/*
MIT License
Copyright (c) 2023-2024 Code17 GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "rfl/json/Writer.hpp"
namespace rfl::json {
Writer::Writer(yyjson_mut_doc* _doc) : doc_(_doc) {}
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);
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);
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);
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);
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);
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);
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);
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);
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);
return OutputVarType(null);
}
void Writer::end_array(OutputArrayType*) const noexcept {}
void Writer::end_object(OutputObjectType*) const noexcept {}
} // namespace rfl::json
+298
View File
@@ -0,0 +1,298 @@
/*
MIT License
Copyright (c) 2023-2024 Code17 GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "rfl/json/to_schema.hpp"
namespace rfl::json {
schema::Type type_to_json_schema_type(const parsing::schema::Type& _type);
bool is_optional(const parsing::schema::Type& _t) {
const auto handle = [](const auto& _v) -> bool {
using T = std::remove_cvref_t<decltype(_v)>;
return std::is_same<T, parsing::schema::Type::Optional>();
};
return rfl::visit(handle, _t.variant_);
}
std::string numeric_type_to_string(const parsing::schema::Type& _type) {
const auto handle_variant = [](const auto& _t) -> std::string {
using T = std::remove_cvref_t<decltype(_t)>;
using Type = parsing::schema::Type;
if constexpr (std::is_same<T, Type::Int32>() ||
std::is_same<T, Type::Int64>() ||
std::is_same<T, Type::UInt32>() ||
std::is_same<T, Type::UInt64>() ||
std::is_same<T, Type::Integer>()) {
return schema::Type::Integer{}.type.str();
} else {
return schema::Type::Number{}.type.str();
}
};
return rfl::visit(handle_variant, _type.variant_);
}
schema::Type handle_validation_type(
const parsing::schema::Type& _type,
const parsing::schema::ValidationType& _validation_type) {
auto handle_variant = [&](const auto& _v) -> schema::Type {
using T = std::remove_cvref_t<decltype(_v)>;
using ValidationType = parsing::schema::ValidationType;
if constexpr (std::is_same<T, ValidationType::AllOf>()) {
auto all_of = std::vector<schema::Type>();
for (const auto& t : _v.types_) {
all_of.emplace_back(handle_validation_type(_type, t));
}
return schema::Type{.value = schema::Type::AllOf{.allOf = all_of}};
} else if constexpr (std::is_same<T, ValidationType::AnyOf>()) {
auto any_of = std::vector<schema::Type>();
for (const auto& t : _v.types_) {
any_of.emplace_back(handle_validation_type(_type, t));
}
return schema::Type{.value = schema::Type::AnyOf{.anyOf = any_of}};
} else if constexpr (std::is_same<T, ValidationType::OneOf>()) {
auto one_of = std::vector<schema::Type>();
for (const auto& t : _v.types_) {
one_of.emplace_back(handle_validation_type(_type, t));
}
return schema::Type{.value = schema::Type::OneOf{.oneOf = one_of}};
} else if constexpr (std::is_same<T, ValidationType::Regex>()) {
return schema::Type{.value = schema::Type::Regex{.pattern = _v.pattern_}};
} else if constexpr (std::is_same<T, ValidationType::Size>()) {
auto t = type_to_json_schema_type(_type);
const auto to_size = [](const auto _v) {
return static_cast<size_t>(_v);
};
auto handle_size_variant = [&](auto& _t, const auto& _size_limit) {
using U = std::remove_cvref_t<decltype(_t)>;
using V = std::remove_cvref_t<decltype(_size_limit)>;
if constexpr (std::is_same<U, schema::Type::TypedArray>() ||
std::is_same<U, schema::Type::String>()) {
if constexpr (std::is_same<V, ValidationType::Minimum>()) {
_t.minSize = _size_limit.value_.visit(to_size);
return t;
} else if constexpr (std::is_same<V, ValidationType::Maximum>()) {
_t.maxSize = _size_limit.value_.visit(to_size);
return t;
} else if constexpr (std::is_same<V, ValidationType::EqualTo>()) {
_t.minSize = _size_limit.value_.visit(to_size);
_t.maxSize = _size_limit.value_.visit(to_size);
return t;
} else if constexpr (std::is_same<V, ValidationType::AnyOf>() ||
std::is_same<V, ValidationType::AllOf>()) {
V v;
for (const auto& limiter : _size_limit.types_) {
v.types_.push_back(ValidationType{ValidationType::Size{
.size_limit_ = rfl::Ref<ValidationType>::make(limiter)}});
}
return handle_validation_type(_type, ValidationType{.variant_ = v});
}
}
return t;
};
return rfl::visit(handle_size_variant, t.value, _v.size_limit_->variant_);
} else if constexpr (std::is_same<T, ValidationType::ExclusiveMaximum>()) {
return schema::Type{.value = schema::Type::ExclusiveMaximum{
.exclusiveMaximum = _v.value_,
.type = numeric_type_to_string(_type)}};
} else if constexpr (std::is_same<T, ValidationType::ExclusiveMinimum>()) {
return schema::Type{.value = schema::Type::ExclusiveMinimum{
.exclusiveMinimum = _v.value_,
.type = numeric_type_to_string(_type)}};
} else if constexpr (std::is_same<T, ValidationType::Maximum>()) {
return schema::Type{
.value = schema::Type::Maximum{
.maximum = _v.value_, .type = numeric_type_to_string(_type)}};
} else if constexpr (std::is_same<T, ValidationType::Minimum>()) {
return schema::Type{
.value = schema::Type::Minimum{
.minimum = _v.value_, .type = numeric_type_to_string(_type)}};
} else if constexpr (std::is_same<T, ValidationType::EqualTo>()) {
const auto maximum = schema::Type{
.value = schema::Type::Maximum{
.maximum = _v.value_, .type = numeric_type_to_string(_type)}};
const auto minimum = schema::Type{
.value = schema::Type::Minimum{
.minimum = _v.value_, .type = numeric_type_to_string(_type)}};
return schema::Type{.value =
schema::Type::AllOf{.allOf = {maximum, minimum}}};
} else if constexpr (std::is_same<T, ValidationType::NotEqualTo>()) {
const auto excl_maximum =
schema::Type{.value = schema::Type::ExclusiveMaximum{
.exclusiveMaximum = _v.value_,
.type = numeric_type_to_string(_type)}};
const auto excl_minimum =
schema::Type{.value = schema::Type::ExclusiveMinimum{
.exclusiveMinimum = _v.value_,
.type = numeric_type_to_string(_type)}};
return schema::Type{
.value = schema::Type::AnyOf{.anyOf = {excl_maximum, excl_minimum}}};
} else {
static_assert(rfl::always_false_v<T>, "Not all cases were covered.");
}
};
return rfl::visit(handle_variant, _validation_type.variant_);
}
schema::Type type_to_json_schema_type(const parsing::schema::Type& _type) {
auto handle_variant = [](const auto& _t) -> schema::Type {
using T = std::remove_cvref_t<decltype(_t)>;
using Type = parsing::schema::Type;
if constexpr (std::is_same<T, Type::Boolean>()) {
return schema::Type{.value = schema::Type::Boolean{}};
} else if constexpr (std::is_same<T, Type::Int32>() ||
std::is_same<T, Type::Int64>() ||
std::is_same<T, Type::UInt32>() ||
std::is_same<T, Type::UInt64>() ||
std::is_same<T, Type::Integer>()) {
return schema::Type{.value = schema::Type::Integer{}};
} else if constexpr (std::is_same<T, Type::Float>() ||
std::is_same<T, Type::Double>()) {
return schema::Type{.value = schema::Type::Number{}};
} else if constexpr (std::is_same<T, Type::String>()) {
return schema::Type{.value = schema::Type::String{}};
} else if constexpr (std::is_same<T, Type::AnyOf>()) {
auto any_of = std::vector<schema::Type>();
for (const auto& t : _t.types_) {
any_of.emplace_back(type_to_json_schema_type(t));
}
return schema::Type{.value = schema::Type::AnyOf{.anyOf = any_of}};
} else if constexpr (std::is_same<T, Type::Description>()) {
auto res = type_to_json_schema_type(*_t.type_);
const auto update_prediction = [&](auto _v) -> schema::Type {
_v.description = _t.description_;
return schema::Type{_v};
};
return rfl::visit(update_prediction, res.value);
} else if constexpr (std::is_same<T, Type::FixedSizeTypedArray>()) {
return schema::Type{.value = schema::Type::FixedSizeTypedArray{
.items = Ref<schema::Type>::make(
type_to_json_schema_type(*_t.type_)),
.minItems = _t.size_,
.maxItems = _t.size_}};
} else if constexpr (std::is_same<T, Type::Literal>()) {
return schema::Type{.value =
schema::Type::StringEnum{.values = _t.values_}};
} else if constexpr (std::is_same<T, Type::Object>()) {
auto properties = std::map<std::string, schema::Type>();
auto required = std::vector<std::string>();
for (const auto& [k, v] : _t.types_) {
properties[k] = type_to_json_schema_type(v);
if (!is_optional(v)) {
required.push_back(k);
}
}
auto additional_properties =
_t.additional_properties_
? std::make_shared<schema::Type>(
type_to_json_schema_type(*_t.additional_properties_))
: std::shared_ptr<schema::Type>();
return schema::Type{.value = schema::Type::Object{
.properties = properties,
.required = required,
.additionalProperties = additional_properties}};
} else if constexpr (std::is_same<T, Type::Optional>()) {
return schema::Type{.value = schema::Type::AnyOf{
.anyOf = {type_to_json_schema_type(*_t.type_),
schema::Type{schema::Type::Null{}}}}};
} else if constexpr (std::is_same<T, Type::Reference>()) {
return schema::Type{
.value = schema::Type::Reference{.ref = "#/definitions/" + _t.name_}};
} else if constexpr (std::is_same<T, Type::StringMap>()) {
return schema::Type{.value = schema::Type::StringMap{
.additionalProperties = Ref<schema::Type>::make(
type_to_json_schema_type(*_t.value_type_))}};
} else if constexpr (std::is_same<T, Type::Tuple>()) {
auto items = std::vector<schema::Type>();
for (const auto& t : _t.types_) {
items.emplace_back(type_to_json_schema_type(t));
}
return schema::Type{.value = schema::Type::Tuple{.prefixItems = items}};
} else if constexpr (std::is_same<T, Type::TypedArray>()) {
return schema::Type{.value = schema::Type::TypedArray{
.items = Ref<schema::Type>::make(
type_to_json_schema_type(*_t.type_))}};
} else if constexpr (std::is_same<T, Type::Validated>()) {
return handle_validation_type(*_t.type_, _t.validation_);
} else {
static_assert(rfl::always_false_v<T>, "Not all cases were covered.");
}
};
return rfl::visit(handle_variant, _type.variant_);
}
std::string to_schema_internal_schema(
const parsing::schema::Definition& internal_schema,
const yyjson_write_flag _flag) {
auto definitions = std::map<std::string, schema::Type>();
for (const auto& [k, v] : internal_schema.definitions_) {
definitions[k] = type_to_json_schema_type(v);
}
using JSONSchemaType =
typename TypeHelper<schema::Type::ReflectionType>::JSONSchemaType;
const auto to_schema = [&](auto&& _root) -> JSONSchemaType {
using U = std::decay_t<decltype(_root)>;
return schema::JSONSchema<U>{.root = std::move(_root),
.definitions = definitions};
};
auto root = type_to_json_schema_type(internal_schema.root_);
const auto json_schema = rfl::visit(to_schema, std::move(root.value));
return write(json_schema, _flag);
}
} // namespace rfl::json
+50
View File
@@ -0,0 +1,50 @@
#include "rfl/msgpack/Reader.hpp"
namespace rfl::msgpack {
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
const size_t _idx, const InputArrayType _arr) const noexcept {
if (_idx >= _arr.size) {
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
}
return _arr.ptr[_idx];
}
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
const std::string& _name, const InputObjectType& _obj) const noexcept {
for (uint32_t i = 0; i < _obj.size; ++i) {
const auto& key = _obj.ptr[i].key;
if (key.type != MSGPACK_OBJECT_STR) {
return Error("Key in element " + std::to_string(i) +
" was not a string.");
}
const auto current_name =
std::string_view(key.via.str.ptr, key.via.str.size);
if (_name == current_name) {
return _obj.ptr[i].val;
}
}
return Error("No field named '" + _name + "' was found.");
}
bool Reader::is_empty(const InputVarType& _var) const noexcept {
return _var.type == MSGPACK_OBJECT_NIL;
}
rfl::Result<Reader::InputArrayType> Reader::to_array(
const InputVarType& _var) const noexcept {
if (_var.type != MSGPACK_OBJECT_ARRAY) {
return Error("Could not cast to an array.");
}
return _var.via.array;
}
rfl::Result<Reader::InputObjectType> Reader::to_object(
const InputVarType& _var) const noexcept {
if (_var.type != MSGPACK_OBJECT_MAP) {
return Error("Could not cast to a map.");
}
return _var.via.map;
}
} // namespace rfl::msgpack
+78
View File
@@ -0,0 +1,78 @@
#include "rfl/msgpack/Writer.hpp"
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 {
return new_array(_size);
}
Writer::OutputObjectType Writer::object_as_root(
const size_t _size) const noexcept {
return new_object(_size);
}
Writer::OutputVarType Writer::null_as_root() const noexcept {
msgpack_pack_nil(pk_);
return OutputVarType{};
}
Writer::OutputArrayType Writer::add_array_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
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());
return new_array(_size);
}
Writer::OutputObjectType Writer::add_object_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
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());
return new_object(_size);
}
Writer::OutputVarType Writer::add_null_to_array(
OutputArrayType* _parent) const noexcept {
msgpack_pack_nil(pk_);
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_);
return OutputVarType{};
}
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);
return OutputArrayType{};
}
Writer::OutputObjectType Writer::new_object(const size_t _size) const noexcept {
msgpack_pack_map(pk_, _size);
return OutputObjectType{};
}
} // namespace rfl::msgpack
+40
View File
@@ -0,0 +1,40 @@
/*
MIT License
Copyright (c) 2023-2024 Code17 GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "rfl/parsing/schema/Type.hpp"
namespace rfl::parsing::schema {
/// Requires a lot of template instantiation, so we do not want this to be
/// inlined.
Type::Type() : variant_() {}
Type::Type(const VariantType& _variant) : variant_(_variant) {}
Type::~Type() = default;
} // namespace rfl::parsing::schema
+44
View File
@@ -0,0 +1,44 @@
#include "rfl/toml/Reader.hpp"
namespace rfl::toml {
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
const size_t _idx, const InputArrayType _arr) const noexcept {
if (_idx >= _arr->size()) {
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
}
return _arr->get(_idx);
}
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
const std::string& _name, const InputObjectType& _obj) const noexcept {
auto var = (*_obj)[_name];
if (!var) {
return rfl::Error("Object contains no field named '" + _name + "'.");
}
return var.node();
}
bool Reader::is_empty(const InputVarType& _var) const noexcept {
return !_var && true;
}
rfl::Result<Reader::InputArrayType> Reader::to_array(
const InputVarType& _var) const noexcept {
const auto ptr = _var->as_array();
if (!ptr) {
return rfl::Error("Could not cast to an array!");
}
return ptr;
}
rfl::Result<Reader::InputObjectType> Reader::to_object(
const InputVarType& _var) const noexcept {
const auto ptr = _var->as_table();
if (!ptr) {
return rfl::Error("Could not cast to a table!");
}
return ptr;
}
} // namespace rfl::toml
+62
View File
@@ -0,0 +1,62 @@
#include "rfl/toml/Writer.hpp"
namespace rfl::toml {
Writer::Writer(::toml::table* _root) : root_(_root) {}
Writer::~Writer() = default;
Writer::OutputObjectType Writer::object_as_root(
const size_t _size) const noexcept {
return OutputObjectType{root_};
}
Writer::OutputVarType Writer::null_as_root() const noexcept {
return OutputVarType{};
}
Writer::OutputArrayType Writer::add_array_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
const auto i = _parent->val_->size();
_parent->val_->push_back(::toml::array());
return OutputArrayType{_parent->val_->at(i).as_array()};
}
Writer::OutputArrayType Writer::add_array_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
_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 auto i = _parent->val_->size();
_parent->val_->push_back(::toml::table());
return OutputObjectType{_parent->val_->at(i).as_table()};
}
Writer::OutputObjectType Writer::add_object_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
_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 {
_parent->val_->push_back(std::string(""));
return OutputVarType{};
}
Writer::OutputVarType Writer::add_null_to_object(
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
_parent->val_->emplace(_name, ::toml::value(std::string("")));
return OutputVarType{};
}
void Writer::end_array(OutputArrayType* _arr) const noexcept {}
void Writer::end_object(OutputObjectType* _obj) const noexcept {}
} // namespace rfl::toml
+87
View File
@@ -0,0 +1,87 @@
/*
MIT License
Copyright (c) 2023-2024 Code17 GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "rfl/xml/Reader.hpp"
namespace rfl::xml {
/// XML-only helper function. This is needed because XML distinguishes between
/// nodes and attributes.
static rfl::Result<pugi::xml_node> cast_as_node(
const std::variant<pugi::xml_node, pugi::xml_attribute>&
_node_or_attribute) {
const auto cast = [](const auto& _n) -> Result<pugi::xml_node> {
using Type = std::remove_cvref_t<decltype(_n)>;
if constexpr (std::is_same<Type, pugi::xml_node>()) {
return _n;
} else {
return Error("Field '" + std::string(_n.name()) + "' is an attribute.");
}
};
return std::visit(cast, _node_or_attribute);
}
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
const size_t _idx, const InputArrayType& _arr) const noexcept {
const auto name = _arr.node_.name();
size_t i = 0;
for (auto node = _arr.node_; node; node = node.next_sibling(name)) {
if (i == _idx) {
return InputVarType(node);
}
}
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
}
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
const std::string& _name, const InputObjectType _obj) const noexcept {
const auto node = _obj.node_.child(_name.c_str());
if (!node) {
return rfl::Error("Object contains no field named '" + _name + "'.");
}
return InputVarType(node);
}
bool Reader::is_empty(const InputVarType _var) const noexcept {
const auto wrap = [](const auto& _node) { return !_node; };
return std::visit(cast_as_node, _var.node_or_attribute_)
.transform(wrap)
.value_or(false);
}
rfl::Result<Reader::InputArrayType> Reader::to_array(
const InputVarType _var) const noexcept {
const auto wrap = [](const auto& _node) { return InputArrayType(_node); };
return std::visit(cast_as_node, _var.node_or_attribute_).transform(wrap);
}
rfl::Result<Reader::InputObjectType> Reader::to_object(
const InputVarType _var) const noexcept {
const auto wrap = [](const auto& _node) { return InputObjectType(_node); };
return std::visit(cast_as_node, _var.node_or_attribute_).transform(wrap);
}
} // namespace rfl::xml
+144
View File
@@ -0,0 +1,144 @@
/*
MIT License
Copyright (c) 2023-2024 Code17 GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "rfl/xml/Writer.hpp"
#include <pugixml.hpp>
namespace rfl::xml {
static constexpr const char* XML_CONTENT = "xml_content";
Writer::Writer(const Ref<pugi::xml_node>& _root, const std::string& _root_name)
: root_(_root), root_name_(_root_name) {}
Writer::~Writer() = default;
Writer::OutputArrayType Writer::array_as_root(
const size_t _size) const noexcept {
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 {
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 {
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 {
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());
return OutputVarType(node_child);
}
Writer::OutputArrayType Writer::add_array_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
return *_parent;
}
Writer::OutputArrayType Writer::add_array_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
return OutputArrayType(_name, _parent->node_);
}
Writer::OutputVarType Writer::add_value_to_array_impl(
const std::string& _str, OutputArrayType* _parent) const noexcept {
auto node_child = Ref<pugi::xml_node>::make(
_parent->node_->append_child(_parent->name_.data()));
node_child->append_child(pugi::node_pcdata).set_value(_str.c_str());
return OutputVarType(node_child);
}
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 {
if (_is_attribute) {
_parent->node_->append_attribute(_name.data()) = _str.c_str();
return OutputVarType(_parent->node_);
} else if (_name == XML_CONTENT) {
_parent->node_->append_child(pugi::node_pcdata).set_value(_str.c_str());
return OutputVarType(_parent->node_);
} else {
auto node_child =
Ref<pugi::xml_node>::make(_parent->node_->append_child(_name.data()));
node_child->append_child(pugi::node_pcdata).set_value(_str.c_str());
return OutputVarType(node_child);
}
}
Writer::OutputObjectType Writer::add_object_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
auto node_child = Ref<pugi::xml_node>::make(
_parent->node_->append_child(_parent->name_.data()));
return OutputObjectType(node_child);
}
Writer::OutputObjectType Writer::add_object_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
auto node_child =
Ref<pugi::xml_node>::make(_parent->node_->append_child(_name.data()));
return OutputObjectType(node_child);
}
Writer::OutputVarType Writer::add_null_to_array(
OutputArrayType* _parent) const noexcept {
auto node_child = Ref<pugi::xml_node>::make(
_parent->node_->append_child(_parent->name_.data()));
return OutputVarType(node_child);
}
Writer::OutputVarType Writer::add_null_to_object(
const std::string_view& _name, OutputObjectType* _parent,
const bool _is_attribute) const noexcept {
if (_is_attribute) {
return OutputVarType(_parent->node_);
} else if (_name == XML_CONTENT) {
return OutputVarType(_parent->node_);
} else {
auto node_child =
Ref<pugi::xml_node>::make(_parent->node_->append_child(_name.data()));
return OutputVarType(node_child);
}
}
void Writer::end_array(OutputArrayType* _arr) const noexcept {}
void Writer::end_object(OutputObjectType* _obj) const noexcept {}
} // namespace rfl::xml
+42
View File
@@ -0,0 +1,42 @@
#include "rfl/yaml/Reader.hpp"
namespace rfl::yaml {
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
const size_t _idx, const InputArrayType& _arr) const noexcept {
if (_idx >= _arr.node_.size()) {
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
}
return InputVarType(_arr.node_[_idx]);
}
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
const std::string& _name, const InputObjectType& _obj) const noexcept {
auto var = InputVarType(_obj.node_[_name]);
if (!var.node_) {
return rfl::Error("Object contains no field named '" + _name + "'.");
}
return var;
}
bool Reader::is_empty(const InputVarType& _var) const noexcept {
return !_var.node_ && true;
}
rfl::Result<Reader::InputArrayType> Reader::to_array(
const InputVarType& _var) const noexcept {
if (!_var.node_.IsSequence()) {
return rfl::Error("Could not cast to sequence!");
}
return InputArrayType(_var.node_);
}
rfl::Result<Reader::InputObjectType> Reader::to_object(
const InputVarType& _var) const noexcept {
if (!_var.node_.IsMap()) {
return rfl::Error("Could not cast to map!");
}
return InputObjectType(_var.node_);
}
} // namespace rfl::yaml
+85
View File
@@ -0,0 +1,85 @@
#include "rfl/yaml/Writer.hpp"
namespace rfl::yaml {
Writer::Writer(const Ref<YAML::Emitter>& _out) : out_(_out) {}
Writer::~Writer() = default;
Writer::OutputArrayType Writer::array_as_root(
const size_t _size) const noexcept {
return new_array();
}
Writer::OutputObjectType Writer::object_as_root(
const size_t _size) const noexcept {
return new_object();
}
Writer::OutputVarType Writer::null_as_root() const noexcept {
return insert_value(YAML::Null);
}
Writer::OutputArrayType Writer::add_array_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
return new_array();
}
Writer::OutputArrayType Writer::add_array_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
return new_array(_name);
}
Writer::OutputObjectType Writer::add_object_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
return new_object();
}
Writer::OutputObjectType Writer::add_object_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
return new_object(_name);
}
Writer::OutputVarType Writer::add_null_to_array(
OutputArrayType* _parent) const noexcept {
return insert_value(YAML::Null);
}
Writer::OutputVarType Writer::add_null_to_object(
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
return insert_value(_name, YAML::Null);
}
void Writer::end_array(OutputArrayType* _arr) const noexcept {
(*out_) << YAML::EndSeq;
}
void Writer::end_object(OutputObjectType* _obj) const noexcept {
(*out_) << YAML::EndMap;
}
Writer::OutputArrayType Writer::new_array(
const std::string_view& _name) const noexcept {
(*out_) << YAML::Key << _name.data() << YAML::Value << YAML::BeginSeq;
return OutputArrayType{};
}
Writer::OutputArrayType Writer::new_array() const noexcept {
(*out_) << YAML::BeginSeq;
return OutputArrayType{};
}
Writer::OutputObjectType Writer::new_object(
const std::string_view& _name) const noexcept {
(*out_) << YAML::Key << _name.data() << YAML::Value << YAML::BeginMap;
return OutputObjectType{};
}
Writer::OutputObjectType Writer::new_object() const noexcept {
(*out_) << YAML::BeginMap;
return OutputObjectType{};
}
} // namespace rfl::yaml