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:
2025-12-06 10:55:46 -05:00
parent 2b5abeae58
commit ec13264050
365 changed files with 63946 additions and 357 deletions

View File

@@ -0,0 +1,15 @@
#ifndef RFL_TOML_PARSER_HPP_
#define RFL_TOML_PARSER_HPP_
#include "../parsing/Parser.hpp"
#include "Reader.hpp"
#include "Writer.hpp"
namespace rfl::toml {
template <class T, class ProcessorsType>
using Parser = parsing::Parser<Reader, Writer, T, ProcessorsType>;
} // namespace rfl::toml
#endif

View File

@@ -0,0 +1,135 @@
#ifndef RFL_TOML_READER_HPP_
#define RFL_TOML_READER_HPP_
#include <array>
#include <exception>
#include <map>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <toml++/toml.hpp>
#include <type_traits>
#include <unordered_map>
#include <vector>
#include "../Result.hpp"
#include "../always_false.hpp"
namespace rfl::toml {
struct Reader {
using InputArrayType = ::toml::array*;
using InputObjectType = ::toml::table*;
using InputVarType = ::toml::node*;
template <class T>
static constexpr bool has_custom_constructor =
(requires(InputVarType var) { T::from_toml_obj(var); });
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->get(_idx);
}
rfl::Result<InputVarType> get_field_from_object(
const std::string& _name, const InputObjectType& _obj) const noexcept {
auto var = (*_obj)[_name];
if (!var) {
return error("Object contains no field named '" + _name + "'.");
}
return var.node();
}
bool is_empty(const InputVarType& _var) const noexcept {
return !_var && true;
}
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>()) {
const auto ptr = _var->as<std::string>();
if (!ptr) {
return error("Could not cast the node to std::string!");
}
return **ptr;
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
const auto ptr = _var->as<bool>();
if (!ptr) {
return error("Could not cast the node to bool!");
}
return **ptr;
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
const auto ptr = _var->as<double>();
if (!ptr) {
return error("Could not cast the node to double!");
}
return static_cast<std::remove_cvref_t<T>>(**ptr);
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
const auto ptr = _var->as<int64_t>();
if (!ptr) {
return error("Could not cast the node to int64_t!");
}
return static_cast<std::remove_cvref_t<T>>(**ptr);
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
}
rfl::Result<InputArrayType> to_array(
const InputVarType& _var) const noexcept {
const auto ptr = _var->as_array();
if (!ptr) {
return error("Could not cast to an array!");
}
return ptr;
}
template <class ArrayReader>
std::optional<Error> read_array(const ArrayReader& _array_reader,
const InputArrayType& _arr) const noexcept {
for (auto& node : *_arr) {
const auto err = _array_reader.read(&node);
if (err) {
return err;
}
}
return std::nullopt;
}
template <class ObjectReader>
std::optional<Error> read_object(const ObjectReader& _object_reader,
InputObjectType _obj) const noexcept {
for (auto& [k, v] : *_obj) {
_object_reader.read(std::string_view(k), &v);
}
return std::nullopt;
}
rfl::Result<InputObjectType> to_object(
const InputVarType& _var) const noexcept {
const auto ptr = _var->as_table();
if (!ptr) {
return error("Could not cast to a table!");
}
return ptr;
}
template <class T>
rfl::Result<T> use_custom_constructor(
const InputVarType _var) const noexcept {
try {
return T::from_toml_obj(_var);
} catch (std::exception& e) {
return error(e.what());
}
}
};
} // namespace rfl::toml
#endif

View File

@@ -0,0 +1,96 @@
#ifndef RFL_TOML_WRITER_HPP_
#define RFL_TOML_WRITER_HPP_
#include <map>
#include <string>
#include <string_view>
#include <toml++/toml.hpp>
#include <type_traits>
#include <vector>
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
#include "../common.hpp"
namespace rfl::toml {
class RFL_API Writer {
public:
struct TOMLArray {
::toml::array* val_;
};
struct TOMLObject {
::toml::table* val_;
};
struct TOMLVar {};
using OutputArrayType = TOMLArray;
using OutputObjectType = TOMLObject;
using OutputVarType = TOMLVar;
Writer(::toml::table* _root);
~Writer();
template <class T>
OutputArrayType array_as_root(const T _size) const;
OutputObjectType object_as_root(const size_t _size) const;
OutputVarType null_as_root() const;
template <class T>
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;
OutputArrayType add_array_to_object(const std::string_view& _name,
const size_t _size,
OutputObjectType* _parent) const;
OutputObjectType add_object_to_array(const size_t _size,
OutputArrayType* _parent) const;
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 {
_parent->val_->push_back(::toml::value(_var));
return OutputVarType{};
}
template <class T>
OutputVarType add_value_to_object(const std::string_view& _name,
const T& _var,
OutputObjectType* _parent) const {
_parent->val_->emplace(_name, ::toml::value(_var));
return OutputVarType{};
}
OutputVarType add_null_to_array(OutputArrayType* _parent) const;
OutputVarType add_null_to_object(const std::string_view& _name,
OutputObjectType* _parent) const;
void end_array(OutputArrayType* _arr) const;
void end_object(OutputObjectType* _obj) const;
private:
::toml::table* root_;
};
} // namespace rfl::toml
#endif

View File

@@ -0,0 +1,20 @@
#ifndef RFL_TOML_LOAD_HPP_
#define RFL_TOML_LOAD_HPP_
#include "../Result.hpp"
#include "../io/load_string.hpp"
#include "read.hpp"
namespace rfl::toml {
template <class T, class... Ps>
Result<T> load(const std::string& _fname) {
const auto read_string = [](const auto& _str) {
return read<T, Ps...>(_str);
};
return rfl::io::load_string(_fname).and_then(read_string);
}
} // namespace rfl::toml
#endif

View File

@@ -0,0 +1,59 @@
#ifndef RFL_TOML_READ_HPP_
#define RFL_TOML_READ_HPP_
#include <istream>
#include <string>
#include <string_view>
#include <toml++/toml.hpp>
#include "../Processors.hpp"
#include "../internal/wrap_in_rfl_array_t.hpp"
#include "Parser.hpp"
#include "Reader.hpp"
namespace rfl::toml {
using InputVarType = typename Reader::InputVarType;
/// Parses an object from a TOML var.
template <class T, class... Ps>
auto read(InputVarType _var) {
const auto r = Reader();
using ProcessorsType = Processors<Ps...>;
static_assert(!ProcessorsType::no_field_names_,
"The NoFieldNames processor is not supported for BSON, XML, "
"TOML, or YAML.");
return Parser<T, ProcessorsType>::read(r, _var);
}
/// Reads a TOML string.
template <class T, class... Ps>
Result<internal::wrap_in_rfl_array_t<T>> read(
const std::string_view _toml_str) noexcept {
#if TOML_EXCEPTIONS
try {
auto table = ::toml::parse(_toml_str);
return read<T, Ps...>(&table);
} catch (const std::exception& e) {
return error(e.what());
}
#else
auto result = ::toml::parse(_toml_str);
if (!result) {
return error(std::string(result.error().description()));
}
return read<T, Ps...>(&result.table());
#endif
}
/// Parses an object from a stringstream.
template <class T, class... Ps>
auto read(std::istream& _stream) {
const auto toml_str = std::string(std::istreambuf_iterator<char>(_stream),
std::istreambuf_iterator<char>());
return read<T, Ps...>(toml_str);
}
} // namespace rfl::toml
#endif

View File

@@ -0,0 +1,26 @@
#ifndef RFL_TOML_SAVE_HPP_
#define RFL_TOML_SAVE_HPP_
#include <fstream>
#include <iostream>
#include <string>
#include "../Result.hpp"
#include "../io/save_string.hpp"
#include "write.hpp"
namespace rfl {
namespace toml {
template <class... Ps>
Result<Nothing> save(const std::string& _fname, const auto& _obj) {
const auto write_func = [](const auto& _obj, auto& _stream) -> auto& {
return write<Ps...>(_obj, _stream);
};
return rfl::io::save_string(_fname, _obj, write_func);
}
} // namespace toml
} // namespace rfl
#endif

View File

@@ -0,0 +1,51 @@
#ifndef RFL_TOML_WRITE_HPP_
#define RFL_TOML_WRITE_HPP_
#include <ostream>
#include <sstream>
#include <string>
#include <toml++/toml.hpp>
#include <type_traits>
#include "../Processors.hpp"
#include "../parsing/Parent.hpp"
#include "Parser.hpp"
namespace rfl::toml {
/// Writes a TOML into an ostream.
template <class... Ps>
std::ostream& write(const auto& _obj, std::ostream& _stream) {
using T = std::remove_cvref_t<decltype(_obj)>;
using ParentType = parsing::Parent<Writer>;
::toml::table root;
auto w = Writer(&root);
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(w, _obj, typename ParentType::Root{});
_stream << root;
return _stream;
}
/// Returns a TOML string.
template <class... Ps>
std::string write(const auto& _obj) {
using T = std::remove_cvref_t<decltype(_obj)>;
using ParentType = parsing::Parent<Writer>;
std::stringstream sstream;
::toml::table root;
auto w = Writer(&root);
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(w, _obj, typename ParentType::Root{});
sstream << root;
return sstream.str();
}
} // namespace rfl::toml
#endif