From 23f462b598b9da7b30e2924af39f807c45deaa63 Mon Sep 17 00:00:00 2001 From: Anthony VH Date: Sun, 10 Jan 2021 19:23:32 +0100 Subject: [PATCH] Reduced code duplication, renamed tag to identity_tag. --- include/nlohmann/adl_serializer.hpp | 7 +- .../nlohmann/detail/conversions/from_json.hpp | 101 ++++++++-------- .../detail/meta/{tag.hpp => identity_tag.hpp} | 2 +- single_include/nlohmann/json.hpp | 111 ++++++++---------- 4 files changed, 103 insertions(+), 118 deletions(-) rename include/nlohmann/detail/meta/{tag.hpp => identity_tag.hpp} (75%) diff --git a/include/nlohmann/adl_serializer.hpp b/include/nlohmann/adl_serializer.hpp index 9eb751b70..1dee29eb9 100644 --- a/include/nlohmann/adl_serializer.hpp +++ b/include/nlohmann/adl_serializer.hpp @@ -5,6 +5,7 @@ #include #include +#include #include namespace nlohmann @@ -46,10 +47,10 @@ struct adl_serializer */ template static auto from_json(BasicJsonType && j) noexcept( - noexcept(::nlohmann::from_json(std::forward(j), detail::tag {}))) - -> decltype(::nlohmann::from_json(std::forward(j), detail::tag {})) + noexcept(::nlohmann::from_json(std::forward(j), detail::identity_tag {}))) + -> decltype(::nlohmann::from_json(std::forward(j), detail::identity_tag {})) { - return ::nlohmann::from_json(std::forward(j), detail::tag {}); + return ::nlohmann::from_json(std::forward(j), detail::identity_tag {}); } /*! diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index c9b88ce5b..b29ac05c6 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include @@ -250,16 +250,16 @@ void()) } template < typename BasicJsonType, typename Array, std::size_t... Is > -Array from_json_array_impl(BasicJsonType&& j, tag /*unused*/, index_sequence /*unused*/) +Array from_json_array_impl(BasicJsonType&& j, identity_tag /*unused*/, index_sequence /*unused*/) { return { std::forward(j).at(Is).template get()... }; } template < typename BasicJsonType, typename T, std::size_t N, enable_if_t < !std::is_default_constructible>::value, int > = 0 > -auto from_json(BasicJsonType && j, tag> t) +auto from_json(BasicJsonType && j, identity_tag> tag) -> decltype(j.template get(), -from_json_array_impl(std::forward(j), t, make_index_sequence {})) +from_json_array_impl(std::forward(j), tag, make_index_sequence {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -267,7 +267,7 @@ from_json_array_impl(std::forward(j), t, make_index_sequence { std::string(j.type_name()))); } - return from_json_array_impl(std::forward(j), t, make_index_sequence {}); + return from_json_array_impl(std::forward(j), tag, make_index_sequence {}); } template @@ -345,44 +345,24 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) } } -template>::value, int> = 0> -void from_json(BasicJsonType && j, std::pair& p) +template < typename BasicJsonType, class A1, class A2 > +std::pair from_json_pair_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<0> /*unused*/) { - if (JSON_HEDLEY_UNLIKELY(!j.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + - std::string(j.type_name()))); - } - - p = {std::forward(j).at(0).template get(), - std::forward(j).at(1).template get() - }; -} - -template < typename BasicJsonType, class A1, class A2, - enable_if_t < !std::is_default_constructible>::value, int > = 0 > -std::pair from_json(BasicJsonType && j, tag> /*unused*/) -{ - if (JSON_HEDLEY_UNLIKELY(!j.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + - std::string(j.type_name()))); - } - return {std::forward(j).at(0).template get(), std::forward(j).at(1).template get()}; } -template -void from_json_tuple_impl(BasicJsonType&& j, Tuple& t, index_sequence /*unused*/) + +template>::value, int> = 0> +void from_json_pair_impl(BasicJsonType && j, std::pair& p, priority_tag<1> /*unused*/) { - t = std::make_tuple(std::forward(j).at(Idx).template get::type>()...); + p = from_json_pair_impl(std::forward(j), identity_tag> {}, priority_tag<0> {}); } -template>::value, int > = 0 > -void from_json(BasicJsonType && j, std::tuple& t) +template +auto from_json(BasicJsonType&& j, PairRelatedType&& p) +-> decltype(from_json_pair_impl(std::forward(j), std::forward(p), priority_tag<1> {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -390,18 +370,25 @@ void from_json(BasicJsonType && j, std::tuple& t) std::string(j.type_name()))); } - from_json_tuple_impl(std::forward(j), t, index_sequence_for {}); + return from_json_pair_impl(std::forward(j), std::forward(p), priority_tag<1> {}); } template -Tuple from_json_tuple_impl(BasicJsonType&& j, tag /*unused*/, index_sequence /*unused*/) +Tuple from_json_tuple_impl(BasicJsonType&& j, identity_tag /*unused*/, index_sequence /*unused*/, priority_tag<0> /*unused*/) { return std::make_tuple(std::forward(j).at(Idx).template get::type>()...); } -template < typename BasicJsonType, typename... Args, - enable_if_t < !std::is_default_constructible>::value, int > = 0 > -std::tuple from_json(BasicJsonType && j, tag> t) +template::value, int> = 0> +void from_json_tuple_impl(BasicJsonType && j, Tuple& t, index_sequence /*unused*/, priority_tag<1> /*unused*/) +{ + t = from_json_tuple_impl(std::forward(j), identity_tag {}, priority_tag<0> {}); +} + +template +auto from_json_tuple(BasicJsonType&& j, TupleRelated&& t, index_sequence idx) +-> decltype(from_json_tuple_impl(std::forward(j), std::forward(t), idx, priority_tag<1> {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -409,7 +396,21 @@ std::tuple from_json(BasicJsonType && j, tag> t) std::string(j.type_name()))); } - return from_json_tuple_impl(std::forward(j), t, index_sequence_for {}); + return from_json_tuple_impl(std::forward(j), std::forward(t), idx, priority_tag<1> {}); +} + +template +auto from_json(BasicJsonType&& j, std::tuple& t) +-> decltype(from_json_tuple(std::forward(j), t, index_sequence_for {})) +{ + from_json_tuple(std::forward(j), t, index_sequence_for {}); +} + +template +auto from_json(BasicJsonType&& j, identity_tag> tag) +-> decltype(from_json_tuple(std::forward(j), std::move(tag), index_sequence_for {})) +{ + return from_json_tuple(std::forward(j), std::move(tag), index_sequence_for {}); } template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator, @@ -455,21 +456,11 @@ void from_json(const BasicJsonType& j, std::unordered_map - auto operator()(const BasicJsonType& j, T& val) const - noexcept(noexcept(from_json(j, val))) - -> decltype(from_json(j, val), void()) + auto operator()(const BasicJsonType& j, T&& val) const + noexcept(noexcept(from_json(j, std::forward(val)))) + -> decltype(from_json(j, std::forward(val))) { - return from_json(j, val); - } - - // overload to pass calls to built-in from_json functions for non-default constructible STL - // types (e.g. std::array, where X is not default constructible). - template - auto operator()(const BasicJsonType& j, detail::tag t) const - noexcept(noexcept(from_json(j, t))) - -> decltype(from_json(j, t)) - { - return from_json(j, t); + return from_json(j, std::forward(val)); } }; } // namespace detail diff --git a/include/nlohmann/detail/meta/tag.hpp b/include/nlohmann/detail/meta/identity_tag.hpp similarity index 75% rename from include/nlohmann/detail/meta/tag.hpp rename to include/nlohmann/detail/meta/identity_tag.hpp index 631887d1d..73a3e9170 100644 --- a/include/nlohmann/detail/meta/tag.hpp +++ b/include/nlohmann/detail/meta/identity_tag.hpp @@ -5,6 +5,6 @@ namespace nlohmann namespace detail { // dispatching helper struct -template struct tag {}; +template struct identity_tag {}; } // namespace detail } // namespace nlohmann diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 06f7caa37..f3c1b987c 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2813,7 +2813,7 @@ constexpr T static_const::value; } // namespace detail } // namespace nlohmann -// #include +// #include namespace nlohmann @@ -2821,7 +2821,7 @@ namespace nlohmann namespace detail { // dispatching helper struct -template struct tag {}; +template struct identity_tag {}; } // namespace detail } // namespace nlohmann @@ -3747,16 +3747,16 @@ void()) } template < typename BasicJsonType, typename Array, std::size_t... Is > -Array from_json_array_impl(BasicJsonType&& j, tag /*unused*/, index_sequence /*unused*/) +Array from_json_array_impl(BasicJsonType&& j, identity_tag /*unused*/, index_sequence /*unused*/) { return { std::forward(j).at(Is).template get()... }; } template < typename BasicJsonType, typename T, std::size_t N, enable_if_t < !std::is_default_constructible>::value, int > = 0 > -auto from_json(BasicJsonType && j, tag> t) +auto from_json(BasicJsonType && j, identity_tag> tag) -> decltype(j.template get(), -from_json_array_impl(std::forward(j), t, make_index_sequence {})) +from_json_array_impl(std::forward(j), tag, make_index_sequence {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -3764,7 +3764,7 @@ from_json_array_impl(std::forward(j), t, make_index_sequence { std::string(j.type_name()))); } - return from_json_array_impl(std::forward(j), t, make_index_sequence {}); + return from_json_array_impl(std::forward(j), tag, make_index_sequence {}); } template @@ -3842,44 +3842,24 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) } } -template>::value, int> = 0> -void from_json(BasicJsonType && j, std::pair& p) +template < typename BasicJsonType, class A1, class A2 > +std::pair from_json_pair_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<0> /*unused*/) { - if (JSON_HEDLEY_UNLIKELY(!j.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + - std::string(j.type_name()))); - } - - p = {std::forward(j).at(0).template get(), - std::forward(j).at(1).template get() - }; -} - -template < typename BasicJsonType, class A1, class A2, - enable_if_t < !std::is_default_constructible>::value, int > = 0 > -std::pair from_json(BasicJsonType && j, tag> /*unused*/) -{ - if (JSON_HEDLEY_UNLIKELY(!j.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + - std::string(j.type_name()))); - } - return {std::forward(j).at(0).template get(), std::forward(j).at(1).template get()}; } -template -void from_json_tuple_impl(BasicJsonType&& j, Tuple& t, index_sequence /*unused*/) + +template>::value, int> = 0> +void from_json_pair_impl(BasicJsonType && j, std::pair& p, priority_tag<1> /*unused*/) { - t = std::make_tuple(std::forward(j).at(Idx).template get::type>()...); + p = from_json_pair_impl(std::forward(j), identity_tag> {}, priority_tag<0> {}); } -template>::value, int > = 0 > -void from_json(BasicJsonType && j, std::tuple& t) +template +auto from_json(BasicJsonType&& j, PairRelatedType&& p) +-> decltype(from_json_pair_impl(std::forward(j), std::forward(p), priority_tag<1> {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -3887,18 +3867,25 @@ void from_json(BasicJsonType && j, std::tuple& t) std::string(j.type_name()))); } - from_json_tuple_impl(std::forward(j), t, index_sequence_for {}); + return from_json_pair_impl(std::forward(j), std::forward(p), priority_tag<1> {}); } template -Tuple from_json_tuple_impl(BasicJsonType&& j, tag /*unused*/, index_sequence /*unused*/) +Tuple from_json_tuple_impl(BasicJsonType&& j, identity_tag /*unused*/, index_sequence /*unused*/, priority_tag<0> /*unused*/) { return std::make_tuple(std::forward(j).at(Idx).template get::type>()...); } -template < typename BasicJsonType, typename... Args, - enable_if_t < !std::is_default_constructible>::value, int > = 0 > -std::tuple from_json(BasicJsonType && j, tag> t) +template::value, int> = 0> +void from_json_tuple_impl(BasicJsonType && j, Tuple& t, index_sequence /*unused*/, priority_tag<1> /*unused*/) +{ + t = from_json_tuple_impl(std::forward(j), identity_tag {}, priority_tag<0> {}); +} + +template +auto from_json_tuple(BasicJsonType&& j, TupleRelated&& t, index_sequence idx) +-> decltype(from_json_tuple_impl(std::forward(j), std::forward(t), idx, priority_tag<1> {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -3906,7 +3893,21 @@ std::tuple from_json(BasicJsonType && j, tag> t) std::string(j.type_name()))); } - return from_json_tuple_impl(std::forward(j), t, index_sequence_for {}); + return from_json_tuple_impl(std::forward(j), std::forward(t), idx, priority_tag<1> {}); +} + +template +auto from_json(BasicJsonType&& j, std::tuple& t) +-> decltype(from_json_tuple(std::forward(j), t, index_sequence_for {})) +{ + from_json_tuple(std::forward(j), t, index_sequence_for {}); +} + +template +auto from_json(BasicJsonType&& j, identity_tag> tag) +-> decltype(from_json_tuple(std::forward(j), std::move(tag), index_sequence_for {})) +{ + return from_json_tuple(std::forward(j), std::move(tag), index_sequence_for {}); } template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator, @@ -3952,21 +3953,11 @@ void from_json(const BasicJsonType& j, std::unordered_map - auto operator()(const BasicJsonType& j, T& val) const - noexcept(noexcept(from_json(j, val))) - -> decltype(from_json(j, val), void()) + auto operator()(const BasicJsonType& j, T&& val) const + noexcept(noexcept(from_json(j, std::forward(val)))) + -> decltype(from_json(j, std::forward(val))) { - return from_json(j, val); - } - - // overload to pass calls to built-in from_json functions for non-default constructible STL - // types (e.g. std::array, where X is not default constructible). - template - auto operator()(const BasicJsonType& j, detail::tag t) const - noexcept(noexcept(from_json(j, t))) - -> decltype(from_json(j, t)) - { - return from_json(j, t); + return from_json(j, std::forward(val)); } }; } // namespace detail @@ -4540,6 +4531,8 @@ constexpr const auto& to_json = detail::static_const::value; } // namespace } // namespace nlohmann +// #include + // #include @@ -4582,10 +4575,10 @@ struct adl_serializer */ template static auto from_json(BasicJsonType && j) noexcept( - noexcept(::nlohmann::from_json(std::forward(j), detail::tag {}))) - -> decltype(::nlohmann::from_json(std::forward(j), detail::tag {})) + noexcept(::nlohmann::from_json(std::forward(j), detail::identity_tag {}))) + -> decltype(::nlohmann::from_json(std::forward(j), detail::identity_tag {})) { - return ::nlohmann::from_json(std::forward(j), detail::tag {}); + return ::nlohmann::from_json(std::forward(j), detail::identity_tag {}); } /*!