Use toml11 instead of tomlplusplus (#381)

This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2025-03-15 13:23:23 +08:00
committed by GitHub
parent c118963de0
commit 8027c21e8a
10 changed files with 75 additions and 84 deletions
+3 -9
View File
@@ -194,16 +194,10 @@ if (REFLECTCPP_TOML)
list(APPEND REFLECT_CPP_SOURCES list(APPEND REFLECT_CPP_SOURCES
src/reflectcpp_toml.cpp src/reflectcpp_toml.cpp
) )
if(REFLECTCPP_USE_VCPKG) if (NOT TARGET toml11)
find_package(PkgConfig REQUIRED) find_package(toml11 CONFIG REQUIRED)
pkg_check_modules(tomlplusplus REQUIRED IMPORTED_TARGET tomlplusplus)
target_link_libraries(reflectcpp PUBLIC PkgConfig::tomlplusplus)
else()
if (NOT TARGET tomlplusplus)
find_package(tomlplusplus)
endif()
target_link_libraries(reflectcpp PUBLIC tomlplusplus::tomlplusplus)
endif() endif()
target_link_libraries(reflectcpp PUBLIC toml11::toml11)
endif() endif()
if (REFLECTCPP_UBJSON) if (REFLECTCPP_UBJSON)
+1 -1
View File
@@ -69,7 +69,7 @@ The following table lists the serialization formats currently supported by refle
| CBOR | [jsoncons](https://github.com/danielaparker/jsoncons)| >= 0.176.0 | BSL 1.0 | JSON-like binary format | | CBOR | [jsoncons](https://github.com/danielaparker/jsoncons)| >= 0.176.0 | BSL 1.0 | JSON-like binary format |
| flexbuffers | [flatbuffers](https://github.com/google/flatbuffers) | >= 23.5.26 | Apache 2.0 | Schema-less version of flatbuffers, binary format | | flexbuffers | [flatbuffers](https://github.com/google/flatbuffers) | >= 23.5.26 | Apache 2.0 | Schema-less version of flatbuffers, binary format |
| msgpack | [msgpack-c](https://github.com/msgpack/msgpack-c) | >= 6.0.0 | BSL 1.0 | JSON-like binary format | | msgpack | [msgpack-c](https://github.com/msgpack/msgpack-c) | >= 6.0.0 | BSL 1.0 | JSON-like binary format |
| TOML | [toml++](https://github.com/marzer/tomlplusplus) | >= 3.4.0 | MIT | Textual format with an emphasis on readability | | TOML | [toml11](https://github.com/ToruNiina/toml11) | >= 4.2.0 | MIT | Textual format with an emphasis on readability |
| UBJSON | [jsoncons](https://github.com/danielaparker/jsoncons)| >= 0.176.0 | BSL 1.0 | JSON-like binary format | | UBJSON | [jsoncons](https://github.com/danielaparker/jsoncons)| >= 0.176.0 | BSL 1.0 | JSON-like binary format |
| XML | [pugixml](https://github.com/zeux/pugixml) | >= 1.14 | MIT | Textual format used in many legacy projects | | XML | [pugixml](https://github.com/zeux/pugixml) | >= 1.14 | MIT | Textual format used in many legacy projects |
| YAML | [yaml-cpp](https://github.com/jbeder/yaml-cpp) | >= 0.8.0 | MIT | Textual format with an emphasis on readability | | YAML | [yaml-cpp](https://github.com/jbeder/yaml-cpp) | >= 0.8.0 | MIT | Textual format with an emphasis on readability |
+1 -1
View File
@@ -80,7 +80,7 @@ class ReflectCppConan(ConanFile):
if self.options.with_msgpack: if self.options.with_msgpack:
self.requires("msgpack-c/6.0.0", transitive_headers=True) self.requires("msgpack-c/6.0.0", transitive_headers=True)
if self.options.with_toml: if self.options.with_toml:
self.requires("tomlplusplus/3.4.0", transitive_headers=True) self.requires("toml11/4.4.0", transitive_headers=True)
if self.options.with_xml: if self.options.with_xml:
self.requires("pugixml/1.14", transitive_headers=True) self.requires("pugixml/1.14", transitive_headers=True)
if self.options.with_yaml: if self.options.with_yaml:
+1 -1
View File
@@ -1,6 +1,6 @@
# TOML # TOML
For TOML support, you must also include the header `<rfl/toml.hpp>` and include the [toml++](https://github.com/marzer/tomlplusplus) library. For TOML support, you must also include the header `<rfl/toml.hpp>` and include the [toml11](https://github.com/ToruNiina/toml11) library.
Furthermore, when compiling reflect-cpp, you need to pass `-DREFLECTCPP_TOML=ON` to cmake. If you are using vcpkg or Conan, there Furthermore, when compiling reflect-cpp, you need to pass `-DREFLECTCPP_TOML=ON` to cmake. If you are using vcpkg or Conan, there
should be an appropriate feature (vcpkg) or option (Conan) that will abstract this away for you. should be an appropriate feature (vcpkg) or option (Conan) that will abstract this away for you.
+37 -40
View File
@@ -9,20 +9,24 @@
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <toml++/toml.hpp>
#include <type_traits> #include <type_traits>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
#include <toml.hpp>
#pragma GCC diagnostic pop
#include "../Result.hpp" #include "../Result.hpp"
#include "../always_false.hpp" #include "../always_false.hpp"
namespace rfl::toml { namespace rfl::toml {
struct Reader { struct Reader {
using InputArrayType = ::toml::array*; using InputArrayType = const ::toml::array*;
using InputObjectType = ::toml::table*; using InputObjectType = const ::toml::table*;
using InputVarType = ::toml::node*; using InputVarType = const ::toml::value*;
template <class T> template <class T>
static constexpr bool has_custom_constructor = static constexpr bool has_custom_constructor =
@@ -33,66 +37,60 @@ struct Reader {
if (_idx >= _arr->size()) { if (_idx >= _arr->size()) {
return error("Index " + std::to_string(_idx) + " of of bounds."); return error("Index " + std::to_string(_idx) + " of of bounds.");
} }
return _arr->get(_idx); return &(*_arr)[_idx];
} }
rfl::Result<InputVarType> get_field_from_object( rfl::Result<InputVarType> get_field_from_object(
const std::string& _name, const InputObjectType& _obj) const noexcept { const std::string& _name, const InputObjectType _obj) const noexcept {
auto var = (*_obj)[_name]; const auto it = _obj->find(_name);
if (!var) { if (it == _obj->end()) {
return error("Object contains no field named '" + _name + "'."); return error("Object contains no field named '" + _name + "'.");
} }
return var.node(); return &it->second;
} }
bool is_empty(const InputVarType& _var) const noexcept { bool is_empty(const InputVarType _var) const noexcept {
return !_var && true; return _var->is_empty();
} }
template <class T> template <class T>
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept { rfl::Result<T> to_basic_type(const InputVarType _var) const noexcept {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) { if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
const auto ptr = _var->as<std::string>(); if (!_var->is_string()) {
if (!ptr) { return error("Could not cast to std::string!");
return error("Could not cast the node to std::string!");
} }
return **ptr; return _var->as_string();
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) { } else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
const auto ptr = _var->as<bool>(); if (!_var->is_boolean()) {
if (!ptr) { return error("Could not cast to bool!");
return error("Could not cast the node to bool!");
} }
return **ptr; return _var->as_boolean();
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) { } else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
const auto ptr = _var->as<double>(); if (!_var->is_floating()) {
if (!ptr) { return error("Could not cast to double!");
return error("Could not cast the node to double!");
} }
return static_cast<std::remove_cvref_t<T>>(**ptr); return static_cast<std::remove_cvref_t<T>>(_var->as_floating());
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) { } else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
const auto ptr = _var->as<int64_t>(); if (!_var->is_integer()) {
if (!ptr) { return error("Could not cast to int64_t!");
return error("Could not cast the node to int64_t!");
} }
return static_cast<std::remove_cvref_t<T>>(**ptr); return static_cast<std::remove_cvref_t<T>>(_var->as_integer());
} else { } else {
static_assert(rfl::always_false_v<T>, "Unsupported type."); static_assert(rfl::always_false_v<T>, "Unsupported type.");
} }
} }
rfl::Result<InputArrayType> to_array( rfl::Result<InputArrayType> to_array(const InputVarType _var) const noexcept {
const InputVarType& _var) const noexcept { if (!_var->is_array()) {
const auto ptr = _var->as_array();
if (!ptr) {
return error("Could not cast to an array!"); return error("Could not cast to an array!");
} }
return ptr; return &_var->as_array();
} }
template <class ArrayReader> template <class ArrayReader>
std::optional<Error> read_array(const ArrayReader& _array_reader, std::optional<Error> read_array(const ArrayReader& _array_reader,
const InputArrayType& _arr) const noexcept { const InputArrayType _arr) const noexcept {
for (auto& node : *_arr) { for (const auto& node : *_arr) {
const auto err = _array_reader.read(&node); const auto err = _array_reader.read(&node);
if (err) { if (err) {
return err; return err;
@@ -104,19 +102,18 @@ struct Reader {
template <class ObjectReader> template <class ObjectReader>
std::optional<Error> read_object(const ObjectReader& _object_reader, std::optional<Error> read_object(const ObjectReader& _object_reader,
InputObjectType _obj) const noexcept { InputObjectType _obj) const noexcept {
for (auto& [k, v] : *_obj) { for (const auto& [k, v] : *_obj) {
_object_reader.read(std::string_view(k), &v); _object_reader.read(std::string_view(k), &v);
} }
return std::nullopt; return std::nullopt;
} }
rfl::Result<InputObjectType> to_object( rfl::Result<InputObjectType> to_object(
const InputVarType& _var) const noexcept { const InputVarType _var) const noexcept {
const auto ptr = _var->as_table(); if (!_var->is_table()) {
if (!ptr) {
return error("Could not cast to a table!"); return error("Could not cast to a table!");
} }
return ptr; return &_var->as_table();
} }
template <class T> template <class T>
+6 -2
View File
@@ -7,10 +7,14 @@
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <toml++/toml.hpp>
#include <type_traits> #include <type_traits>
#include <vector> #include <vector>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
#include <toml.hpp>
#pragma GCC diagnostic pop
#include "../Ref.hpp" #include "../Ref.hpp"
#include "../Result.hpp" #include "../Result.hpp"
#include "../always_false.hpp" #include "../always_false.hpp"
@@ -76,7 +80,7 @@ class Writer {
OutputVarType add_value_to_object(const std::string_view& _name, OutputVarType add_value_to_object(const std::string_view& _name,
const T& _var, const T& _var,
OutputObjectType* _parent) const noexcept { OutputObjectType* _parent) const noexcept {
_parent->val_->emplace(_name, ::toml::value(_var)); (*_parent->val_)[std::string(_name)] = ::toml::value(_var);
return OutputVarType{}; return OutputVarType{};
} }
+11 -3
View File
@@ -3,7 +3,11 @@
#include <istream> #include <istream>
#include <string> #include <string>
#include <toml++/toml.hpp>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
#include <toml.hpp>
#pragma GCC diagnostic pop
#include "../Processors.hpp" #include "../Processors.hpp"
#include "../internal/wrap_in_rfl_array_t.hpp" #include "../internal/wrap_in_rfl_array_t.hpp"
@@ -28,8 +32,12 @@ auto read(InputVarType _var) {
/// Parses an object from TOML using reflection. /// Parses an object from TOML using reflection.
template <class T, class... Ps> template <class T, class... Ps>
Result<internal::wrap_in_rfl_array_t<T>> read(const std::string& _toml_str) { Result<internal::wrap_in_rfl_array_t<T>> read(const std::string& _toml_str) {
auto table = ::toml::parse(_toml_str); auto res = ::toml::try_parse_str(_toml_str);
return read<T, Ps...>(&table); if (res.is_ok()) {
return read<T, Ps...>(&res.unwrap());
} else {
return error(::toml::format_error(res.unwrap_err().at(0)));
}
} }
/// Parses an object from a stringstream. /// Parses an object from a stringstream.
+9 -19
View File
@@ -4,7 +4,7 @@
#include <ostream> #include <ostream>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <toml++/toml.hpp> #include <toml.hpp>
#include <type_traits> #include <type_traits>
#include "../Processors.hpp" #include "../Processors.hpp"
@@ -13,22 +13,6 @@
namespace rfl::toml { 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. /// Returns a TOML string.
template <class... Ps> template <class... Ps>
std::string write(const auto& _obj) { std::string write(const auto& _obj) {
@@ -42,8 +26,14 @@ std::string write(const auto& _obj) {
"The NoFieldNames processor is not supported for BSON, XML, " "The NoFieldNames processor is not supported for BSON, XML, "
"TOML, or YAML."); "TOML, or YAML.");
Parser<T, ProcessorsType>::write(w, _obj, typename ParentType::Root{}); Parser<T, ProcessorsType>::write(w, _obj, typename ParentType::Root{});
sstream << root; return ::toml::format(::toml::value(root));
return sstream.str(); }
/// Writes a TOML into an ostream.
template <class... Ps>
std::ostream& write(const auto& _obj, std::ostream& _stream) {
_stream << write(_obj);
return _stream;
} }
} // namespace rfl::toml } // namespace rfl::toml
+4 -6
View File
@@ -17,30 +17,28 @@ Writer::OutputVarType Writer::null_as_root() const noexcept {
Writer::OutputArrayType Writer::add_array_to_array( Writer::OutputArrayType Writer::add_array_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept { const size_t _size, OutputArrayType* _parent) const noexcept {
const auto i = _parent->val_->size();
_parent->val_->push_back(::toml::array()); _parent->val_->push_back(::toml::array());
return OutputArrayType{_parent->val_->at(i).as_array()}; return OutputArrayType{&_parent->val_->back().as_array()};
} }
Writer::OutputArrayType Writer::add_array_to_object( Writer::OutputArrayType Writer::add_array_to_object(
const std::string_view& _name, const size_t _size, const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept { OutputObjectType* _parent) const noexcept {
_parent->val_->emplace(_name, ::toml::array()); _parent->val_->emplace(_name, ::toml::array());
return OutputArrayType{_parent->val_->at_path(_name).as_array()}; return OutputArrayType{&(*_parent->val_)[std::string(_name)].as_array()};
} }
Writer::OutputObjectType Writer::add_object_to_array( Writer::OutputObjectType Writer::add_object_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept { const size_t _size, OutputArrayType* _parent) const noexcept {
const auto i = _parent->val_->size();
_parent->val_->push_back(::toml::table()); _parent->val_->push_back(::toml::table());
return OutputObjectType{_parent->val_->at(i).as_table()}; return OutputObjectType{&_parent->val_->back().as_table()};
} }
Writer::OutputObjectType Writer::add_object_to_object( Writer::OutputObjectType Writer::add_object_to_object(
const std::string_view& _name, const size_t _size, const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept { OutputObjectType* _parent) const noexcept {
_parent->val_->emplace(_name, ::toml::table()); _parent->val_->emplace(_name, ::toml::table());
return OutputObjectType{_parent->val_->at_path(_name).as_table()}; return OutputObjectType{&(*_parent->val_)[std::string(_name)].as_table()};
} }
Writer::OutputVarType Writer::add_null_to_array( Writer::OutputVarType Writer::add_null_to_array(
+2 -2
View File
@@ -48,8 +48,8 @@
"version>=": "3.9.3" "version>=": "3.9.3"
}, },
{ {
"name": "tomlplusplus", "name": "toml11",
"version>=": "3.4.0" "version>=": "4.2.0"
}, },
{ {
"name": "yaml-cpp", "name": "yaml-cpp",