From c43b1b1a1ad42619d50538e43a9410db5095d8e5 Mon Sep 17 00:00:00 2001 From: rachytski Date: Sat, 12 May 2012 20:40:58 +0400 Subject: [PATCH] helper function for managing global refs to jobject by shared_ptr. --- android/jni/com/mapswithme/core/jni_helper.cpp | 16 ++++++++++++++++ android/jni/com/mapswithme/core/jni_helper.hpp | 3 +++ 2 files changed, 19 insertions(+) diff --git a/android/jni/com/mapswithme/core/jni_helper.cpp b/android/jni/com/mapswithme/core/jni_helper.cpp index c0641c3bc2..b023b8aa39 100644 --- a/android/jni/com/mapswithme/core/jni_helper.cpp +++ b/android/jni/com/mapswithme/core/jni_helper.cpp @@ -73,4 +73,20 @@ namespace jni return g_jvm; } + struct global_ref_deleter + { + void operator()(jobject * ref) + { + jni::GetEnv()->DeleteGlobalRef(*ref); + delete ref; + } + }; + + shared_ptr make_global_ref(jobject obj) + { + jobject * ref = new jobject; + *ref = jni::GetEnv()->NewGlobalRef(obj); + return shared_ptr(ref, global_ref_deleter()); + } + } // namespace jni diff --git a/android/jni/com/mapswithme/core/jni_helper.hpp b/android/jni/com/mapswithme/core/jni_helper.hpp index bbc7fbd40d..48b33374dc 100644 --- a/android/jni/com/mapswithme/core/jni_helper.hpp +++ b/android/jni/com/mapswithme/core/jni_helper.hpp @@ -3,6 +3,7 @@ #include #include "../../../../../std/string.hpp" +#include "../../../../../std/shared_ptr.hpp" namespace jni { @@ -13,4 +14,6 @@ namespace jni string ToString(JNIEnv * env, jstring str); JNIEnv * GetEnv(); JavaVM * GetJVM(); + + shared_ptr make_global_ref(jobject obj); }