diff --git a/CMakeLists.txt b/CMakeLists.txt index 282b1e2e..78bd2b97 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -194,10 +194,10 @@ if (REFLECTCPP_TOML) list(APPEND REFLECT_CPP_SOURCES src/reflectcpp_toml.cpp ) - if (NOT TARGET toml11) - find_package(toml11 CONFIG REQUIRED) + if (NOT TARGET tomlplusplus) + find_package(tomlplusplus) endif() - target_link_libraries(reflectcpp PUBLIC toml11::toml11) + target_link_libraries(reflectcpp PUBLIC tomlplusplus::tomlplusplus) endif() if (REFLECTCPP_UBJSON) diff --git a/README.md b/README.md index 65ac677e..d51ecf9a 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 | [toml11](https://github.com/ToruNiina/toml11) | >= 4.2.0 | MIT | Textual format with an emphasis on readability | +| TOML | [toml++](https://github.com/marzer/tomlplusplus) | >= 3.4.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 3cf5e96a..fd864362 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("toml11/4.4.0", transitive_headers=True) + self.requires("tomlplusplus/3.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 f7294116..1abd7e25 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 [toml11](https://github.com/ToruNiina/toml11) library. +For TOML support, you must also include the header `` and include the [toml++](https://github.com/marzer/tomlplusplus) 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 a586a3a8..2dda297d 100644 --- a/include/rfl/toml/Reader.hpp +++ b/include/rfl/toml/Reader.hpp @@ -9,24 +9,20 @@ #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 = const ::toml::array*; - using InputObjectType = const ::toml::table*; - using InputVarType = const ::toml::value*; + using InputArrayType = ::toml::array*; + using InputObjectType = ::toml::table*; + using InputVarType = ::toml::node*; template static constexpr bool has_custom_constructor = @@ -37,64 +33,66 @@ struct Reader { if (_idx >= _arr->size()) { return error("Index " + std::to_string(_idx) + " of of bounds."); } - return &(*_arr)[_idx]; + return _arr->get(_idx); } rfl::Result get_field_from_object( - const std::string& _name, const InputObjectType _obj) const noexcept { - const auto it = _obj->find(_name); - if (it == _obj->end()) { + const std::string& _name, const InputObjectType& _obj) const noexcept { + auto var = (*_obj)[_name]; + if (!var) { return error("Object contains no field named '" + _name + "'."); } - return &it->second; + return var.node(); } - bool is_empty(const InputVarType _var) const noexcept { - return _var->is_empty(); + bool is_empty(const InputVarType& _var) const noexcept { + return !_var && true; } 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>()) { - if (!_var->is_string()) { - return error("Could not cast to std::string!"); + const auto ptr = _var->as(); + if (!ptr) { + return error("Could not cast the node to std::string!"); } - return _var->as_string(); - + return **ptr; } else if constexpr (std::is_same, bool>()) { - if (!_var->is_boolean()) { - return error("Could not cast to bool!"); + const auto ptr = _var->as(); + if (!ptr) { + return error("Could not cast the node to bool!"); } - return _var->as_boolean(); - + return **ptr; } else if constexpr (std::is_floating_point>()) { - if (!_var->is_floating()) { - return error("Could not cast to double!"); + const auto ptr = _var->as(); + if (!ptr) { + return error("Could not cast the node to double!"); } - return static_cast>(_var->as_floating()); - + return static_cast>(**ptr); } else if constexpr (std::is_integral>()) { - if (!_var->is_integer()) { - return error("Could not cast to int64_t!"); + const auto ptr = _var->as(); + if (!ptr) { + return error("Could not cast the node to int64_t!"); } - return static_cast>(_var->as_integer()); - + return static_cast>(**ptr); } else { static_assert(rfl::always_false_v, "Unsupported type."); } } - rfl::Result to_array(const InputVarType _var) const noexcept { - if (!_var->is_array()) { + rfl::Result to_array( + const InputVarType& _var) const noexcept { + const auto ptr = _var->as_array(); + if (!ptr) { return error("Could not cast to an array!"); } - return &_var->as_array(); + return ptr; } template std::optional read_array(const ArrayReader& _array_reader, - const InputArrayType _arr) const noexcept { - for (const auto& node : *_arr) { + const InputArrayType& _arr) const noexcept { + for (auto& node : *_arr) { const auto err = _array_reader.read(&node); if (err) { return err; @@ -106,18 +104,19 @@ struct Reader { template std::optional read_object(const ObjectReader& _object_reader, InputObjectType _obj) const noexcept { - for (const auto& [k, v] : *_obj) { + for (auto& [k, v] : *_obj) { _object_reader.read(std::string_view(k), &v); } return std::nullopt; } rfl::Result to_object( - const InputVarType _var) const noexcept { - if (!_var->is_table()) { + const InputVarType& _var) const noexcept { + const auto ptr = _var->as_table(); + if (!ptr) { return error("Could not cast to a table!"); } - return &_var->as_table(); + return ptr; } template diff --git a/include/rfl/toml/Writer.hpp b/include/rfl/toml/Writer.hpp index cc332537..e53c96b4 100644 --- a/include/rfl/toml/Writer.hpp +++ b/include/rfl/toml/Writer.hpp @@ -7,14 +7,10 @@ #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" @@ -80,7 +76,7 @@ class Writer { OutputVarType add_value_to_object(const std::string_view& _name, const T& _var, OutputObjectType* _parent) const noexcept { - (*_parent->val_)[std::string(_name)] = ::toml::value(_var); + _parent->val_->emplace(_name, ::toml::value(_var)); return OutputVarType{}; } diff --git a/include/rfl/toml/read.hpp b/include/rfl/toml/read.hpp index 9aea0b8b..e47e00e3 100644 --- a/include/rfl/toml/read.hpp +++ b/include/rfl/toml/read.hpp @@ -3,12 +3,7 @@ #include #include -#include - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Warray-bounds" -#include -#pragma GCC diagnostic pop +#include #include "../Processors.hpp" #include "../internal/wrap_in_rfl_array_t.hpp" @@ -33,12 +28,8 @@ auto read(InputVarType _var) { /// Reads a TOML string. template Result> read(const std::string& _toml_str) { - 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))); - } + auto table = ::toml::parse(_toml_str); + return read(&table); } /// Reads a TOML string. diff --git a/include/rfl/toml/write.hpp b/include/rfl/toml/write.hpp index c3141dfa..9d4f306f 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,6 +13,22 @@ 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) { @@ -26,14 +42,8 @@ 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{}); - 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; + sstream << root; + return sstream.str(); } } // namespace rfl::toml diff --git a/src/rfl/toml/Writer.cpp b/src/rfl/toml/Writer.cpp index 49dadf33..22e19032 100644 --- a/src/rfl/toml/Writer.cpp +++ b/src/rfl/toml/Writer.cpp @@ -17,28 +17,30 @@ 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_->back().as_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_)[std::string(_name)].as_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_->back().as_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_)[std::string(_name)].as_table()}; + return OutputObjectType{_parent->val_->at_path(_name).as_table()}; } Writer::OutputVarType Writer::add_null_to_array( diff --git a/tests/json/test_json_schema4.cpp b/tests/json/test_json_schema4.cpp index 541d40e7..9518c33a 100644 --- a/tests/json/test_json_schema4.cpp +++ b/tests/json/test_json_schema4.cpp @@ -8,7 +8,7 @@ #include "write_and_read.hpp" -namespace test_json_schema { +namespace test_json_schema4 { enum class Color { red, green, blue }; @@ -66,8 +66,8 @@ TEST(json, test_json_schema4) { rfl::json::to_schema(); const std::string expected = - R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/definitions/test_json_schema__Person","definitions":{"rfl__Generic":{"anyOf":[{"anyOf":[{"type":"boolean"},{"type":"integer"},{"type":"number"},{"type":"string"},{"type":"object","additionalProperties":{"$ref":"#/definitions/rfl__Generic"}},{"type":"array","items":{"$ref":"#/definitions/rfl__Generic"}}]},{"type":"null"}]},"test_json_schema__Circle":{"type":"object","properties":{"radius":{"type":"number"}},"required":[]},"test_json_schema__Circle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Circle"]},"radius":{"type":"number"}},"required":[]},"test_json_schema__Person":{"type":"object","properties":{"firstName":{"type":"string"},"lastName":{"type":"string"},"email":{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},"town":{"type":"string"},"color":{"type":"string","enum":["red","green","blue"]},"age":{"allOf":[{"minimum":0,"type":"integer"},{"maximum":130,"type":"integer"}]},"salary":{"type":"number"},"children":{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/definitions/test_json_schema__Person"}},"variant":{"anyOf":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema__Person"}},{"type":"integer"}]},"tuple":{"type":"array","prefixItems":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema__Person"}},{"type":"integer"}],"items":false},"taggedUnion":{"anyOf":[{"$ref":"#/definitions/test_json_schema__Rectangle__tagged"},{"$ref":"#/definitions/test_json_schema__Square__tagged"},{"$ref":"#/definitions/test_json_schema__Circle__tagged"}]},"fieldVariant":{"anyOf":[{"type":"object","properties":{"rectangle":{"$ref":"#/definitions/test_json_schema__Rectangle"}},"required":[]},{"type":"object","properties":{"square":{"$ref":"#/definitions/test_json_schema__Square"}},"required":[]},{"type":"object","properties":{"circle":{"$ref":"#/definitions/test_json_schema__Circle"}},"required":[]}]},"generic":{"$ref":"#/definitions/rfl__Generic"},"with_extra_fields":{"$ref":"#/definitions/test_json_schema__WithExtraFields"}},"required":[]},"test_json_schema__Rectangle":{"type":"object","properties":{"width":{"type":"number"},"height":{"type":"number"}},"required":[]},"test_json_schema__Rectangle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Rectangle"]},"width":{"type":"number"},"height":{"type":"number"}},"required":[]},"test_json_schema__Square":{"type":"object","properties":{"width":{"type":"number"}},"required":[]},"test_json_schema__Square__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Square"]},"width":{"type":"number"}},"required":[]},"test_json_schema__WithExtraFields":{"type":"object","properties":{"f1":{"type":"string"},"f2":{"type":"string"}},"required":[],"additionalProperties":{"type":"string"}}}})"; + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/definitions/test_json_schema4__Person","definitions":{"rfl__Generic":{"anyOf":[{"anyOf":[{"type":"boolean"},{"type":"integer"},{"type":"number"},{"type":"string"},{"type":"object","additionalProperties":{"$ref":"#/definitions/rfl__Generic"}},{"type":"array","items":{"$ref":"#/definitions/rfl__Generic"}}]},{"type":"null"}]},"test_json_schema4__Circle":{"type":"object","properties":{"radius":{"type":"number"}},"required":[]},"test_json_schema4__Circle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Circle"]},"radius":{"type":"number"}},"required":[]},"test_json_schema4__Person":{"type":"object","properties":{"firstName":{"type":"string"},"lastName":{"type":"string"},"email":{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},"town":{"type":"string"},"color":{"type":"string","enum":["red","green","blue"]},"age":{"allOf":[{"minimum":0,"type":"integer"},{"maximum":130,"type":"integer"}]},"salary":{"type":"number"},"children":{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/definitions/test_json_schema4__Person"}},"variant":{"anyOf":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema4__Person"}},{"type":"integer"}]},"tuple":{"type":"array","prefixItems":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema4__Person"}},{"type":"integer"}],"items":false},"taggedUnion":{"anyOf":[{"$ref":"#/definitions/test_json_schema4__Rectangle__tagged"},{"$ref":"#/definitions/test_json_schema4__Square__tagged"},{"$ref":"#/definitions/test_json_schema4__Circle__tagged"}]},"fieldVariant":{"anyOf":[{"type":"object","properties":{"rectangle":{"$ref":"#/definitions/test_json_schema4__Rectangle"}},"required":[]},{"type":"object","properties":{"square":{"$ref":"#/definitions/test_json_schema4__Square"}},"required":[]},{"type":"object","properties":{"circle":{"$ref":"#/definitions/test_json_schema4__Circle"}},"required":[]}]},"generic":{"$ref":"#/definitions/rfl__Generic"},"with_extra_fields":{"$ref":"#/definitions/test_json_schema4__WithExtraFields"}},"required":[]},"test_json_schema4__Rectangle":{"type":"object","properties":{"width":{"type":"number"},"height":{"type":"number"}},"required":[]},"test_json_schema4__Rectangle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Rectangle"]},"width":{"type":"number"},"height":{"type":"number"}},"required":[]},"test_json_schema4__Square":{"type":"object","properties":{"width":{"type":"number"}},"required":[]},"test_json_schema4__Square__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Square"]},"width":{"type":"number"}},"required":[]},"test_json_schema4__WithExtraFields":{"type":"object","properties":{"f1":{"type":"string"},"f2":{"type":"string"}},"required":[],"additionalProperties":{"type":"string"}}}})"; EXPECT_EQ(json_schema, expected); } -} // namespace test_json_schema +} // namespace test_json_schema4 diff --git a/vcpkg b/vcpkg index cd124b84..e1139424 160000 --- a/vcpkg +++ b/vcpkg @@ -1 +1 @@ -Subproject commit cd124b84feb0c02a24a2d90981e8358fdee0e077 +Subproject commit e113942454e6b829366af7bcf196bbdbccbd6b3c diff --git a/vcpkg.json b/vcpkg.json index 4725f0b3..fd41f379 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -48,8 +48,8 @@ "version>=": "3.9.3" }, { - "name": "toml11", - "version>=": "4.2.0" + "name": "tomlplusplus", + "version>=": "3.4.0#1" }, { "name": "yaml-cpp", @@ -76,7 +76,7 @@ "version>=": "2022-02-06#1" }, { - "name":"vcpkg-pkgconfig-get-modules", + "name": "vcpkg-pkgconfig-get-modules", "version>=": "2024-04-03" } ]