From 92761b3019264706b0dbe0750b44d0461abaf814 Mon Sep 17 00:00:00 2001 From: Mikhail Pozdnyakov Date: Wed, 27 Feb 2019 19:29:04 +0200 Subject: [android] Make Source wrappers ownership shared --- platform/android/src/style/sources/source.cpp | 18 +++++++++--------- platform/android/src/style/sources/source.hpp | 4 +++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/platform/android/src/style/sources/source.cpp b/platform/android/src/style/sources/source.cpp index e13f55aff1..c81ccd1522 100644 --- a/platform/android/src/style/sources/source.cpp +++ b/platform/android/src/style/sources/source.cpp @@ -34,17 +34,17 @@ namespace mbgl { namespace android { - static std::unique_ptr createSourcePeer(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend& frontend) { + static std::shared_ptr createSourcePeer(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend& frontend) { if (coreSource.is()) { - return std::make_unique(env, *coreSource.as(), frontend); + return std::shared_ptr(new VectorSource(env, *coreSource.as(), frontend)); } else if (coreSource.is()) { - return std::make_unique(env, *coreSource.as(), frontend); + return std::shared_ptr(new RasterSource(env, *coreSource.as(), frontend)); } else if (coreSource.is()) { - return std::make_unique(env, *coreSource.as(), frontend); + return std::shared_ptr(new GeoJSONSource(env, *coreSource.as(), frontend)); } else if (coreSource.is()) { - return std::make_unique(env, *coreSource.as(), frontend); + return std::shared_ptr(new ImageSource(env, *coreSource.as(), frontend)); } else { - return std::make_unique(env, coreSource, frontend); + return std::shared_ptr(new UnknownSource(env, coreSource, frontend)); } } @@ -52,7 +52,7 @@ namespace android { if (!coreSource.peer.has_value()) { coreSource.peer = createSourcePeer(env, coreSource, frontend); } - return coreSource.peer.get>()->javaPeer; + return coreSource.peer.get>()->javaPeer; } Source::Source(jni::JNIEnv& env, mbgl::style::Source& coreSource, const jni::Object& obj, AndroidRendererFrontend& frontend) @@ -103,7 +103,7 @@ namespace android { map.getStyle().addSource(std::move(ownedSource)); // Add peer to core source - source.peer = std::unique_ptr(this); + source.peer = shared_from_this(); // Add strong reference to java source javaPeer = jni::NewGlobal(env, obj); @@ -132,7 +132,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.get>().reset(); ownedSource->peer.reset(); // Release the strong reference to the java peer diff --git a/platform/android/src/style/sources/source.hpp b/platform/android/src/style/sources/source.hpp index 93b706425a..5e6365c2c1 100644 --- a/platform/android/src/style/sources/source.hpp +++ b/platform/android/src/style/sources/source.hpp @@ -12,8 +12,10 @@ namespace mbgl { namespace android { -class Source : private mbgl::util::noncopyable { +class Source : public std::enable_shared_from_this { public: + Source(const Source&) = delete; + Source& operator=(const Source&) = delete; static constexpr auto Name() { return "com/mapbox/mapboxsdk/style/sources/Source"; }; -- cgit v1.2.1