From 307ebd1f476a81ee59e7f62695511da14e38a830 Mon Sep 17 00:00:00 2001 From: Mark Gillard Date: Mon, 15 Nov 2021 09:06:03 +0200 Subject: [PATCH] refactoring --- CHANGELOG.md | 2 +- examples/parse_benchmark.cpp | 4 ++- include/toml++/impl/node.h | 41 +++++++++++++----------------- include/toml++/impl/node_view.h | 22 ++++++++-------- include/toml++/impl/preprocessor.h | 26 +++++++------------ include/toml++/impl/value.h | 10 ++++---- include/toml++/toml.h | 1 - toml.hpp | 37 ++++++++++----------------- 8 files changed, 60 insertions(+), 83 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 925a281..595cc84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,7 +72,7 @@ code changes at callsites or in build systems are indicated with ⚠️. - ⚠️ renamed `TOML_PARSER` option to `TOML_ENABLE_PARSER` (`TOML_PARSER` will continue to work but is deprecated) - ⚠️ renamed `TOML_UNRELEASED_FEATURES` to `TOML_ENABLE_UNRELEASED_FEATURES` (`TOML_UNRELEASED_FEATURES` will continue to work but is deprecated) - ⚠️ renamed `TOML_WINDOWS_COMPAT` to `TOML_ENABLE_WINDOWS_COMPAT` (`TOML_WINDOWS_COMPAT` will continue to work but is deprecated) -- `toml::table::ref()` now supports explicit ref categories and cv-qualifiers +- `toml::node::ref()` now supports explicit ref categories and cv-qualifiers - applied clang-format to all the things 🎉️ - improved performance of parser - made date/time constructors accept any integral types diff --git a/examples/parse_benchmark.cpp b/examples/parse_benchmark.cpp index b484a43..6d97471 100644 --- a/examples/parse_benchmark.cpp +++ b/examples/parse_benchmark.cpp @@ -58,8 +58,10 @@ int main(int argc, char** argv) #else const auto result = toml::parse(file_content, file_path); if (!result) + { std::cerr << result.error() << "\n"; - return 1; + return 1; + } #endif } diff --git a/include/toml++/impl/node.h b/include/toml++/impl/node.h index d9600e4..084eda5 100644 --- a/include/toml++/impl/node.h +++ b/include/toml++/impl/node.h @@ -663,24 +663,7 @@ TOML_NAMESPACE_START //# TOML_NODISCARD //# std::vector select() const noexcept; - /// \brief Gets a raw reference to a value node's underlying data. - /// - /// \note Providing explicit ref qualifiers acts as an explicit ref-category cast. Providing - /// explicit cv-ref qualifiers 'merges' them with whatever the cv qualification of the node is. Examples: - /// | node | T | return type | - /// |-------------|------------------------|------------------------------| - /// | node& | std::string | std::string& | - /// | node& | std::string& | std::string& | - /// | node& | std::string&& | std::string&& | - /// | node&& | std::string | std::string&& | - /// | node&& | std::string& | std::string& | - /// | node&& | std::string&& | std::string&& | - /// | const node& | std::string | const std::string& | - /// | const node& | std::string& | const std::string& | - /// | const node& | std::string&& | const std::string&& | - /// | const node& | volatile std::string | const volatile std::string& | - /// | const node& | volatile std::string& | const volatile std::string& | - /// | const node& | volatile std::string&& | const volatile std::string&& | + /// \brief Gets a raw reference to a node's underlying data. /// /// \warning This function is dangerous if used carelessly and **WILL** break your code if the /// chosen value type doesn't match the node's actual type. In debug builds an assertion @@ -691,11 +674,21 @@ TOML_NAMESPACE_START /// max = 45 /// )"sv); /// - /// int64_t& min_ref = tbl.get("min")->ref(); // matching type - /// double& max_ref = tbl.get("max")->ref(); // mismatched type, hits assert() + /// int64_t& min_ref = tbl.at("min").ref(); // matching type + /// double& max_ref = tbl.at("max").ref(); // mismatched type, hits assert() /// \ecpp /// - /// \tparam T One of the TOML value types. + /// \note Specifying explicit ref qualifiers acts as an explicit ref-category cast, + /// whereas specifying explicit cv-ref qualifiers merges them with whatever + /// the cv qualification of the node is (to ensure cv-correctness is propagated), e.g.: + /// | node | T | return type | + /// |-------------|------------------------|------------------------------| + /// | node& | std::string | std::string& | + /// | node& | std::string&& | std::string&& | + /// | const node& | volatile std::string | const volatile std::string& | + /// | const node& | volatile std::string&& | const volatile std::string&& | + /// + /// \tparam T toml::table, toml::array, or one of the TOML value types. /// /// \returns A reference to the underlying data. template @@ -705,7 +698,7 @@ TOML_NAMESPACE_START return do_ref(*this); } - /// \brief Gets a raw reference to a value node's underlying data (rvalue overload). + /// \brief Gets a raw reference to a node's underlying data (rvalue overload). template TOML_PURE_GETTER decltype(auto) ref() && noexcept @@ -713,7 +706,7 @@ TOML_NAMESPACE_START return do_ref(std::move(*this)); } - /// \brief Gets a raw reference to a value node's underlying data (const lvalue overload). + /// \brief Gets a raw reference to a node's underlying data (const lvalue overload). template TOML_PURE_GETTER decltype(auto) ref() const& noexcept @@ -721,7 +714,7 @@ TOML_NAMESPACE_START return do_ref(*this); } - /// \brief Gets a raw reference to a value node's underlying data (const rvalue overload). + /// \brief Gets a raw reference to a node's underlying data (const rvalue overload). template TOML_PURE_GETTER decltype(auto) ref() const&& noexcept diff --git a/include/toml++/impl/node_view.h b/include/toml++/impl/node_view.h index 650c7f1..af95017 100644 --- a/include/toml++/impl/node_view.h +++ b/include/toml++/impl/node_view.h @@ -526,17 +526,6 @@ TOML_NAMESPACE_START /// \brief Gets a raw reference to the viewed node's underlying data. /// - /// /// \note Providing explicit ref qualifiers acts as an explicit ref-category cast. Providing - /// explicit cv-ref qualifiers 'merges' them with whatever the cv qualification of the viewed node is. Examples: - /// | node | T | return type | - /// |-------------|------------------------|------------------------------| - /// | node | std::string | std::string& | - /// | node | std::string& | std::string& | - /// | node | std::string&& | std::string&& | - /// | const node | volatile std::string | const volatile std::string& | - /// | const node | volatile std::string& | const volatile std::string& | - /// | const node | volatile std::string&& | const volatile std::string&& | - /// /// \warning This function is dangerous if used carelessly and **WILL** break your code if the /// node_view didn't reference a node, or the chosen value type doesn't match the node's /// actual type. In debug builds an assertion will fire when invalid accesses are attempted: \cpp @@ -551,6 +540,17 @@ TOML_NAMESPACE_START /// int64_t& foo_ref = tbl["foo"].ref(); // nonexistent key, hits assert() /// \ecpp /// + /// \note Specifying explicit ref qualifiers acts as an explicit ref-category cast, + /// whereas specifying explicit cv-ref qualifiers merges them with whatever + /// the cv qualification of the viewed node is (to ensure cv-correctness is propagated), e.g.: + /// | node_view | T | return type | + /// |-----------------------|------------------------|------------------------------| + /// | node_view | std::string | std::string& | + /// | node_view | std::string&& | std::string&& | + /// | node_view | volatile std::string | const volatile std::string& | + /// | node_view | volatile std::string&& | const volatile std::string&& | + /// + /// /// \tparam T One of the TOML value types. /// /// \returns A reference to the underlying data. diff --git a/include/toml++/impl/preprocessor.h b/include/toml++/impl/preprocessor.h index a491f69..1dafee4 100644 --- a/include/toml++/impl/preprocessor.h +++ b/include/toml++/impl/preprocessor.h @@ -486,14 +486,6 @@ #error toml++ requires C++17 or higher. For a TOML library supporting pre-C++11 see https://github.com/ToruNiina/Boost.toml #elif TOML_CPP_VERSION < 201703L #error toml++ requires C++17 or higher. For a TOML library supporting C++11 see https://github.com/ToruNiina/toml11 -#elif TOML_CPP_VERSION >= 202600L - #define TOML_CPP 26 -#elif TOML_CPP_VERSION >= 202300L - #define TOML_CPP 23 -#elif TOML_CPP_VERSION >= 202002L - #define TOML_CPP 20 -#elif TOML_CPP_VERSION >= 201703L - #define TOML_CPP 17 #endif #undef TOML_CPP_VERSION @@ -809,24 +801,24 @@ #define TOML_LIB_SINGLE_HEADER 0 -#define TOML_MAKE_VERSION(maj, min, rev) \ - ((maj) * 1000 + (min) * 25 + (rev)) +#define TOML_MAKE_VERSION(major, minor, patch) \ + ((major) * 10000 + (minor) * 100 + (patch)) #if TOML_ENABLE_UNRELEASED_FEATURES - #define TOML_LANG_EFFECTIVE_VERSION \ + #define TOML_LANG_EFFECTIVE_VERSION \ TOML_MAKE_VERSION(TOML_LANG_MAJOR, TOML_LANG_MINOR, TOML_LANG_PATCH+1) #else - #define TOML_LANG_EFFECTIVE_VERSION \ + #define TOML_LANG_EFFECTIVE_VERSION \ TOML_MAKE_VERSION(TOML_LANG_MAJOR, TOML_LANG_MINOR, TOML_LANG_PATCH) #endif -#define TOML_LANG_HIGHER_THAN(maj, min, rev) \ - (TOML_LANG_EFFECTIVE_VERSION > TOML_MAKE_VERSION(maj, min, rev)) +#define TOML_LANG_HIGHER_THAN(major, minor, patch) \ + (TOML_LANG_EFFECTIVE_VERSION > TOML_MAKE_VERSION(major, minor, patch)) -#define TOML_LANG_AT_LEAST(maj, min, rev) \ - (TOML_LANG_EFFECTIVE_VERSION >= TOML_MAKE_VERSION(maj, min, rev)) +#define TOML_LANG_AT_LEAST(major, minor, patch) \ + (TOML_LANG_EFFECTIVE_VERSION >= TOML_MAKE_VERSION(major, minor, patch)) -#define TOML_LANG_UNRELEASED \ +#define TOML_LANG_UNRELEASED \ TOML_LANG_HIGHER_THAN(TOML_LANG_MAJOR, TOML_LANG_MINOR, TOML_LANG_PATCH) #ifndef TOML_ABI_NAMESPACES diff --git a/include/toml++/impl/value.h b/include/toml++/impl/value.h index 443b475..bf86c13 100644 --- a/include/toml++/impl/value.h +++ b/include/toml++/impl/value.h @@ -779,12 +779,12 @@ TOML_NAMESPACE_START { if constexpr (std::is_same_v) { - const auto lhs_class = impl::fpclassify(lhs.val_); - const auto rhs_class = impl::fpclassify(rhs); - if (lhs_class == impl::fp_class::nan && rhs_class == impl::fp_class::nan) - return true; - if ((lhs_class == impl::fp_class::nan) != (rhs_class == impl::fp_class::nan)) + const auto lhs_nan = impl::fpclassify(lhs.val_) == impl::fp_class::nan; + const auto rhs_nan = impl::fpclassify(rhs) == impl::fp_class::nan; + if (lhs_nan != rhs_nan) return false; + if (lhs_nan) + return true; } return lhs.val_ == rhs; } diff --git a/include/toml++/toml.h b/include/toml++/toml.h index 71eb761..be67a52 100644 --- a/include/toml++/toml.h +++ b/include/toml++/toml.h @@ -90,7 +90,6 @@ TOML_POP_WARNINGS; #undef TOML_CONST_GETTER #undef TOML_CONST_INLINE_GETTER #undef TOML_CONSTRAINED_TEMPLATE -#undef TOML_CPP #undef TOML_DISABLE_ARITHMETIC_WARNINGS #undef TOML_DISABLE_CODE_ANALYSIS_WARNINGS #undef TOML_DISABLE_SPAM_WARNINGS diff --git a/toml.hpp b/toml.hpp index 18c3a17..642726d 100644 --- a/toml.hpp +++ b/toml.hpp @@ -501,14 +501,6 @@ #error toml++ requires C++17 or higher. For a TOML library supporting pre-C++11 see https://github.com/ToruNiina/Boost.toml #elif TOML_CPP_VERSION < 201703L #error toml++ requires C++17 or higher. For a TOML library supporting C++11 see https://github.com/ToruNiina/toml11 -#elif TOML_CPP_VERSION >= 202600L - #define TOML_CPP 26 -#elif TOML_CPP_VERSION >= 202300L - #define TOML_CPP 23 -#elif TOML_CPP_VERSION >= 202002L - #define TOML_CPP 20 -#elif TOML_CPP_VERSION >= 201703L - #define TOML_CPP 17 #endif #undef TOML_CPP_VERSION @@ -808,24 +800,24 @@ #define TOML_LIB_SINGLE_HEADER 1 -#define TOML_MAKE_VERSION(maj, min, rev) \ - ((maj) * 1000 + (min) * 25 + (rev)) +#define TOML_MAKE_VERSION(major, minor, patch) \ + ((major) * 10000 + (minor) * 100 + (patch)) #if TOML_ENABLE_UNRELEASED_FEATURES - #define TOML_LANG_EFFECTIVE_VERSION \ + #define TOML_LANG_EFFECTIVE_VERSION \ TOML_MAKE_VERSION(TOML_LANG_MAJOR, TOML_LANG_MINOR, TOML_LANG_PATCH+1) #else - #define TOML_LANG_EFFECTIVE_VERSION \ + #define TOML_LANG_EFFECTIVE_VERSION \ TOML_MAKE_VERSION(TOML_LANG_MAJOR, TOML_LANG_MINOR, TOML_LANG_PATCH) #endif -#define TOML_LANG_HIGHER_THAN(maj, min, rev) \ - (TOML_LANG_EFFECTIVE_VERSION > TOML_MAKE_VERSION(maj, min, rev)) +#define TOML_LANG_HIGHER_THAN(major, minor, patch) \ + (TOML_LANG_EFFECTIVE_VERSION > TOML_MAKE_VERSION(major, minor, patch)) -#define TOML_LANG_AT_LEAST(maj, min, rev) \ - (TOML_LANG_EFFECTIVE_VERSION >= TOML_MAKE_VERSION(maj, min, rev)) +#define TOML_LANG_AT_LEAST(major, minor, patch) \ + (TOML_LANG_EFFECTIVE_VERSION >= TOML_MAKE_VERSION(major, minor, patch)) -#define TOML_LANG_UNRELEASED \ +#define TOML_LANG_UNRELEASED \ TOML_LANG_HIGHER_THAN(TOML_LANG_MAJOR, TOML_LANG_MINOR, TOML_LANG_PATCH) #ifndef TOML_ABI_NAMESPACES @@ -4278,12 +4270,12 @@ TOML_NAMESPACE_START { if constexpr (std::is_same_v) { - const auto lhs_class = impl::fpclassify(lhs.val_); - const auto rhs_class = impl::fpclassify(rhs); - if (lhs_class == impl::fp_class::nan && rhs_class == impl::fp_class::nan) - return true; - if ((lhs_class == impl::fp_class::nan) != (rhs_class == impl::fp_class::nan)) + const auto lhs_nan = impl::fpclassify(lhs.val_) == impl::fp_class::nan; + const auto rhs_nan = impl::fpclassify(rhs) == impl::fp_class::nan; + if (lhs_nan != rhs_nan) return false; + if (lhs_nan) + return true; } return lhs.val_ == rhs; } @@ -15320,7 +15312,6 @@ TOML_POP_WARNINGS; #undef TOML_CONST_GETTER #undef TOML_CONST_INLINE_GETTER #undef TOML_CONSTRAINED_TEMPLATE -#undef TOML_CPP #undef TOML_DISABLE_ARITHMETIC_WARNINGS #undef TOML_DISABLE_CODE_ANALYSIS_WARNINGS #undef TOML_DISABLE_SPAM_WARNINGS