summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Shalamov <alexander.shalamov@mapbox.com>2019-04-05 17:22:55 +0300
committerAlexander Shalamov <alexander.shalamov@mapbox.com>2019-04-05 19:46:50 +0300
commit1721ce26f0d20f3a96254dc655adaf3260040a14 (patch)
tree69977c5b7b0a764d7b98709baa50d3df61bb8c50
parent5a173faec1aed27e08cdded48371cf9c7752b779 (diff)
downloadqtlocation-mapboxgl-upstream/alexshalamov_dont_delete_while_moving.tar.gz
[core] Move should not call destructorsupstream/alexshalamov_dont_delete_while_moving
-rw-r--r--include/mbgl/style/conversion_impl.hpp25
-rw-r--r--test/style/conversion/conversion_impl.test.cpp98
-rw-r--r--test/test-files.json1
3 files changed, 105 insertions, 19 deletions
diff --git a/include/mbgl/style/conversion_impl.hpp b/include/mbgl/style/conversion_impl.hpp
index 27b2ee1917..d61dd3d10b 100644
--- a/include/mbgl/style/conversion_impl.hpp
+++ b/include/mbgl/style/conversion_impl.hpp
@@ -89,29 +89,18 @@ public:
new (static_cast<void*>(&storage)) std::decay_t<T>(std::forward<T>(value));
}
- Convertible(Convertible&& v)
- : vtable(v.vtable)
- {
- if (vtable) {
- vtable->move(std::move(v.storage), this->storage);
- }
+ Convertible(Convertible&& v) : vtable(v.vtable) {
+ vtable->move(std::move(v.storage), storage);
}
~Convertible() {
- if (vtable) {
- vtable->destroy(storage);
- }
+ vtable->destroy(storage);
}
Convertible& operator=(Convertible&& v) {
- if (vtable) {
- vtable->destroy(storage);
- }
+ vtable->destroy(storage);
vtable = v.vtable;
- if (vtable) {
- vtable->move(std::move(v.storage), this->storage);
- }
- v.vtable = nullptr;
+ vtable->move(std::move(v.storage), storage);
return *this;
}
@@ -235,9 +224,7 @@ private:
using Traits = ConversionTraits<T>;
static VTable vtable = {
[] (Storage&& src, Storage& dest) {
- auto srcValue = reinterpret_cast<T&&>(src);
- new (static_cast<void*>(&dest)) T(std::move(srcValue));
- srcValue.~T();
+ new (static_cast<void*>(&dest)) T(reinterpret_cast<T&&>(src));
},
[] (Storage& s) {
reinterpret_cast<T&>(s).~T();
diff --git a/test/style/conversion/conversion_impl.test.cpp b/test/style/conversion/conversion_impl.test.cpp
new file mode 100644
index 0000000000..cb4f67095e
--- /dev/null
+++ b/test/style/conversion/conversion_impl.test.cpp
@@ -0,0 +1,98 @@
+#include <mbgl/test/util.hpp>
+#include <mbgl/style/conversion_impl.hpp>
+
+namespace mbgl {
+namespace style {
+namespace conversion {
+
+class MockConvertible {
+public:
+ MockConvertible() = default;
+ MockConvertible(int* counter_) : counter(counter_) {}
+
+ MockConvertible(MockConvertible&& other) noexcept : counter(other.counter) {}
+
+ ~MockConvertible() {
+ ++*counter;
+ }
+
+ int* counter = nullptr;
+};
+
+template <>
+class ConversionTraits<MockConvertible> {
+public:
+ static bool isUndefined(const MockConvertible&) {
+ return false;
+ }
+
+ static bool isArray(const MockConvertible&) {
+ return false;
+ }
+
+ static bool isObject(const MockConvertible&) {
+ return false;
+ }
+
+ static std::size_t arrayLength(const MockConvertible&) {
+ return 0u;
+ }
+
+ static MockConvertible arrayMember(const MockConvertible&, std::size_t) {
+ return {};
+ }
+
+ static optional<MockConvertible> objectMember(const MockConvertible&, const char *) {
+ return nullopt;
+ }
+
+ template <class Fn>
+ static optional<Error> eachMember(const MockConvertible&, Fn&&) {
+ return nullopt;
+ }
+
+ static optional<bool> toBool(const MockConvertible&) {
+ return nullopt;
+ }
+
+ static optional<float> toNumber(const MockConvertible&) {
+ return nullopt;
+ }
+
+ static optional<double> toDouble(const MockConvertible&) {
+ return nullopt;
+ }
+
+ static optional<std::string> toString(const MockConvertible&) {
+ return nullopt;
+ }
+
+ static optional<mbgl::Value> toValue(const MockConvertible&) {
+ return nullopt;
+ }
+
+ static optional<GeoJSON> toGeoJSON(const MockConvertible&, Error&) {
+ return nullopt;
+ }
+};
+
+} // namespace conversion
+} // namespace style
+} // namespace mbgl
+
+using namespace mbgl;
+using namespace mbgl::style;
+using namespace mbgl::style::conversion;
+
+TEST(Conversion, Move) {
+ int dtorCounter = 0;
+
+ {
+ MockConvertible mock(&dtorCounter);
+ Convertible a(std::move(mock));
+ Convertible b(std::move(a));
+ a = std::move(b);
+ }
+
+ ASSERT_EQ(dtorCounter, 4);
+}
diff --git a/test/test-files.json b/test/test-files.json
index 58970689be..0ba0a07577 100644
--- a/test/test-files.json
+++ b/test/test-files.json
@@ -49,6 +49,7 @@
"test/storage/online_file_source.test.cpp",
"test/storage/resource.test.cpp",
"test/storage/sqlite.test.cpp",
+ "test/style/conversion/conversion_impl.test.cpp",
"test/style/conversion/function.test.cpp",
"test/style/conversion/geojson_options.test.cpp",
"test/style/conversion/layer.test.cpp",