summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2017-01-17 13:25:27 +0100
committerKonstantin Käfer <mail@kkaefer.com>2017-01-17 13:25:27 +0100
commit7e0cc3b545a5b47a8770e55e5476e9646809a618 (patch)
treea57127d2f1e8ed14506f47f54e44f1e3028d8214
parentea273d4e75f51a6cb7a6fc7573c7722e34e730f1 (diff)
downloadqtlocation-mapboxgl-upstream/7743-nativepeer-defaultfilesource.tar.gz
[android] introduce generic Peer object for JNI wrappingupstream/7743-nativepeer-defaultfilesource
-rw-r--r--platform/android/config.cmake1
-rwxr-xr-xplatform/android/src/jni.cpp8
-rw-r--r--platform/android/src/jni/peer.hpp40
-rwxr-xr-xplatform/android/src/native_map_view.cpp9
-rwxr-xr-xplatform/android/src/native_map_view.hpp9
-rw-r--r--platform/android/src/storage/default_file_source_peer.cpp84
-rw-r--r--platform/android/src/storage/default_file_source_peer.hpp40
7 files changed, 97 insertions, 94 deletions
diff --git a/platform/android/config.cmake b/platform/android/config.cmake
index 61669d48a7..8a8b5cb768 100644
--- a/platform/android/config.cmake
+++ b/platform/android/config.cmake
@@ -91,7 +91,6 @@ macro(mbgl_platform_core)
# Storage
PRIVATE platform/android/src/storage/default_file_source_peer.cpp
- PRIVATE platform/android/src/storage/default_file_source_peer.hpp
# Conversion C++ -> Java
platform/android/src/conversion/constant.hpp
diff --git a/platform/android/src/jni.cpp b/platform/android/src/jni.cpp
index d136766455..a4631997ad 100755
--- a/platform/android/src/jni.cpp
+++ b/platform/android/src/jni.cpp
@@ -9,12 +9,12 @@
#include <sys/system_properties.h>
#include "jni.hpp"
+#include "jni/peer.hpp"
#include "java_types.hpp"
#include "native_map_view.hpp"
#include "connectivity_listener.hpp"
#include "style/layers/layers.hpp"
#include "style/sources/sources.hpp"
-#include "storage/default_file_source_peer.hpp"
#include "conversion/conversion.hpp"
#include "conversion/collection.hpp"
@@ -298,7 +298,7 @@ namespace {
using namespace mbgl::android;
using DebugOptions = mbgl::MapDebugOptions;
-jlong nativeCreate(JNIEnv *env, jni::jobject* obj, jni::Object<DefaultFileSourcePeer> fileSource, jfloat pixelRatio, jint availableProcessors, jlong totalMemory) {
+jlong nativeCreate(JNIEnv *env, jni::jobject* obj, jni::Object<Peer<mbgl::DefaultFileSource>> fileSource, jfloat pixelRatio, jint availableProcessors, jlong totalMemory) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeCreate");
return reinterpret_cast<jlong>(new NativeMapView(env, jni::Unwrap(obj), fileSource, pixelRatio, availableProcessors, totalMemory));
}
@@ -1705,9 +1705,7 @@ void registerNatives(JavaVM *vm) {
registerNativeLayers(env);
registerNativeSources(env);
ConnectivityListener::registerNative(env);
-
- mbgl::Log::Error(mbgl::Event::JNI, "registering DefaultFileSource peer");
- DefaultFileSourcePeer::registerNative(env);
+ Peer<DefaultFileSource>::RegisterNative(env);
latLngClass = &jni::FindClass(env, "com/mapbox/mapboxsdk/geometry/LatLng");
latLngClass = jni::NewGlobalRef(env, latLngClass).release();
diff --git a/platform/android/src/jni/peer.hpp b/platform/android/src/jni/peer.hpp
new file mode 100644
index 0000000000..2c16cce710
--- /dev/null
+++ b/platform/android/src/jni/peer.hpp
@@ -0,0 +1,40 @@
+#pragma once
+
+#include <jni/jni.hpp>
+
+#include <memory>
+
+namespace mbgl {
+namespace android {
+
+template <typename T>
+class Peer {
+public:
+ static constexpr auto Name();
+ static void RegisterNative(jni::JNIEnv&);
+
+ static T& Get(jni::JNIEnv& env, const jni::Object<Peer<T>>& obj) {
+ jni::Field<Peer, jlong> field{ env, javaClass, peer };
+ return *reinterpret_cast<Peer*>(obj.Get(env, field))->native;
+ }
+
+protected:
+ template <typename... Args>
+ Peer(Args&&... args) : native(std::make_unique<T>(std::forward<Args>(args)...)) {
+ }
+
+ const std::unique_ptr<T> native;
+
+private:
+ static const char* peer;
+ static jni::Class<Peer<T>> javaClass;
+};
+
+template <typename T>
+jni::Class<Peer<T>> Peer<T>::javaClass;
+
+template <typename T>
+const char* Peer<T>::peer = "peer";
+
+} // namespace android
+} // namespace mbgl
diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp
index 3e2013c0d3..df47c6588b 100755
--- a/platform/android/src/native_map_view.cpp
+++ b/platform/android/src/native_map_view.cpp
@@ -1,5 +1,6 @@
#include "native_map_view.hpp"
#include "jni.hpp"
+#include "jni/peer.hpp"
#include <cstdlib>
#include <ctime>
@@ -40,7 +41,7 @@ void log_egl_string(EGLDisplay display, EGLint name, const char *label) {
NativeMapView::NativeMapView(JNIEnv* env_,
jobject obj_,
- jni::Object<DefaultFileSourcePeer> fileSource_,
+ jni::Object<Peer<DefaultFileSource>> fileSource_,
float pixelRatio,
int availableProcessors_,
size_t totalMemory_)
@@ -65,13 +66,9 @@ NativeMapView::NativeMapView(JNIEnv* env_,
return;
}
- jni::Field<DefaultFileSourcePeer, jlong> peerField{ *env, DefaultFileSourcePeer::javaClass,
- "peer" };
- auto fileSourcePtr = reinterpret_cast<DefaultFileSourcePeer*>(fileSource->Get(*env, peerField));
-
map = std::make_unique<mbgl::Map>(
*this, mbgl::Size{ static_cast<uint32_t>(width), static_cast<uint32_t>(height) },
- pixelRatio, fileSourcePtr->getFileSource(), threadPool, MapMode::Continuous);
+ pixelRatio, Peer<DefaultFileSource>::Get(*env, *fileSource), threadPool, MapMode::Continuous);
float zoomFactor = map->getMaxZoom() - map->getMinZoom() + 1;
float cpuFactor = availableProcessors;
diff --git a/platform/android/src/native_map_view.hpp b/platform/android/src/native_map_view.hpp
index ea75ef7702..bc1306405f 100755
--- a/platform/android/src/native_map_view.hpp
+++ b/platform/android/src/native_map_view.hpp
@@ -6,7 +6,7 @@
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/default_thread_pool.hpp>
-#include "storage/default_file_source_peer.hpp"
+#include "jni/peer.hpp"
#include <string>
#include <jni.h>
@@ -14,13 +14,16 @@
#include <EGL/egl.h>
namespace mbgl {
+
+class DefaultFileSource;
+
namespace android {
class NativeMapView : public mbgl::View, public mbgl::Backend {
public:
NativeMapView(JNIEnv* env,
jobject obj,
- jni::Object<DefaultFileSourcePeer> fileSourcePeer,
+ jni::Object<Peer<DefaultFileSource>> fileSourcePeer,
float pixelRatio,
int availableProcessors,
size_t totalMemory);
@@ -101,7 +104,7 @@ private:
size_t totalMemory = 0;
// Ensure these are initialised last
- jni::UniqueObject<DefaultFileSourcePeer> fileSource;
+ jni::UniqueObject<Peer<DefaultFileSource>> fileSource;
mbgl::ThreadPool threadPool;
std::unique_ptr<mbgl::Map> map;
mbgl::EdgeInsets insets;
diff --git a/platform/android/src/storage/default_file_source_peer.cpp b/platform/android/src/storage/default_file_source_peer.cpp
index 6f8a36b78a..968ec1c652 100644
--- a/platform/android/src/storage/default_file_source_peer.cpp
+++ b/platform/android/src/storage/default_file_source_peer.cpp
@@ -1,53 +1,59 @@
-#include "default_file_source_peer.hpp"
-
+#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/default_file_source.hpp>
+#include "../jni/peer.hpp"
+
namespace mbgl {
namespace android {
-jni::Class<DefaultFileSourcePeer> DefaultFileSourcePeer::javaClass;
+template <>
+constexpr auto Peer<DefaultFileSource>::Name() {
+ return "com/mapbox/mapboxsdk/storage/DefaultFileSource";
+}
+
+class Impl : public Peer<DefaultFileSource> {
+public:
+ Impl(jni::JNIEnv& env,
+ jni::String cachePath,
+ jni::String assetRoot,
+ jni::jlong maximumCacheSize)
+ : Peer(jni::Make<std::string>(env, cachePath),
+ jni::Make<std::string>(env, assetRoot),
+ maximumCacheSize) {
+ }
+
+ void setAPIBaseURL(jni::JNIEnv& env, jni::String baseURL) {
+ native->setAPIBaseURL(jni::Make<std::string>(env, baseURL));
+ }
+
+ jni::String getAPIBaseURL(jni::JNIEnv& env) {
+ return jni::Make<jni::String>(env, native->getAPIBaseURL());
+ }
+
+ void setAccessToken(jni::JNIEnv& env, jni::String accessToken) {
+ native->setAccessToken(jni::Make<std::string>(env, accessToken));
+ }
+
+ jni::String getAccessToken(jni::JNIEnv& env) {
+ return jni::Make<jni::String>(env, native->getAccessToken());
+ }
+};
#define NATIVE_METHOD(MethodPtr, name) \
jni::MakeNativePeerMethod<decltype(MethodPtr), (MethodPtr)>(name)
-void DefaultFileSourcePeer::registerNative(jni::JNIEnv& env) {
- javaClass = *jni::Class<DefaultFileSourcePeer>::Find(env).NewGlobalRef(env).release();
+template <>
+void Peer<DefaultFileSource>::RegisterNative(jni::JNIEnv& env) {
+ javaClass = *jni::Class<Peer<DefaultFileSource>>::Find(env).NewGlobalRef(env).release();
- jni::RegisterNativePeer<DefaultFileSourcePeer>(
- env, javaClass, "peer",
- std::make_unique<DefaultFileSourcePeer, JNIEnv&, jni::String, jni::String, jni::jlong>,
+ jni::RegisterNativePeer<Impl>(
+ env, javaClass, peer,
+ std::make_unique<Impl, JNIEnv&, jni::String, jni::String, jni::jlong>,
"initialize", "finalize",
- NATIVE_METHOD(&DefaultFileSourcePeer::setAPIBaseURL, "setAPIBaseURL"),
- NATIVE_METHOD(&DefaultFileSourcePeer::getAPIBaseURL, "getAPIBaseURL"),
- NATIVE_METHOD(&DefaultFileSourcePeer::setAccessToken, "setAccessToken"),
- NATIVE_METHOD(&DefaultFileSourcePeer::getAccessToken, "getAccessToken"));
-}
-
-DefaultFileSourcePeer::DefaultFileSourcePeer(jni::JNIEnv& env,
- jni::String cachePath,
- jni::String assetRoot,
- jni::jlong maximumCacheSize)
- : native(std::make_unique<DefaultFileSource>(jni::Make<std::string>(env, cachePath),
- jni::Make<std::string>(env, assetRoot),
- maximumCacheSize)) {
-}
-
-DefaultFileSourcePeer::~DefaultFileSourcePeer() = default;
-
-void DefaultFileSourcePeer::setAPIBaseURL(jni::JNIEnv& env, jni::String baseURL) {
- native->setAPIBaseURL(jni::Make<std::string>(env, baseURL));
-}
-
-jni::String DefaultFileSourcePeer::getAPIBaseURL(jni::JNIEnv& env) {
- return jni::Make<jni::String>(env, native->getAPIBaseURL());
-}
-
-void DefaultFileSourcePeer::setAccessToken(jni::JNIEnv& env, jni::String accessToken) {
- native->setAccessToken(jni::Make<std::string>(env, accessToken));
-}
-
-jni::String DefaultFileSourcePeer::getAccessToken(jni::JNIEnv& env) {
- return jni::Make<jni::String>(env, native->getAccessToken());
+ NATIVE_METHOD(&Impl::setAPIBaseURL, "setAPIBaseURL"),
+ NATIVE_METHOD(&Impl::getAPIBaseURL, "getAPIBaseURL"),
+ NATIVE_METHOD(&Impl::setAccessToken, "setAccessToken"),
+ NATIVE_METHOD(&Impl::getAccessToken, "getAccessToken"));
}
} // namespace android
diff --git a/platform/android/src/storage/default_file_source_peer.hpp b/platform/android/src/storage/default_file_source_peer.hpp
deleted file mode 100644
index c0f60447b3..0000000000
--- a/platform/android/src/storage/default_file_source_peer.hpp
+++ /dev/null
@@ -1,40 +0,0 @@
-#pragma once
-
-#include <jni/jni.hpp>
-
-#include <memory>
-
-namespace mbgl {
-
-class DefaultFileSource;
-
-namespace android {
-
-class DefaultFileSourcePeer {
-public:
- static constexpr auto Name() { return "com/mapbox/mapboxsdk/storage/DefaultFileSource"; }
- static jni::Class<DefaultFileSourcePeer> javaClass;
- static void registerNative(jni::JNIEnv&);
-
- DefaultFileSourcePeer(jni::JNIEnv&,
- jni::String cachePath,
- jni::String assetRoot,
- jni::jlong maximumCacheSize);
- ~DefaultFileSourcePeer();
-
- void setAPIBaseURL(jni::JNIEnv&, jni::String);
- jni::String getAPIBaseURL(jni::JNIEnv&);
-
- void setAccessToken(jni::JNIEnv&, jni::String);
- jni::String getAccessToken(jni::JNIEnv&);
-
- DefaultFileSource& getFileSource() {
- return *native;
- }
-
-private:
- const std::unique_ptr<DefaultFileSource> native;
-};
-
-} // namespace android
-} // namespace mbgl