feat(reflect-cpp): Switched from glaze -> reflect cpp
A bug was discovered in glaze which prevented valid toml output. We have switched to toml++ and reflect-cpp. The interface has remained the same so this should not break any code
This commit is contained in:
17
build-config/reflect-cpp/include/rfl/generic/Parser.hpp
Normal file
17
build-config/reflect-cpp/include/rfl/generic/Parser.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef GENERIC_PARSER_HPP_
|
||||
#define GENERIC_PARSER_HPP_
|
||||
|
||||
#include "../parsing/Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
#include "Writer.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace generic {
|
||||
|
||||
template <class T, class ProcessorsType>
|
||||
using Parser = parsing::Parser<Reader, Writer, T, ProcessorsType>;
|
||||
|
||||
}
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
102
build-config/reflect-cpp/include/rfl/generic/Reader.hpp
Normal file
102
build-config/reflect-cpp/include/rfl/generic/Reader.hpp
Normal file
@@ -0,0 +1,102 @@
|
||||
#ifndef GENERIC_READER_HPP_
|
||||
#define GENERIC_READER_HPP_
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Generic.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl::generic {
|
||||
|
||||
struct Reader {
|
||||
using InputArrayType = Generic::Array;
|
||||
using InputObjectType = Generic::Object;
|
||||
using InputVarType = Generic;
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor = false;
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_array(
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept {
|
||||
if (_idx >= _arr.size()) {
|
||||
return error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
return _arr[_idx];
|
||||
}
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
return _obj.get(_name);
|
||||
}
|
||||
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return _var.is_null();
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
return _var.to_string();
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
return _var.to_bool();
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
return _var.to_double().transform(
|
||||
[](const auto& _v) { return static_cast<T>(_v); });
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
if constexpr (sizeof(T) > sizeof(int)) {
|
||||
return _var.to_int64().transform(
|
||||
[](const auto& _v) { return static_cast<T>(_v); });
|
||||
} else {
|
||||
return _var.to_int().transform(
|
||||
[](const auto& _v) { return static_cast<T>(_v); });
|
||||
}
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
|
||||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(const ArrayReader& _array_reader,
|
||||
const InputArrayType& _arr) const noexcept {
|
||||
for (const auto& v : _arr) {
|
||||
const auto err = _array_reader.read(InputVarType(v));
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
template <class ObjectReader>
|
||||
std::optional<Error> read_object(const ObjectReader& _object_reader,
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
for (const auto& [k, v] : _obj) {
|
||||
_object_reader.read(std::string_view(k), v);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
return _var.to_array();
|
||||
}
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
return _var.to_object();
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> use_custom_constructor(
|
||||
const InputVarType /*_var*/) const noexcept {
|
||||
return error("Not supported for generic types");
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace rfl::generic
|
||||
|
||||
#endif
|
||||
110
build-config/reflect-cpp/include/rfl/generic/Writer.hpp
Normal file
110
build-config/reflect-cpp/include/rfl/generic/Writer.hpp
Normal file
@@ -0,0 +1,110 @@
|
||||
#ifndef GENERIC_WRITER_HPP_
|
||||
#define GENERIC_WRITER_HPP_
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "../Generic.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../common.hpp"
|
||||
|
||||
namespace rfl::generic {
|
||||
|
||||
struct RFL_API Writer {
|
||||
struct OutputArray {
|
||||
Generic::Array* val_;
|
||||
};
|
||||
|
||||
struct OutputObject {
|
||||
Generic::Object* val_;
|
||||
};
|
||||
|
||||
using OutputArrayType = OutputArray;
|
||||
using OutputObjectType = OutputObject;
|
||||
using OutputVarType = Generic;
|
||||
|
||||
Writer() {}
|
||||
|
||||
~Writer() = default;
|
||||
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputVarType null_as_root() const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
root_ = to_generic(_var);
|
||||
return root_;
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputArrayType add_array_to_object(const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
const auto g = to_generic(_var);
|
||||
_parent->val_->push_back(g);
|
||||
return g;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(const std::string_view& _name,
|
||||
const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
const auto g = to_generic(_var);
|
||||
_parent->val_->insert(_name, g);
|
||||
return g;
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputVarType add_null_to_object(const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
void end_array(OutputArrayType*) const noexcept {}
|
||||
|
||||
void end_object(OutputObjectType*) const noexcept {}
|
||||
|
||||
OutputVarType& root() { return root_; }
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
OutputVarType to_generic(const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
return OutputVarType(_var);
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
return OutputVarType(_var);
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
return OutputVarType(static_cast<double>(_var));
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
return OutputVarType(static_cast<int64_t>(_var));
|
||||
} else {
|
||||
static_assert(always_false_v<T>, "Unsupported type");
|
||||
}
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
private:
|
||||
mutable OutputVarType root_;
|
||||
};
|
||||
|
||||
} // namespace rfl::generic
|
||||
|
||||
#endif
|
||||
22
build-config/reflect-cpp/include/rfl/generic/read.hpp
Normal file
22
build-config/reflect-cpp/include/rfl/generic/read.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef GENERIC_READ_HPP_
|
||||
#define GENERIC_READ_HPP_
|
||||
|
||||
|
||||
#include "../Generic.hpp"
|
||||
#include "../Processors.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace generic {
|
||||
|
||||
/// Parses an object from a generic type.
|
||||
template <class T, class... Ps>
|
||||
auto read(const Generic& _g) {
|
||||
const auto r = Reader();
|
||||
return Parser<T, Processors<Ps...>>::read(r, _g);
|
||||
}
|
||||
|
||||
} // namespace generic
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
24
build-config/reflect-cpp/include/rfl/generic/write.hpp
Normal file
24
build-config/reflect-cpp/include/rfl/generic/write.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef GENERIC_WRITE_HPP_
|
||||
#define GENERIC_WRITE_HPP_
|
||||
|
||||
#include "../Generic.hpp"
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace generic {
|
||||
|
||||
/// Writes an object to a generic.
|
||||
template <class... Ps>
|
||||
Generic write(const auto& _t) {
|
||||
using T = std::remove_cvref_t<decltype(_t)>;
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
auto w = Writer();
|
||||
Parser<T, Processors<Ps...>>::write(w, _t, typename ParentType::Root{});
|
||||
return w.root();
|
||||
}
|
||||
|
||||
} // namespace generic
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user