Fix call of overloaded 'swap(...)' is ambiguous (fixes #1678)

This commit is contained in:
Benoit Blanchon
2021-11-23 10:47:31 +01:00
parent 599e927590
commit 2df1bc7d4f
6 changed files with 31 additions and 39 deletions

View File

@ -12,6 +12,7 @@ HEAD
* Add safe bool idiom in `JsonString`
* Remove `DeserializationError == bool` and `DeserializationError != bool`
* Fix `JsonVariant::memoryUsage()` for raw strings
* Fix `call of overloaded 'swap(BasicJsonDocument&, BasicJsonDocument&)' is ambiguous` (issue #1678)
v6.18.5 (2021-09-28)
-------

View File

@ -6,9 +6,9 @@
#include <stdlib.h> // malloc, free
#include <catch.hpp>
#include <sstream>
#include <utility>
using ARDUINOJSON_NAMESPACE::addPadding;
using ARDUINOJSON_NAMESPACE::move;
class SpyingAllocator {
public:
@ -78,7 +78,7 @@ TEST_CASE("BasicJsonDocument") {
BasicJsonDocument<SpyingAllocator> doc1(4096, log);
doc1.set(std::string("The size of this string is 32!!"));
BasicJsonDocument<SpyingAllocator> doc2(move(doc1));
BasicJsonDocument<SpyingAllocator> doc2(std::move(doc1));
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
REQUIRE(doc1.as<std::string>() == "null");
@ -111,7 +111,7 @@ TEST_CASE("BasicJsonDocument") {
doc1.set(std::string("The size of this string is 32!!"));
BasicJsonDocument<SpyingAllocator> doc2(8, log);
doc2 = move(doc1);
doc2 = std::move(doc1);
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
REQUIRE(doc1.as<std::string>() == "null");

View File

@ -19,6 +19,7 @@ add_executable(JsonDocumentTests
size.cpp
StaticJsonDocument.cpp
subscript.cpp
swap.cpp
)
add_test(JsonDocument JsonDocumentTests)

View File

@ -0,0 +1,13 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include <utility>
using namespace std;
TEST_CASE("std::swap") {
SECTION("DynamicJsonDocument*") {
DynamicJsonDocument *p1, *p2;
swap(p1, p2); // issue #1678
}
}

View File

@ -5,25 +5,30 @@
#pragma once
#include <ArduinoJson/Polyfills/type_traits.hpp>
#include <ArduinoJson/Polyfills/utility.hpp>
namespace ARDUINOJSON_NAMESPACE {
#if ARDUINOJSON_LITTLE_ENDIAN
inline void swapBytes(uint8_t &a, uint8_t &b) {
uint8_t t(a);
a = b;
b = t;
}
inline void fixEndianess(uint8_t *p, integral_constant<size_t, 8>) {
swap(p[0], p[7]);
swap(p[1], p[6]);
swap(p[2], p[5]);
swap(p[3], p[4]);
swapBytes(p[0], p[7]);
swapBytes(p[1], p[6]);
swapBytes(p[2], p[5]);
swapBytes(p[3], p[4]);
}
inline void fixEndianess(uint8_t *p, integral_constant<size_t, 4>) {
swap(p[0], p[3]);
swap(p[1], p[2]);
swapBytes(p[0], p[3]);
swapBytes(p[1], p[2]);
}
inline void fixEndianess(uint8_t *p, integral_constant<size_t, 2>) {
swap(p[0], p[1]);
swapBytes(p[0], p[1]);
}
inline void fixEndianess(uint8_t *, integral_constant<size_t, 1>) {}

View File

@ -1,28 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright Benoit Blanchon 2014-2021
// MIT License
#pragma once
#include "type_traits.hpp"
namespace ARDUINOJSON_NAMESPACE {
template <typename T>
inline void swap(T& a, T& b) {
T t(a);
a = b;
b = t;
}
#if ARDUINOJSON_HAS_RVALUE_REFERENCES
template <typename T>
typename remove_reference<T>::type&& move(T&& t) {
return static_cast<typename remove_reference<T>::type&&>(t);
}
#else
template <typename T>
T& move(T& t) {
return t;
}
#endif
} // namespace ARDUINOJSON_NAMESPACE