Added move-constructor and move-assignment to BasicJsonDocument

This commit is contained in:
Benoit Blanchon
2020-03-01 17:24:29 +01:00
parent 2641697e0b
commit 2540b4e01b
5 changed files with 98 additions and 2 deletions

View File

@ -13,9 +13,11 @@
#if __cplusplus >= 201103L
#define ARDUINOJSON_HAS_LONG_LONG 1
#define ARDUINOJSON_HAS_NULLPTR 1
#define ARDUINOJSON_HAS_RVALUE_REFERENCES 1
#else
#define ARDUINOJSON_HAS_LONG_LONG 0
#define ARDUINOJSON_HAS_NULLPTR 0
#define ARDUINOJSON_HAS_RVALUE_REFERENCES 0
#endif
// Small or big machine?

View File

@ -52,6 +52,14 @@ class BasicJsonDocument : AllocatorOwner<TAllocator>, public JsonDocument {
set(src);
}
#if ARDUINOJSON_HAS_RVALUE_REFERENCES
BasicJsonDocument(BasicJsonDocument&& src)
: AllocatorOwner<TAllocator>(src), JsonDocument(src) {
src._data.setNull();
src._pool = MemoryPool(0, 0);
}
#endif
// disambiguate
BasicJsonDocument(VariantRef src)
: JsonDocument(allocPool(src.memoryUsage())) {
@ -68,6 +76,17 @@ class BasicJsonDocument : AllocatorOwner<TAllocator>, public JsonDocument {
return *this;
}
#if ARDUINOJSON_HAS_RVALUE_REFERENCES
BasicJsonDocument& operator=(BasicJsonDocument&& src) {
freePool();
_data = src._data;
_pool = src._pool;
src._data.setNull();
src._pool = MemoryPool(0, 0);
return *this;
}
#endif
template <typename T>
BasicJsonDocument& operator=(const T& src) {
reallocPoolIfTooSmall(src.memoryUsage());

View File

@ -4,7 +4,7 @@
#pragma once
#include <ArduinoJson/Namespace.hpp>
#include "type_traits.hpp"
namespace ARDUINOJSON_NAMESPACE {
template <typename T>
@ -13,4 +13,16 @@ inline void swap(T& a, T& b) {
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