#pragma once #include #include "../attach_env.hpp" namespace mbgl { namespace android { // A deleter that doesn't retain an JNIEnv handle but instead tries to attach the JVM. This means // it can be used on any thread to delete a global ref. struct GenericGlobalRefDeleter { void operator()(jni::jobject* p) const { if (p) { auto env = AttachEnv(); env->DeleteGlobalRef(jni::Unwrap(p)); } } }; template < class TagType > class GenericWeakObjectRefDeleter; template < class TagType = jni::ObjectTag > using GenericUniqueWeakObject = std::unique_ptr< const jni::Object, GenericWeakObjectRefDeleter >; template < class TagType > class GenericWeakObjectRefDeleter { public: using pointer = jni::PointerToValue< jni::Object >; void operator()(pointer p) const { if (p) { auto env = AttachEnv(); env->DeleteWeakGlobalRef(jni::Unwrap(p->Get())); } } }; template < class TagType > GenericUniqueWeakObject SeizeGenericWeakRef(JNIEnv&, jni::Object&& object) { return GenericUniqueWeakObject(jni::PointerToValue>(std::move(object)), GenericWeakObjectRefDeleter()); }; } // namespace android } // namespace mbgl