Skip to content

Commit

Permalink
test 1
Browse files Browse the repository at this point in the history
  • Loading branch information
falbrechtskirchinger committed Apr 24, 2022
1 parent 3f5ec72 commit 59d8fd5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 62 deletions.
14 changes: 10 additions & 4 deletions include/nlohmann/detail/meta/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,12 +508,18 @@ template<typename T>
using detect_is_transparent = typename T::is_transparent;

// type trait to check if KeyType can be used as object key
// Modify refers to the context in which KeyType is used;
// essentially only true for operator[]
template<typename BasicJsonType, typename KeyType, bool Modify = false, typename UnqualKeyType = uncvref_t<KeyType>>
// true if:
// - KeyType is comparable with BasicJsonType::object_t::key_type
// - if ExcludeObjectKeyType is true, KeyType is not BasicJsonType::object_t::key_type
// - the comparator is transparent or RequireTransparentComparator is false
// - KeyType is not a JSON iterator or json_pointer
template<typename BasicJsonType, typename KeyType, bool RequireTransparentComparator = true,
bool ExcludeObjectKeyType = RequireTransparentComparator, typename UnqualKeyType = uncvref_t<KeyType>>
using is_usable_as_key_type = typename std::conditional <
is_key_type_comparable<BasicJsonType, KeyType>::value
&& (Modify || is_detected <
&& !(ExcludeObjectKeyType && std::is_same<UnqualKeyType,
typename BasicJsonType::object_t::key_type>::value)
&& (!RequireTransparentComparator || is_detected <
detect_is_transparent,
typename BasicJsonType::object_comparator_t >::value)
&& !is_json_iterator_of<BasicJsonType, UnqualKeyType>::value
Expand Down
45 changes: 18 additions & 27 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2022,9 +2022,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief access specified object element with bounds checking
/// @sa https://json.nlohmann.me/api/basic_json/at/
template < class KeyType, detail::enable_if_t <
detail::is_usable_as_key_type < basic_json_t, KeyType >::value
&& !detail::is_json_pointer<KeyType>::value, int > = 0 >
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
reference at(KeyType && key)
{
// at only works for objects
Expand Down Expand Up @@ -2061,9 +2060,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief access specified object element with bounds checking
/// @sa https://json.nlohmann.me/api/basic_json/at/
template < class KeyType, detail::enable_if_t <
detail::is_usable_as_key_type<basic_json_t, KeyType>::value
&& !detail::is_json_pointer<KeyType>::value, int > = 0 >
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
const_reference at(KeyType && key) const
{
// at only works for objects
Expand Down Expand Up @@ -2178,9 +2176,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief access specified object element
/// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
template < class KeyType, detail::enable_if_t <
detail::is_usable_as_key_type<basic_json_t, KeyType, true>::value
&& !detail::is_json_pointer<KeyType>::value, int > = 0 >
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<basic_json_t, KeyType, true>::value, int> = 0>
reference operator[](KeyType && key)
{
// implicitly convert null value to an empty object
Expand All @@ -2203,9 +2200,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief access specified object element
/// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
template < class KeyType, detail::enable_if_t <
detail::is_usable_as_key_type<basic_json_t, KeyType, true>::value
&& !detail::is_json_pointer<KeyType>::value, int > = 0 >
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<basic_json_t, KeyType, true>::value, int> = 0>
const_reference operator[](KeyType && key) const
{
// const operator[] only works for objects
Expand Down Expand Up @@ -2523,9 +2519,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief remove element from a JSON object given a key
/// @sa https://json.nlohmann.me/api/basic_json/erase/
template < class KeyType, detail::enable_if_t <
detail::is_usable_as_key_type<basic_json_t, KeyType>::value
&& !detail::is_json_pointer<KeyType>::value, int > = 0 >
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
size_type erase(KeyType && key)
{
return erase_internal(std::forward<KeyType>(key));
Expand Down Expand Up @@ -2575,9 +2570,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
return result;
}

template < class KeyType, detail::enable_if_t <
detail::is_usable_as_key_type<basic_json_t, KeyType>::value
&& !detail::is_json_pointer<KeyType>::value, int > = 0 >
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
iterator find(KeyType && key)
{
auto result = end();
Expand Down Expand Up @@ -2606,9 +2600,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief find an element in a JSON object
/// @sa https://json.nlohmann.me/api/basic_json/find/
template < class KeyType, detail::enable_if_t <
detail::is_usable_as_key_type<basic_json_t, KeyType>::value
&& !detail::is_json_pointer<KeyType>::value, int > = 0 >
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
const_iterator find(KeyType && key) const
{
auto result = cend();
Expand All @@ -2631,9 +2624,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief returns the number of occurrences of a key in a JSON object
/// @sa https://json.nlohmann.me/api/basic_json/count/
template < class KeyType, detail::enable_if_t <
detail::is_usable_as_key_type<basic_json_t, KeyType>::value
&& !detail::is_json_pointer<KeyType>::value, int > = 0 >
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
size_type count(KeyType && key) const
{
// return 0 for all nonobject types
Expand All @@ -2649,9 +2641,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief check the existence of an element in a JSON object
/// @sa https://json.nlohmann.me/api/basic_json/contains/
template < class KeyType, detail::enable_if_t <
detail::is_usable_as_key_type<basic_json_t, KeyType>::value
&& !detail::is_json_pointer<KeyType>::value, int > = 0 >
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
bool contains(KeyType && key) const
{
return is_object() && m_value.object->find(std::forward<KeyType>(key)) != m_value.object->end();
Expand Down
59 changes: 28 additions & 31 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3645,12 +3645,18 @@ template<typename T>
using detect_is_transparent = typename T::is_transparent;

// type trait to check if KeyType can be used as object key
// Modify refers to the context in which KeyType is used;
// essentially only true for operator[]
template<typename BasicJsonType, typename KeyType, bool Modify = false, typename UnqualKeyType = uncvref_t<KeyType>>
// true if:
// - KeyType is comparable with BasicJsonType::object_t::key_type
// - if ExcludeObjectKeyType is true, KeyType is not BasicJsonType::object_t::key_type
// - the comparator is transparent or RequireTransparentComparator is false
// - KeyType is not a JSON iterator or json_pointer
template<typename BasicJsonType, typename KeyType, bool RequireTransparentComparator = true,
bool ExcludeObjectKeyType = RequireTransparentComparator, typename UnqualKeyType = uncvref_t<KeyType>>
using is_usable_as_key_type = typename std::conditional <
is_key_type_comparable<BasicJsonType, KeyType>::value
&& (Modify || is_detected <
&& !(ExcludeObjectKeyType && std::is_same<UnqualKeyType,
typename BasicJsonType::object_t::key_type>::value)
&& (!RequireTransparentComparator || is_detected <
detect_is_transparent,
typename BasicJsonType::object_comparator_t >::value)
&& !is_json_iterator_of<BasicJsonType, UnqualKeyType>::value
Expand Down Expand Up @@ -19564,9 +19570,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief access specified object element with bounds checking
/// @sa https://json.nlohmann.me/api/basic_json/at/
template < class KeyType, detail::enable_if_t <
detail::is_usable_as_key_type < basic_json_t, KeyType >::value
&& !detail::is_json_pointer<KeyType>::value, int > = 0 >
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
reference at(KeyType && key)
{
// at only works for objects
Expand Down Expand Up @@ -19603,9 +19608,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief access specified object element with bounds checking
/// @sa https://json.nlohmann.me/api/basic_json/at/
template < class KeyType, detail::enable_if_t <
detail::is_usable_as_key_type<basic_json_t, KeyType>::value
&& !detail::is_json_pointer<KeyType>::value, int > = 0 >
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
const_reference at(KeyType && key) const
{
// at only works for objects
Expand Down Expand Up @@ -19720,9 +19724,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief access specified object element
/// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
template < class KeyType, detail::enable_if_t <
detail::is_usable_as_key_type<basic_json_t, KeyType, true>::value
&& !detail::is_json_pointer<KeyType>::value, int > = 0 >
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<basic_json_t, KeyType, true>::value, int> = 0>
reference operator[](KeyType && key)
{
// implicitly convert null value to an empty object
Expand All @@ -19745,9 +19748,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief access specified object element
/// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
template < class KeyType, detail::enable_if_t <
detail::is_usable_as_key_type<basic_json_t, KeyType, true>::value
&& !detail::is_json_pointer<KeyType>::value, int > = 0 >
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<basic_json_t, KeyType, true>::value, int> = 0>
const_reference operator[](KeyType && key) const
{
// const operator[] only works for objects
Expand Down Expand Up @@ -20065,9 +20067,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief remove element from a JSON object given a key
/// @sa https://json.nlohmann.me/api/basic_json/erase/
template < class KeyType, detail::enable_if_t <
detail::is_usable_as_key_type<basic_json_t, KeyType>::value
&& !detail::is_json_pointer<KeyType>::value, int > = 0 >
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
size_type erase(KeyType && key)
{
return erase_internal(std::forward<KeyType>(key));
Expand Down Expand Up @@ -20117,9 +20118,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
return result;
}

template < class KeyType, detail::enable_if_t <
detail::is_usable_as_key_type<basic_json_t, KeyType>::value
&& !detail::is_json_pointer<KeyType>::value, int > = 0 >
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
iterator find(KeyType && key)
{
auto result = end();
Expand Down Expand Up @@ -20148,9 +20148,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief find an element in a JSON object
/// @sa https://json.nlohmann.me/api/basic_json/find/
template < class KeyType, detail::enable_if_t <
detail::is_usable_as_key_type<basic_json_t, KeyType>::value
&& !detail::is_json_pointer<KeyType>::value, int > = 0 >
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
const_iterator find(KeyType && key) const
{
auto result = cend();
Expand All @@ -20173,9 +20172,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief returns the number of occurrences of a key in a JSON object
/// @sa https://json.nlohmann.me/api/basic_json/count/
template < class KeyType, detail::enable_if_t <
detail::is_usable_as_key_type<basic_json_t, KeyType>::value
&& !detail::is_json_pointer<KeyType>::value, int > = 0 >
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
size_type count(KeyType && key) const
{
// return 0 for all nonobject types
Expand All @@ -20191,9 +20189,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief check the existence of an element in a JSON object
/// @sa https://json.nlohmann.me/api/basic_json/contains/
template < class KeyType, detail::enable_if_t <
detail::is_usable_as_key_type<basic_json_t, KeyType>::value
&& !detail::is_json_pointer<KeyType>::value, int > = 0 >
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
bool contains(KeyType && key) const
{
return is_object() && m_value.object->find(std::forward<KeyType>(key)) != m_value.object->end();
Expand Down
5 changes: 5 additions & 0 deletions test/src/unit-element_access2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,11 @@ TEST_CASE("element access 2")
CHECK_THROWS_WITH_AS(j_const_nonobject["foo"], "[json.exception.type_error.305] cannot use operator[] with a string argument with null", json::type_error&);
CHECK_THROWS_WITH_AS(j_const_nonobject[json::object_t::key_type("foo")], "[json.exception.type_error.305] cannot use operator[] with a string argument with null", json::type_error&);

//auto key = json::object_t::key_type("foo");
//CHECK_THROWS_WITH_AS(j_const_nonobject[key], "[json.exception.type_error.305] cannot use operator[] with a string argument with null", json::type_error&);



#ifdef JSON_HAS_CPP_17
CHECK_NOTHROW(j_nonobject2[std::string_view("foo")]);
CHECK_THROWS_WITH_AS(j_const_nonobject[std::string_view("foo")], "[json.exception.type_error.305] cannot use operator[] with a string argument with null", json::type_error&);
Expand Down

0 comments on commit 59d8fd5

Please sign in to comment.