From 8027c21e8acbf9b191ccbc63b8f67da12b374d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dr=2E=20Patrick=20Urbanke=20=28=E5=8A=89=E8=87=AA=E6=88=90?= =?UTF-8?q?=29?= Date: Sat, 15 Mar 2025 13:23:23 +0800 Subject: [PATCH] Use toml11 instead of tomlplusplus (#381) --- CMakeLists.txt | 12 ++---- README.md | 2 +- conanfile.py | 2 +- docs/supported_formats/toml.md | 2 +- include/rfl/toml/Reader.hpp | 77 ++++++++++++++++------------------ include/rfl/toml/Writer.hpp | 8 +++- include/rfl/toml/read.hpp | 14 +++++-- include/rfl/toml/write.hpp | 28 ++++--------- src/rfl/toml/Writer.cpp | 10 ++--- vcpkg.json | 4 +- 10 files changed, 75 insertions(+), 84 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cf832c76..282b1e2e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -194,16 +194,10 @@ if (REFLECTCPP_TOML) list(APPEND REFLECT_CPP_SOURCES src/reflectcpp_toml.cpp ) - if(REFLECTCPP_USE_VCPKG) - find_package(PkgConfig 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) + if (NOT TARGET toml11) + find_package(toml11 CONFIG REQUIRED) endif() + target_link_libraries(reflectcpp PUBLIC toml11::toml11) endif() if (REFLECTCPP_UBJSON) diff --git a/README.md b/README.md index d51ecf9a..65ac677e 100644 --- a/README.md +++ b/README.md @@ -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 | | 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 | -| 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 | | 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 | diff --git a/conanfile.py b/conanfile.py index fd864362..3cf5e96a 100644 --- a/conanfile.py +++ b/conanfile.py @@ -80,7 +80,7 @@ class ReflectCppConan(ConanFile): if self.options.with_msgpack: self.requires("msgpack-c/6.0.0", transitive_headers=True) 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: self.requires("pugixml/1.14", transitive_headers=True) if self.options.with_yaml: diff --git a/docs/supported_formats/toml.md b/docs/supported_formats/toml.md index 1abd7e25..f7294116 100644 --- a/docs/supported_formats/toml.md +++ b/docs/supported_formats/toml.md @@ -1,6 +1,6 @@ # TOML -For TOML support, you must also include the header `` and include the [toml++](https://github.com/marzer/tomlplusplus) library. +For TOML support, you must also include the header `` 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 should be an appropriate feature (vcpkg) or option (Conan) that will abstract this away for you. diff --git a/include/rfl/toml/Reader.hpp b/include/rfl/toml/Reader.hpp index 2dda297d..90d7ce27 100644 --- a/include/rfl/toml/Reader.hpp +++ b/include/rfl/toml/Reader.hpp @@ -9,20 +9,24 @@ #include #include #include -#include #include #include #include +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Warray-bounds" +#include +#pragma GCC diagnostic pop + #include "../Result.hpp" #include "../always_false.hpp" namespace rfl::toml { struct Reader { - using InputArrayType = ::toml::array*; - using InputObjectType = ::toml::table*; - using InputVarType = ::toml::node*; + using InputArrayType = const ::toml::array*; + using InputObjectType = const ::toml::table*; + using InputVarType = const ::toml::value*; template static constexpr bool has_custom_constructor = @@ -33,66 +37,60 @@ struct Reader { if (_idx >= _arr->size()) { return error("Index " + std::to_string(_idx) + " of of bounds."); } - return _arr->get(_idx); + return &(*_arr)[_idx]; } rfl::Result get_field_from_object( - const std::string& _name, const InputObjectType& _obj) const noexcept { - auto var = (*_obj)[_name]; - if (!var) { + const std::string& _name, const InputObjectType _obj) const noexcept { + const auto it = _obj->find(_name); + if (it == _obj->end()) { return error("Object contains no field named '" + _name + "'."); } - return var.node(); + return &it->second; } - bool is_empty(const InputVarType& _var) const noexcept { - return !_var && true; + bool is_empty(const InputVarType _var) const noexcept { + return _var->is_empty(); } template - rfl::Result to_basic_type(const InputVarType& _var) const noexcept { + rfl::Result to_basic_type(const InputVarType _var) const noexcept { if constexpr (std::is_same, std::string>()) { - const auto ptr = _var->as(); - if (!ptr) { - return error("Could not cast the node to std::string!"); + if (!_var->is_string()) { + return error("Could not cast to std::string!"); } - return **ptr; + return _var->as_string(); } else if constexpr (std::is_same, bool>()) { - const auto ptr = _var->as(); - if (!ptr) { - return error("Could not cast the node to bool!"); + if (!_var->is_boolean()) { + return error("Could not cast to bool!"); } - return **ptr; + return _var->as_boolean(); } else if constexpr (std::is_floating_point>()) { - const auto ptr = _var->as(); - if (!ptr) { - return error("Could not cast the node to double!"); + if (!_var->is_floating()) { + return error("Could not cast to double!"); } - return static_cast>(**ptr); + return static_cast>(_var->as_floating()); } else if constexpr (std::is_integral>()) { - const auto ptr = _var->as(); - if (!ptr) { - return error("Could not cast the node to int64_t!"); + if (!_var->is_integer()) { + return error("Could not cast to int64_t!"); } - return static_cast>(**ptr); + return static_cast>(_var->as_integer()); } else { static_assert(rfl::always_false_v, "Unsupported type."); } } - rfl::Result to_array( - const InputVarType& _var) const noexcept { - const auto ptr = _var->as_array(); - if (!ptr) { + rfl::Result to_array(const InputVarType _var) const noexcept { + if (!_var->is_array()) { return error("Could not cast to an array!"); } - return ptr; + return &_var->as_array(); } template std::optional read_array(const ArrayReader& _array_reader, - const InputArrayType& _arr) const noexcept { - for (auto& node : *_arr) { + const InputArrayType _arr) const noexcept { + for (const auto& node : *_arr) { const auto err = _array_reader.read(&node); if (err) { return err; @@ -104,19 +102,18 @@ struct Reader { template std::optional read_object(const ObjectReader& _object_reader, InputObjectType _obj) const noexcept { - for (auto& [k, v] : *_obj) { + for (const auto& [k, v] : *_obj) { _object_reader.read(std::string_view(k), &v); } return std::nullopt; } rfl::Result to_object( - const InputVarType& _var) const noexcept { - const auto ptr = _var->as_table(); - if (!ptr) { + const InputVarType _var) const noexcept { + if (!_var->is_table()) { return error("Could not cast to a table!"); } - return ptr; + return &_var->as_table(); } template diff --git a/include/rfl/toml/Writer.hpp b/include/rfl/toml/Writer.hpp index e53c96b4..cc332537 100644 --- a/include/rfl/toml/Writer.hpp +++ b/include/rfl/toml/Writer.hpp @@ -7,10 +7,14 @@ #include #include #include -#include #include #include +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Warray-bounds" +#include +#pragma GCC diagnostic pop + #include "../Ref.hpp" #include "../Result.hpp" #include "../always_false.hpp" @@ -76,7 +80,7 @@ class Writer { OutputVarType add_value_to_object(const std::string_view& _name, const T& _var, OutputObjectType* _parent) const noexcept { - _parent->val_->emplace(_name, ::toml::value(_var)); + (*_parent->val_)[std::string(_name)] = ::toml::value(_var); return OutputVarType{}; } diff --git a/include/rfl/toml/read.hpp b/include/rfl/toml/read.hpp index 3175f564..c3bca547 100644 --- a/include/rfl/toml/read.hpp +++ b/include/rfl/toml/read.hpp @@ -3,7 +3,11 @@ #include #include -#include + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Warray-bounds" +#include +#pragma GCC diagnostic pop #include "../Processors.hpp" #include "../internal/wrap_in_rfl_array_t.hpp" @@ -28,8 +32,12 @@ auto read(InputVarType _var) { /// Parses an object from TOML using reflection. template Result> read(const std::string& _toml_str) { - auto table = ::toml::parse(_toml_str); - return read(&table); + auto res = ::toml::try_parse_str(_toml_str); + if (res.is_ok()) { + return read(&res.unwrap()); + } else { + return error(::toml::format_error(res.unwrap_err().at(0))); + } } /// Parses an object from a stringstream. diff --git a/include/rfl/toml/write.hpp b/include/rfl/toml/write.hpp index 9d4f306f..c3141dfa 100644 --- a/include/rfl/toml/write.hpp +++ b/include/rfl/toml/write.hpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include "../Processors.hpp" @@ -13,22 +13,6 @@ namespace rfl::toml { -/// Writes a TOML into an ostream. -template -std::ostream& write(const auto& _obj, std::ostream& _stream) { - using T = std::remove_cvref_t; - using ParentType = parsing::Parent; - ::toml::table root; - auto w = Writer(&root); - using ProcessorsType = Processors; - static_assert(!ProcessorsType::no_field_names_, - "The NoFieldNames processor is not supported for BSON, XML, " - "TOML, or YAML."); - Parser::write(w, _obj, typename ParentType::Root{}); - _stream << root; - return _stream; -} - /// Returns a TOML string. template 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, " "TOML, or YAML."); Parser::write(w, _obj, typename ParentType::Root{}); - sstream << root; - return sstream.str(); + return ::toml::format(::toml::value(root)); +} + +/// Writes a TOML into an ostream. +template +std::ostream& write(const auto& _obj, std::ostream& _stream) { + _stream << write(_obj); + return _stream; } } // namespace rfl::toml diff --git a/src/rfl/toml/Writer.cpp b/src/rfl/toml/Writer.cpp index 22e19032..49dadf33 100644 --- a/src/rfl/toml/Writer.cpp +++ b/src/rfl/toml/Writer.cpp @@ -17,30 +17,28 @@ Writer::OutputVarType Writer::null_as_root() const noexcept { 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()}; + return OutputArrayType{&_parent->val_->back().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()}; + return OutputArrayType{&(*_parent->val_)[std::string(_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()}; + return OutputObjectType{&_parent->val_->back().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()}; + return OutputObjectType{&(*_parent->val_)[std::string(_name)].as_table()}; } Writer::OutputVarType Writer::add_null_to_array( diff --git a/vcpkg.json b/vcpkg.json index b15e18c1..4725f0b3 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -48,8 +48,8 @@ "version>=": "3.9.3" }, { - "name": "tomlplusplus", - "version>=": "3.4.0" + "name": "toml11", + "version>=": "4.2.0" }, { "name": "yaml-cpp",