Removed the copy-constructor of JsonDocument (issue #1189)

This commit is contained in:
Benoit Blanchon
2020-03-05 13:46:45 +01:00
parent 735bea1f47
commit 9cb0ddb5e7
4 changed files with 62 additions and 15 deletions

View File

@ -15,6 +15,7 @@ HEAD
* Added `BasicJsonDocument::garbageCollect()` (issue #1195) * Added `BasicJsonDocument::garbageCollect()` (issue #1195)
* Added `StaticJsonDocument::garbageCollect()` * Added `StaticJsonDocument::garbageCollect()`
* Changed copy-constructor of `BasicJsonDocument` to preserve the capacity of the source. * Changed copy-constructor of `BasicJsonDocument` to preserve the capacity of the source.
* Removed copy-constructor of `JsonDocument` (issue #1189)
> ### BREAKING CHANGES > ### BREAKING CHANGES
> >
@ -37,6 +38,23 @@ HEAD
> I made this change to get consistent results between copy-constructor and move-constructor, and whether RVO applies or not. > I made this change to get consistent results between copy-constructor and move-constructor, and whether RVO applies or not.
> >
> If you use the copy-constructor to optimize your documents, you can use `garbageCollect()` or `shrinkToFit()` instead. > If you use the copy-constructor to optimize your documents, you can use `garbageCollect()` or `shrinkToFit()` instead.
>
> #### Copy-constructor of `JsonDocument`
>
> In previous versions, it was possible to create a function that take a `JsonDocument` by value.
>
> ```c++
> void myFunction(JsonDocument doc) {}
> ```
>
> This function gives the wrong clues because it doesn't receive a copy of the `JsonDocument`, only a sliced version.
> It worked because the copy constructor copied the internal pointers, but it was an accident.
>
> From now, if you need to pass a `JsonDocument` to a function, you must use a reference:
>
> ```c++
> void myFunction(JsonDocument& doc) {}
> ```
v6.14.1 (2020-01-27) v6.14.1 (2020-01-27)
------- -------

View File

@ -20,23 +20,35 @@ set_target_properties(MiscTests PROPERTIES UNITY_BUILD OFF)
add_test(Misc MiscTests) add_test(Misc MiscTests)
macro(build_should_fail target)
add_executable(Issue978 set_target_properties(${target}
Issue978.cpp
)
set_target_properties(Issue978
PROPERTIES PROPERTIES
EXCLUDE_FROM_ALL TRUE EXCLUDE_FROM_ALL TRUE
EXCLUDE_FROM_DEFAULT_BUILD TRUE EXCLUDE_FROM_DEFAULT_BUILD TRUE
) )
add_test( add_test(
NAME NAME
Issue978 ${target}
COMMAND COMMAND
${CMAKE_COMMAND} --build . --target Issue978 --config $<CONFIGURATION> ${CMAKE_COMMAND} --build . --target ${target} --config $<CONFIGURATION>
WORKING_DIRECTORY WORKING_DIRECTORY
${CMAKE_BINARY_DIR} ${CMAKE_BINARY_DIR}
) )
set_tests_properties(Issue978 set_tests_properties(${target}
PROPERTIES PROPERTIES
WILL_FAIL TRUE) WILL_FAIL TRUE)
endmacro()
add_executable(Issue978
Issue978.cpp
)
build_should_fail(Issue978)
add_executable(Issue1189
Issue1189.cpp
)
build_should_fail(Issue1189)

View File

@ -0,0 +1,13 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2020
// MIT License
#include <ArduinoJson.h>
// a function should not be able to get a JsonDocument by value
void f(JsonDocument) {}
int main() {
DynamicJsonDocument doc(1024);
f(doc);
}

View File

@ -319,6 +319,10 @@ class JsonDocument : public Visitable {
MemoryPool _pool; MemoryPool _pool;
VariantData _data; VariantData _data;
private:
JsonDocument(const JsonDocument&);
JsonDocument& operator=(const JsonDocument&);
}; };
} // namespace ARDUINOJSON_NAMESPACE } // namespace ARDUINOJSON_NAMESPACE