Merge pull request #1391 from pratikpc/develop

Added Support for Structured Bindings
This commit is contained in:
Niels Lohmann 2018-12-20 08:58:08 +01:00 committed by GitHub
commit 85849940ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 337 additions and 248 deletions

1
.gitignore vendored
View file

@ -22,3 +22,4 @@ benchmarks/files/numbers/*.json
cmake-build-debug cmake-build-debug
test/test-* test/test-*
/.vs

View file

@ -305,7 +305,7 @@ void to_json(BasicJsonType& j, const std::pair<Args...>& p)
// for https://github.com/nlohmann/json/pull/1134 // for https://github.com/nlohmann/json/pull/1134
template < typename BasicJsonType, typename T, template < typename BasicJsonType, typename T,
enable_if_t<std::is_same<T, typename iteration_proxy<typename BasicJsonType::iterator>::iteration_proxy_internal>::value, int> = 0> enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
void to_json(BasicJsonType& j, const T& b) void to_json(BasicJsonType& j, const T& b)
{ {
j = { {b.key(), b.value()} }; j = { {b.key(), b.value()} };

View file

@ -17,24 +17,21 @@ namespace detail
{ {
// forward declare, to be able to friend it later on // forward declare, to be able to friend it later on
template<typename IteratorType> class iteration_proxy; template<typename IteratorType> class iteration_proxy;
template<typename IteratorType> class iteration_proxy_value;
/*! /*!
@brief a template for a bidirectional iterator for the @ref basic_json class @brief a template for a bidirectional iterator for the @ref basic_json class
This class implements a both iterators (iterator and const_iterator) for the This class implements a both iterators (iterator and const_iterator) for the
@ref basic_json class. @ref basic_json class.
@note An iterator is called *initialized* when a pointer to a JSON value has @note An iterator is called *initialized* when a pointer to a JSON value has
been set (e.g., by a constructor or a copy assignment). If the iterator is been set (e.g., by a constructor or a copy assignment). If the iterator is
default-constructed, it is *uninitialized* and most methods are undefined. default-constructed, it is *uninitialized* and most methods are undefined.
**The library uses assertions to detect calls on uninitialized iterators.** **The library uses assertions to detect calls on uninitialized iterators.**
@requirement The class satisfies the following concept requirements: @requirement The class satisfies the following concept requirements:
- -
[BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator): [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
The iterator that can be moved can be moved in both directions (i.e. The iterator that can be moved can be moved in both directions (i.e.
incremented and decremented). incremented and decremented).
@since version 1.0.0, simplified in version 2.0.9, change to bidirectional @since version 1.0.0, simplified in version 2.0.9, change to bidirectional
iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593) iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
*/ */
@ -45,6 +42,7 @@ class iter_impl
friend iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>; friend iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
friend BasicJsonType; friend BasicJsonType;
friend iteration_proxy<iter_impl>; friend iteration_proxy<iter_impl>;
friend iteration_proxy_value<iter_impl>;
using object_t = typename BasicJsonType::object_t; using object_t = typename BasicJsonType::object_t;
using array_t = typename BasicJsonType::array_t; using array_t = typename BasicJsonType::array_t;

View file

@ -3,25 +3,22 @@
#include <cstddef> // size_t #include <cstddef> // size_t
#include <string> // string, to_string #include <string> // string, to_string
#include <iterator> // input_iterator_tag #include <iterator> // input_iterator_tag
#include <tuple> // tuple_size, get, tuple_element
#include <nlohmann/detail/value_t.hpp> #include <nlohmann/detail/value_t.hpp>
#include <nlohmann/detail/meta/type_traits.hpp>
namespace nlohmann namespace nlohmann
{ {
namespace detail namespace detail
{ {
/// proxy class for the items() function template <typename IteratorType> class iteration_proxy_value
template<typename IteratorType> class iteration_proxy
{
private:
/// helper class for iteration
class iteration_proxy_internal
{ {
public: public:
using difference_type = std::ptrdiff_t; using difference_type = std::ptrdiff_t;
using value_type = iteration_proxy_internal; using value_type = iteration_proxy_value;
using pointer = iteration_proxy_internal*; using pointer = value_type * ;
using reference = iteration_proxy_internal&; using reference = value_type & ;
using iterator_category = std::input_iterator_tag; using iterator_category = std::input_iterator_tag;
private: private:
@ -37,16 +34,16 @@ template<typename IteratorType> class iteration_proxy
const std::string empty_str = ""; const std::string empty_str = "";
public: public:
explicit iteration_proxy_internal(IteratorType it) noexcept : anchor(it) {} explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {}
/// dereference operator (needed for range-based for) /// dereference operator (needed for range-based for)
iteration_proxy_internal& operator*() iteration_proxy_value& operator*()
{ {
return *this; return *this;
} }
/// increment operator (needed for range-based for) /// increment operator (needed for range-based for)
iteration_proxy_internal& operator++() iteration_proxy_value& operator++()
{ {
++anchor; ++anchor;
++array_index; ++array_index;
@ -55,13 +52,13 @@ template<typename IteratorType> class iteration_proxy
} }
/// equality operator (needed for InputIterator) /// equality operator (needed for InputIterator)
bool operator==(const iteration_proxy_internal& o) const noexcept bool operator==(const iteration_proxy_value& o) const noexcept
{ {
return anchor == o.anchor; return anchor == o.anchor;
} }
/// inequality operator (needed for range-based for) /// inequality operator (needed for range-based for)
bool operator!=(const iteration_proxy_internal& o) const noexcept bool operator!=(const iteration_proxy_value& o) const noexcept
{ {
return anchor != o.anchor; return anchor != o.anchor;
} }
@ -101,6 +98,10 @@ template<typename IteratorType> class iteration_proxy
} }
}; };
/// proxy class for the items() function
template<typename IteratorType> class iteration_proxy
{
private:
/// the container to iterate /// the container to iterate
typename IteratorType::reference container; typename IteratorType::reference container;
@ -110,16 +111,52 @@ template<typename IteratorType> class iteration_proxy
: container(cont) {} : container(cont) {}
/// return iterator begin (needed for range-based for) /// return iterator begin (needed for range-based for)
iteration_proxy_internal begin() noexcept iteration_proxy_value<IteratorType> begin() noexcept
{ {
return iteration_proxy_internal(container.begin()); return iteration_proxy_value<IteratorType>(container.begin());
} }
/// return iterator end (needed for range-based for) /// return iterator end (needed for range-based for)
iteration_proxy_internal end() noexcept iteration_proxy_value<IteratorType> end() noexcept
{ {
return iteration_proxy_internal(container.end()); return iteration_proxy_value<IteratorType>(container.end());
} }
}; };
// Structured Bindings Support
// For further reference see https://blog.tartanllama.xyz/structured-bindings/
// And see https://github.com/nlohmann/json/pull/1391
template <std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
{
return i.key();
}
// Structured Bindings Support
// For further reference see https://blog.tartanllama.xyz/structured-bindings/
// And see https://github.com/nlohmann/json/pull/1391
template <std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
{
return i.value();
}
} // namespace detail } // namespace detail
} // namespace nlohmann } // namespace nlohmann
// The Addition to the STD Namespace is required to add
// Structured Bindings Support to the iteration_proxy_value class
// For further reference see https://blog.tartanllama.xyz/structured-bindings/
// And see https://github.com/nlohmann/json/pull/1391
namespace std
{
template <typename IteratorType>
struct tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>>
: std::integral_constant<std::size_t, 2> {};
template <std::size_t N, typename IteratorType>
struct tuple_element <
N, ::nlohmann::detail::iteration_proxy_value<IteratorType >>
{
using type = decltype(
get<N>(std::declval <
::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
};
}

View file

@ -1649,26 +1649,24 @@ constexpr const auto& from_json = detail::static_const<detail::from_json_fn>::va
#include <cstddef> // size_t #include <cstddef> // size_t
#include <string> // string, to_string #include <string> // string, to_string
#include <iterator> // input_iterator_tag #include <iterator> // input_iterator_tag
#include <tuple> // tuple_size, get, tuple_element
// #include <nlohmann/detail/value_t.hpp> // #include <nlohmann/detail/value_t.hpp>
// #include <nlohmann/detail/meta/type_traits.hpp>
namespace nlohmann namespace nlohmann
{ {
namespace detail namespace detail
{ {
/// proxy class for the items() function template <typename IteratorType> class iteration_proxy_value
template<typename IteratorType> class iteration_proxy
{
private:
/// helper class for iteration
class iteration_proxy_internal
{ {
public: public:
using difference_type = std::ptrdiff_t; using difference_type = std::ptrdiff_t;
using value_type = iteration_proxy_internal; using value_type = iteration_proxy_value;
using pointer = iteration_proxy_internal*; using pointer = value_type * ;
using reference = iteration_proxy_internal&; using reference = value_type & ;
using iterator_category = std::input_iterator_tag; using iterator_category = std::input_iterator_tag;
private: private:
@ -1684,16 +1682,16 @@ template<typename IteratorType> class iteration_proxy
const std::string empty_str = ""; const std::string empty_str = "";
public: public:
explicit iteration_proxy_internal(IteratorType it) noexcept : anchor(it) {} explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {}
/// dereference operator (needed for range-based for) /// dereference operator (needed for range-based for)
iteration_proxy_internal& operator*() iteration_proxy_value& operator*()
{ {
return *this; return *this;
} }
/// increment operator (needed for range-based for) /// increment operator (needed for range-based for)
iteration_proxy_internal& operator++() iteration_proxy_value& operator++()
{ {
++anchor; ++anchor;
++array_index; ++array_index;
@ -1702,13 +1700,13 @@ template<typename IteratorType> class iteration_proxy
} }
/// equality operator (needed for InputIterator) /// equality operator (needed for InputIterator)
bool operator==(const iteration_proxy_internal& o) const noexcept bool operator==(const iteration_proxy_value& o) const noexcept
{ {
return anchor == o.anchor; return anchor == o.anchor;
} }
/// inequality operator (needed for range-based for) /// inequality operator (needed for range-based for)
bool operator!=(const iteration_proxy_internal& o) const noexcept bool operator!=(const iteration_proxy_value& o) const noexcept
{ {
return anchor != o.anchor; return anchor != o.anchor;
} }
@ -1748,6 +1746,10 @@ template<typename IteratorType> class iteration_proxy
} }
}; };
/// proxy class for the items() function
template<typename IteratorType> class iteration_proxy
{
private:
/// the container to iterate /// the container to iterate
typename IteratorType::reference container; typename IteratorType::reference container;
@ -1757,20 +1759,55 @@ template<typename IteratorType> class iteration_proxy
: container(cont) {} : container(cont) {}
/// return iterator begin (needed for range-based for) /// return iterator begin (needed for range-based for)
iteration_proxy_internal begin() noexcept iteration_proxy_value<IteratorType> begin() noexcept
{ {
return iteration_proxy_internal(container.begin()); return iteration_proxy_value<IteratorType>(container.begin());
} }
/// return iterator end (needed for range-based for) /// return iterator end (needed for range-based for)
iteration_proxy_internal end() noexcept iteration_proxy_value<IteratorType> end() noexcept
{ {
return iteration_proxy_internal(container.end()); return iteration_proxy_value<IteratorType>(container.end());
} }
}; };
// Structured Bindings Support
// For further reference see https://blog.tartanllama.xyz/structured-bindings/
// And see https://github.com/nlohmann/json/pull/1391
template <std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
{
return i.key();
}
// Structured Bindings Support
// For further reference see https://blog.tartanllama.xyz/structured-bindings/
// And see https://github.com/nlohmann/json/pull/1391
template <std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
{
return i.value();
}
} // namespace detail } // namespace detail
} // namespace nlohmann } // namespace nlohmann
// The Addition to the STD Namespace is required to add
// Structured Bindings Support to the iteration_proxy_value class
// For further reference see https://blog.tartanllama.xyz/structured-bindings/
// And see https://github.com/nlohmann/json/pull/1391
namespace std
{
template <typename IteratorType>
struct tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>>
: std::integral_constant<std::size_t, 2> {};
template <std::size_t N, typename IteratorType>
struct tuple_element <
N, ::nlohmann::detail::iteration_proxy_value<IteratorType >>
{
using type = decltype(
get<N>(std::declval <
::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
};
}
namespace nlohmann namespace nlohmann
{ {
@ -2064,7 +2101,7 @@ void to_json(BasicJsonType& j, const std::pair<Args...>& p)
// for https://github.com/nlohmann/json/pull/1134 // for https://github.com/nlohmann/json/pull/1134
template < typename BasicJsonType, typename T, template < typename BasicJsonType, typename T,
enable_if_t<std::is_same<T, typename iteration_proxy<typename BasicJsonType::iterator>::iteration_proxy_internal>::value, int> = 0> enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
void to_json(BasicJsonType& j, const T& b) void to_json(BasicJsonType& j, const T& b)
{ {
j = { {b.key(), b.value()} }; j = { {b.key(), b.value()} };
@ -5570,24 +5607,21 @@ namespace detail
{ {
// forward declare, to be able to friend it later on // forward declare, to be able to friend it later on
template<typename IteratorType> class iteration_proxy; template<typename IteratorType> class iteration_proxy;
template<typename IteratorType> class iteration_proxy_value;
/*! /*!
@brief a template for a bidirectional iterator for the @ref basic_json class @brief a template for a bidirectional iterator for the @ref basic_json class
This class implements a both iterators (iterator and const_iterator) for the This class implements a both iterators (iterator and const_iterator) for the
@ref basic_json class. @ref basic_json class.
@note An iterator is called *initialized* when a pointer to a JSON value has @note An iterator is called *initialized* when a pointer to a JSON value has
been set (e.g., by a constructor or a copy assignment). If the iterator is been set (e.g., by a constructor or a copy assignment). If the iterator is
default-constructed, it is *uninitialized* and most methods are undefined. default-constructed, it is *uninitialized* and most methods are undefined.
**The library uses assertions to detect calls on uninitialized iterators.** **The library uses assertions to detect calls on uninitialized iterators.**
@requirement The class satisfies the following concept requirements: @requirement The class satisfies the following concept requirements:
- -
[BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator): [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
The iterator that can be moved can be moved in both directions (i.e. The iterator that can be moved can be moved in both directions (i.e.
incremented and decremented). incremented and decremented).
@since version 1.0.0, simplified in version 2.0.9, change to bidirectional @since version 1.0.0, simplified in version 2.0.9, change to bidirectional
iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593) iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
*/ */
@ -5598,6 +5632,7 @@ class iter_impl
friend iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>; friend iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
friend BasicJsonType; friend BasicJsonType;
friend iteration_proxy<iter_impl>; friend iteration_proxy<iter_impl>;
friend iteration_proxy_value<iter_impl>;
using object_t = typename BasicJsonType::object_t; using object_t = typename BasicJsonType::object_t;
using array_t = typename BasicJsonType::array_t; using array_t = typename BasicJsonType::array_t;
@ -6165,7 +6200,6 @@ class iter_impl
}; };
} // namespace detail } // namespace detail
} // namespace nlohmann } // namespace nlohmann
// #include <nlohmann/detail/iterators/iteration_proxy.hpp> // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
// #include <nlohmann/detail/iterators/json_reverse_iterator.hpp> // #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>

View file

@ -3,21 +3,17 @@
__| | __| | | | JSON for Modern C++ (test suite) __| | __| | | | JSON for Modern C++ (test suite)
| | |__ | | | | | | version 3.4.0 | | |__ | | | | | | version 3.4.0
|_____|_____|_____|_|___| https://github.com/nlohmann/json |_____|_____|_____|_|___| https://github.com/nlohmann/json
Licensed under the MIT License <http://opensource.org/licenses/MIT>. Licensed under the MIT License <http://opensource.org/licenses/MIT>.
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Copyright (c) 2013-2018 Niels Lohmann <http://nlohmann.me>. Copyright (c) 2013-2018 Niels Lohmann <http://nlohmann.me>.
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@ -32,6 +28,13 @@ SOFTWARE.
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using nlohmann::json; using nlohmann::json;
#if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
#define JSON_HAS_CPP_17
#define JSON_HAS_CPP_14
#elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
#define JSON_HAS_CPP_14
#endif
TEST_CASE("iterator_wrapper") TEST_CASE("iterator_wrapper")
{ {
SECTION("object") SECTION("object")
@ -875,6 +878,22 @@ TEST_CASE("items()")
CHECK(counter == 3); CHECK(counter == 3);
} }
#ifdef JSON_HAS_CPP_17
SECTION("structured bindings")
{
json j = { {"A", 1}, {"B", 2} };
std::map<std::string, int> m;
for (auto const&[key, value] : j.items())
{
m.emplace(key, value);
}
CHECK(j.get<decltype(m)>() == m);
}
#endif
} }
SECTION("const object") SECTION("const object")