Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use swap() by ADL #3609

Merged
merged 3 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3447,11 +3447,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// swap only works for arrays
if (JSON_HEDLEY_LIKELY(is_array()))
{
std::swap(*(m_value.array), other);
using std::swap;
swap(*(m_value.array), other);
falbrechtskirchinger marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
JSON_THROW(type_error::create(310, detail::concat("cannot use swap() with ", type_name()), this));
JSON_THROW(type_error::create(310, detail::concat("cannot use swap(array_t&) with ", type_name()), this));
}
}

Expand All @@ -3462,11 +3463,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// swap only works for objects
if (JSON_HEDLEY_LIKELY(is_object()))
{
std::swap(*(m_value.object), other);
using std::swap;
swap(*(m_value.object), other);
}
else
{
JSON_THROW(type_error::create(310, detail::concat("cannot use swap() with ", type_name()), this));
JSON_THROW(type_error::create(310, detail::concat("cannot use swap(object_t&) with ", type_name()), this));
}
}

Expand All @@ -3477,11 +3479,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// swap only works for strings
if (JSON_HEDLEY_LIKELY(is_string()))
{
std::swap(*(m_value.string), other);
using std::swap;
swap(*(m_value.string), other);
}
else
{
JSON_THROW(type_error::create(310, detail::concat("cannot use swap() with ", type_name()), this));
JSON_THROW(type_error::create(310, detail::concat("cannot use swap(string_t&) with ", type_name()), this));
}
}

Expand All @@ -3492,11 +3495,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// swap only works for strings
if (JSON_HEDLEY_LIKELY(is_binary()))
{
std::swap(*(m_value.binary), other);
using std::swap;
swap(*(m_value.binary), other);
}
else
{
JSON_THROW(type_error::create(310, detail::concat("cannot use swap() with ", type_name()), this));
JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t&) with ", type_name()), this));
}
}

Expand All @@ -3507,11 +3511,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// swap only works for strings
if (JSON_HEDLEY_LIKELY(is_binary()))
{
std::swap(*(m_value.binary), other);
using std::swap;
swap(*(m_value.binary), other);
}
else
{
JSON_THROW(type_error::create(310, detail::concat("cannot use swap() with ", type_name()), this));
JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t::container_type&) with ", type_name()), this));
}
}

Expand Down
25 changes: 15 additions & 10 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22274,11 +22274,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// swap only works for arrays
if (JSON_HEDLEY_LIKELY(is_array()))
{
std::swap(*(m_value.array), other);
using std::swap;
swap(*(m_value.array), other);
}
else
{
JSON_THROW(type_error::create(310, detail::concat("cannot use swap() with ", type_name()), this));
JSON_THROW(type_error::create(310, detail::concat("cannot use swap(array_t&) with ", type_name()), this));
}
}

Expand All @@ -22289,11 +22290,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// swap only works for objects
if (JSON_HEDLEY_LIKELY(is_object()))
{
std::swap(*(m_value.object), other);
using std::swap;
swap(*(m_value.object), other);
}
else
{
JSON_THROW(type_error::create(310, detail::concat("cannot use swap() with ", type_name()), this));
JSON_THROW(type_error::create(310, detail::concat("cannot use swap(object_t&) with ", type_name()), this));
}
}

Expand All @@ -22304,11 +22306,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// swap only works for strings
if (JSON_HEDLEY_LIKELY(is_string()))
{
std::swap(*(m_value.string), other);
using std::swap;
swap(*(m_value.string), other);
}
else
{
JSON_THROW(type_error::create(310, detail::concat("cannot use swap() with ", type_name()), this));
JSON_THROW(type_error::create(310, detail::concat("cannot use swap(string_t&) with ", type_name()), this));
}
}

Expand All @@ -22319,11 +22322,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// swap only works for strings
if (JSON_HEDLEY_LIKELY(is_binary()))
{
std::swap(*(m_value.binary), other);
using std::swap;
swap(*(m_value.binary), other);
}
else
{
JSON_THROW(type_error::create(310, detail::concat("cannot use swap() with ", type_name()), this));
JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t&) with ", type_name()), this));
}
}

Expand All @@ -22334,11 +22338,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// swap only works for strings
if (JSON_HEDLEY_LIKELY(is_binary()))
{
std::swap(*(m_value.binary), other);
using std::swap;
swap(*(m_value.binary), other);
}
else
{
JSON_THROW(type_error::create(310, detail::concat("cannot use swap() with ", type_name()), this));
JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t::container_type&) with ", type_name()), this));
}
}

Expand Down
10 changes: 5 additions & 5 deletions tests/src/unit-modifiers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ TEST_CASE("modifiers")
json j = 17;
json::array_t a = {"foo", "bar", "baz"};

CHECK_THROWS_WITH_AS(j.swap(a), "[json.exception.type_error.310] cannot use swap() with number", json::type_error&);
CHECK_THROWS_WITH_AS(j.swap(a), "[json.exception.type_error.310] cannot use swap(array_t&) with number", json::type_error&);
}
}

Expand All @@ -880,7 +880,7 @@ TEST_CASE("modifiers")
json j = 17;
json::object_t o = {{"cow", "Kuh"}, {"chicken", "Huhn"}};

CHECK_THROWS_WITH_AS(j.swap(o), "[json.exception.type_error.310] cannot use swap() with number", json::type_error&);
CHECK_THROWS_WITH_AS(j.swap(o), "[json.exception.type_error.310] cannot use swap(object_t&) with number", json::type_error&);
}
}

Expand All @@ -905,7 +905,7 @@ TEST_CASE("modifiers")
json j = 17;
json::string_t s = "Hallo Welt";

CHECK_THROWS_WITH_AS(j.swap(s), "[json.exception.type_error.310] cannot use swap() with number", json::type_error&);
CHECK_THROWS_WITH_AS(j.swap(s), "[json.exception.type_error.310] cannot use swap(string_t&) with number", json::type_error&);
}
}

Expand Down Expand Up @@ -945,8 +945,8 @@ TEST_CASE("modifiers")
json::binary_t s1 = {{1, 2, 3, 4}};
std::vector<std::uint8_t> s2 = {{5, 6, 7, 8}};

CHECK_THROWS_WITH_AS(j.swap(s1), "[json.exception.type_error.310] cannot use swap() with number", json::type_error);
CHECK_THROWS_WITH_AS(j.swap(s2), "[json.exception.type_error.310] cannot use swap() with number", json::type_error);
CHECK_THROWS_WITH_AS(j.swap(s1), "[json.exception.type_error.310] cannot use swap(binary_t&) with number", json::type_error);
CHECK_THROWS_WITH_AS(j.swap(s2), "[json.exception.type_error.310] cannot use swap(binary_t::container_type&) with number", json::type_error);
}
}
}
Expand Down