From dbe25a98620fa279d0245bc8d430af992b5f1077 Mon Sep 17 00:00:00 2001 From: Mikhail Pozdnyakov Date: Mon, 9 Sep 2019 18:35:23 +0300 Subject: [core] Use mapbox::base::TypeWrapper instead of utils::peer --- include/mbgl/style/layer.hpp | 4 +- include/mbgl/style/source.hpp | 4 +- include/mbgl/util/peer.hpp | 34 ----- platform/android/src/style/sources/source.cpp | 2 +- scripts/generate-file-lists.js | 3 +- src/core-files.json | 1 - test/test-files.json | 1 - test/util/peer.test.cpp | 189 -------------------------- vendor/mapbox-base | 2 +- vendor/mapbox-base-files.json | 1 + 10 files changed, 9 insertions(+), 232 deletions(-) delete mode 100644 include/mbgl/util/peer.hpp delete mode 100644 test/util/peer.test.cpp diff --git a/include/mbgl/style/layer.hpp b/include/mbgl/style/layer.hpp index cfaca60e60..ecd3f01f70 100644 --- a/include/mbgl/style/layer.hpp +++ b/include/mbgl/style/layer.hpp @@ -1,12 +1,12 @@ #pragma once -#include #include #include #include #include #include +#include #include #include @@ -121,7 +121,7 @@ public: // For use in SDK bindings, which store a reference to a platform-native peer // object here, so that separately-obtained references to this object share // identical platform-native peers. - util::peer peer; + mapbox::base::TypeWrapper peer; Layer(Immutable); const LayerTypeInfo* getTypeInfo() const noexcept; diff --git a/include/mbgl/style/source.hpp b/include/mbgl/style/source.hpp index 1d5223fa0d..2507b67fdc 100644 --- a/include/mbgl/style/source.hpp +++ b/include/mbgl/style/source.hpp @@ -2,11 +2,11 @@ #include #include -#include #include #include #include +#include #include #include @@ -79,7 +79,7 @@ public: // For use in SDK bindings, which store a reference to a platform-native peer // object here, so that separately-obtained references to this object share // identical platform-native peers. - util::peer peer; + mapbox::base::TypeWrapper peer; virtual mapbox::base::WeakPtr makeWeakPtr() = 0; }; diff --git a/include/mbgl/util/peer.hpp b/include/mbgl/util/peer.hpp deleted file mode 100644 index d16efddb57..0000000000 --- a/include/mbgl/util/peer.hpp +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace mbgl { -namespace util { - -class peer { -public: - peer() noexcept : storage(nullptr, noop_deleter) {} - - template - peer(T&& value) noexcept : storage(new std::decay_t(std::forward(value)), cast_deleter>) { - static_assert(!std::is_same>::value, "Peer must not wrap itself."); - } - - bool has_value() const noexcept { return static_cast(storage); } - - template - T& get() noexcept { return *reinterpret_cast(storage.get()); } - -private: - template - static void cast_deleter(void* ptr) noexcept { delete reinterpret_cast(ptr); } - static void noop_deleter(void*) noexcept {} - - using storage_t = std::unique_ptr; - storage_t storage; -}; - -} // namespace util -} // namespace mbgl diff --git a/platform/android/src/style/sources/source.cpp b/platform/android/src/style/sources/source.cpp index be4c0367fc..786e5cb586 100644 --- a/platform/android/src/style/sources/source.cpp +++ b/platform/android/src/style/sources/source.cpp @@ -133,7 +133,7 @@ namespace android { // Release the peer relationships. These will be re-established when the source is added to a map assert(ownedSource->peer.has_value()); ownedSource->peer.get>().release(); - ownedSource->peer = util::peer(); + ownedSource->peer = mapbox::base::TypeWrapper(); // Release the strong reference to the java peer assert(javaPeer); diff --git a/scripts/generate-file-lists.js b/scripts/generate-file-lists.js index 02d7136f12..d1f5e77585 100755 --- a/scripts/generate-file-lists.js +++ b/scripts/generate-file-lists.js @@ -144,7 +144,8 @@ generateFileList('vendor/mapbox-base-files.json', 'vendor/mapbox-base/mapbox/supercluster.hpp', 'vendor/mapbox-base/mapbox/geojson.hpp', 'vendor/mapbox-base/mapbox/jni.hpp', - 'vendor/mapbox-base/mapbox/weak' ], + 'vendor/mapbox-base/mapbox/weak', + 'vendor/mapbox-base/mapbox/typewrapper' ], vendorRegex, [ "include/*.hpp", "include/**/*.hpp", "include/**/*.h", "optional.hpp", ":!:include/jni/string_conversion.hpp" ]); generateFileList('vendor/polylabel-files.json', [ 'vendor/polylabel' ], vendorRegex, [ "include/**/*.hpp" ]); generateFileList('vendor/protozero-files.json', [ 'vendor/protozero' ], vendorRegex, [ "include/**/*.hpp" ]); diff --git a/src/core-files.json b/src/core-files.json index f2da5a3d05..628ab5e3a7 100644 --- a/src/core-files.json +++ b/src/core-files.json @@ -479,7 +479,6 @@ "mbgl/util/logging.hpp": "include/mbgl/util/logging.hpp", "mbgl/util/noncopyable.hpp": "include/mbgl/util/noncopyable.hpp", "mbgl/util/optional.hpp": "include/mbgl/util/optional.hpp", - "mbgl/util/peer.hpp": "include/mbgl/util/peer.hpp", "mbgl/util/platform.hpp": "include/mbgl/util/platform.hpp", "mbgl/util/premultiply.hpp": "include/mbgl/util/premultiply.hpp", "mbgl/util/projection.hpp": "include/mbgl/util/projection.hpp", diff --git a/test/test-files.json b/test/test-files.json index f5e4013029..b239ac5ad8 100644 --- a/test/test-files.json +++ b/test/test-files.json @@ -93,7 +93,6 @@ "test/util/merge_lines.test.cpp", "test/util/number_conversions.test.cpp", "test/util/offscreen_texture.test.cpp", - "test/util/peer.test.cpp", "test/util/position.test.cpp", "test/util/projection.test.cpp", "test/util/run_loop.test.cpp", diff --git a/test/util/peer.test.cpp b/test/util/peer.test.cpp deleted file mode 100644 index a808dd27d1..0000000000 --- a/test/util/peer.test.cpp +++ /dev/null @@ -1,189 +0,0 @@ -#include - -#include - -using namespace mbgl::util; - -class TestType { -public: - TestType() { - str[0] = 'a'; - } - - //Detect moves - TestType(TestType&& t): i1(t.i1+1), i2(t.i2+2) { - str[0] = t.str[0]+1; - } - - TestType(const TestType&) = delete; - TestType& operator=(const TestType&) = delete; - - int i1 = 0; - int i2 = 1; - char str[256]; -}; - -bool IsStackAllocated (const peer& a, const void* obj1) { - auto a_ptr = (uintptr_t)(&a); - auto obj = (uintptr_t)(obj1); - return (obj >= a_ptr && obj < a_ptr + sizeof(peer)); -}; - -TEST(Peer, Empty) { - EXPECT_FALSE(peer().has_value()); -} - -TEST(Peer, BasicTypes) { - peer i = 3; - EXPECT_TRUE(i.has_value()); - EXPECT_TRUE(i.get() == 3); - - auto iValue = i.get(); - EXPECT_TRUE(iValue == 3); - - EXPECT_TRUE(peer(4).has_value()); - EXPECT_TRUE(peer(4).get() == 4); - - peer f = 6.2f; - EXPECT_TRUE(f.has_value()); - EXPECT_TRUE(f.get() == 6.2f); - - const float fValue = f.get(); - EXPECT_TRUE(fValue == 6.2f); - - EXPECT_TRUE(peer(1.0f).has_value()); - EXPECT_TRUE(peer(1.0f).get() == 1.0f); - - peer c = 'z'; - EXPECT_TRUE(c.has_value()); - EXPECT_TRUE(c.get() == 'z'); - - EXPECT_TRUE(peer('z').has_value()); - EXPECT_TRUE(peer('z').get() == 'z'); -} - -TEST(Peer, BasicTypes_Move) { - peer i = 3; - EXPECT_TRUE(i.has_value()); - - peer f = 6.2f; - EXPECT_TRUE(f.has_value()); - - f = std::move(i); - EXPECT_FALSE(i.has_value()); - - EXPECT_TRUE(f.has_value()); - EXPECT_TRUE(f.get() == 3); -} - -TEST(Peer, SmallType) { - struct T { - T(int32_t* p_) : p(p_) { - (*p)++; - } - - T(T&& t) noexcept : p(t.p) { - (*p)++; - } - - ~T() { - (*p)--; - } - - T(const T&) = delete; - T& operator=(const T&) = delete; - - int32_t* p; - }; - - int32_t p = 0; - - { - peer u1 = peer(T(&p)); - EXPECT_EQ(p, 1); - - auto u2(std::move(u1)); - EXPECT_EQ(p, 1); - } - - EXPECT_EQ(p, 0); -} - -TEST(Peer, LargeType) { - TestType t1; - peer u1 = peer(std::move(t1)); - EXPECT_TRUE(u1.has_value()); - - //TestType should be moved into owning peer - EXPECT_EQ(u1.get().i1, 1); - - auto u2(std::move(u1)); - EXPECT_FALSE(u1.has_value()); - - //TestType should not be moved when owning peer is moved; - EXPECT_EQ(u2.get().i1, 1); - - // TestType should not be moved out of owning peer - auto& t2 = u2.get(); - EXPECT_TRUE(u2.has_value()); - EXPECT_EQ(t2.i1, 1); -} - -TEST(Peer, Pointer) { - auto t1 = new TestType(); - - auto u1 = peer(std::move(t1)); - EXPECT_TRUE(u1.has_value()); - - //Only the pointer should be moved - TestType * t2 = u1.get(); - EXPECT_EQ(t2->i1, 0); - - peer u2(4); - std::swap(u2, u1); - - EXPECT_TRUE(u1.has_value()); - - EXPECT_TRUE(u2.has_value()); - - t2 = u2.get(); - EXPECT_EQ(t2->i1, 0); - delete t2; -} - - -TEST(Peer, UniquePtr) { - auto t1 = std::make_unique(); - auto u1 = peer(std::move(t1)); - - EXPECT_EQ(t1.get(), nullptr); - EXPECT_TRUE(u1.has_value()); - - u1 = peer(); - EXPECT_FALSE(u1.has_value()); - - peer u2; - auto* t3 = new TestType(); - u2 = std::unique_ptr(t3); - EXPECT_TRUE(u2.has_value()); -} - -TEST(Peer, SharedPtr) { - - std::shared_ptr shared(new int(3)); - std::weak_ptr weak = shared; - peer u1 = 0; - - EXPECT_EQ(weak.use_count(), 1); - peer u2 = shared; - EXPECT_EQ(weak.use_count(), 2); - - u1 = std::move(u2); - EXPECT_EQ(weak.use_count(), 2); - std::swap(u2, u1); - EXPECT_EQ(weak.use_count(), 2); - u2 = 0; - EXPECT_EQ(weak.use_count(), 1); - shared = nullptr; - EXPECT_EQ(weak.use_count(), 0); -} diff --git a/vendor/mapbox-base b/vendor/mapbox-base index 51505b91b3..8ca46e8f8e 160000 --- a/vendor/mapbox-base +++ b/vendor/mapbox-base @@ -1 +1 @@ -Subproject commit 51505b91b34451b3bdb32d78f5feb75610c70ddd +Subproject commit 8ca46e8f8ebd212d7e55bd2b076b7ee42eaca5b5 diff --git a/vendor/mapbox-base-files.json b/vendor/mapbox-base-files.json index f27d90b7af..7c638d2b80 100644 --- a/vendor/mapbox-base-files.json +++ b/vendor/mapbox-base-files.json @@ -93,6 +93,7 @@ "optional.hpp": "vendor/mapbox-base/mapbox/optional/optional.hpp", "mapbox/pixelmatch.hpp": "vendor/mapbox-base/mapbox/pixelmatch-cpp/include/mapbox/pixelmatch.hpp", "supercluster.hpp": "vendor/mapbox-base/mapbox/supercluster.hpp/include/supercluster.hpp", + "mapbox/type_wrapper.hpp": "vendor/mapbox-base/mapbox/typewrapper/include/mapbox/type_wrapper.hpp", "mapbox/optional.hpp": "vendor/mapbox-base/mapbox/variant/include/mapbox/optional.hpp", "mapbox/recursive_wrapper.hpp": "vendor/mapbox-base/mapbox/variant/include/mapbox/recursive_wrapper.hpp", "mapbox/variant.hpp": "vendor/mapbox-base/mapbox/variant/include/mapbox/variant.hpp", -- cgit v1.2.1