Lukas Zilka | df710db | 2018-02-27 12:44:09 +0100 | [diff] [blame] | 1 | /* |
Tony Mak | 6c4cc67 | 2018-09-17 11:48:50 +0100 | [diff] [blame] | 2 | * Copyright (C) 2018 The Android Open Source Project |
Lukas Zilka | df710db | 2018-02-27 12:44:09 +0100 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Tony Mak | 6c4cc67 | 2018-09-17 11:48:50 +0100 | [diff] [blame] | 17 | #ifndef LIBTEXTCLASSIFIER_UTILS_JAVA_SCOPED_GLOBAL_REF_H_ |
| 18 | #define LIBTEXTCLASSIFIER_UTILS_JAVA_SCOPED_GLOBAL_REF_H_ |
Lukas Zilka | df710db | 2018-02-27 12:44:09 +0100 | [diff] [blame] | 19 | |
| 20 | #include <jni.h> |
| 21 | #include <memory> |
| 22 | #include <type_traits> |
| 23 | |
Tony Mak | 6c4cc67 | 2018-09-17 11:48:50 +0100 | [diff] [blame] | 24 | #include "utils/base/logging.h" |
Lukas Zilka | df710db | 2018-02-27 12:44:09 +0100 | [diff] [blame] | 25 | |
Tony Mak | 6c4cc67 | 2018-09-17 11:48:50 +0100 | [diff] [blame] | 26 | namespace libtextclassifier3 { |
Lukas Zilka | df710db | 2018-02-27 12:44:09 +0100 | [diff] [blame] | 27 | |
| 28 | // A deleter to be used with std::unique_ptr to delete JNI global references. |
| 29 | class GlobalRefDeleter { |
| 30 | public: |
| 31 | GlobalRefDeleter() : jvm_(nullptr) {} |
| 32 | |
| 33 | // Style guide violating implicit constructor so that the GlobalRefDeleter |
| 34 | // is implicitly constructed from the second argument to ScopedGlobalRef. |
| 35 | GlobalRefDeleter(JavaVM* jvm) : jvm_(jvm) {} // NOLINT(runtime/explicit) |
| 36 | |
| 37 | GlobalRefDeleter(const GlobalRefDeleter& orig) = default; |
| 38 | |
| 39 | // Copy assignment to allow move semantics in ScopedGlobalRef. |
| 40 | GlobalRefDeleter& operator=(const GlobalRefDeleter& rhs) { |
Tony Mak | 6c4cc67 | 2018-09-17 11:48:50 +0100 | [diff] [blame] | 41 | TC3_CHECK_EQ(jvm_, rhs.jvm_); |
Lukas Zilka | df710db | 2018-02-27 12:44:09 +0100 | [diff] [blame] | 42 | return *this; |
| 43 | } |
| 44 | |
| 45 | // The delete operator. |
| 46 | void operator()(jobject object) const { |
| 47 | JNIEnv* env; |
| 48 | if (object != nullptr && jvm_ != nullptr && |
| 49 | JNI_OK == |
| 50 | jvm_->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_4)) { |
| 51 | env->DeleteGlobalRef(object); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | // The jvm_ stashed to use for deletion. |
| 57 | JavaVM* const jvm_; |
| 58 | }; |
| 59 | |
| 60 | // A smart pointer that deletes a JNI global reference when it goes out |
| 61 | // of scope. Usage is: |
| 62 | // ScopedGlobalRef<jobject> scoped_global(env->JniFunction(), jvm); |
| 63 | template <typename T> |
| 64 | using ScopedGlobalRef = |
| 65 | std::unique_ptr<typename std::remove_pointer<T>::type, GlobalRefDeleter>; |
| 66 | |
Tony Mak | 6c4cc67 | 2018-09-17 11:48:50 +0100 | [diff] [blame] | 67 | // A helper to create global references. Assumes the object has a local |
| 68 | // reference, which it deletes. |
Lukas Zilka | df710db | 2018-02-27 12:44:09 +0100 | [diff] [blame] | 69 | template <typename T> |
| 70 | ScopedGlobalRef<T> MakeGlobalRef(T object, JNIEnv* env, JavaVM* jvm) { |
Tony Mak | 6c4cc67 | 2018-09-17 11:48:50 +0100 | [diff] [blame] | 71 | const jobject global_object = env->NewGlobalRef(object); |
| 72 | env->DeleteLocalRef(object); |
| 73 | return ScopedGlobalRef<T>(reinterpret_cast<T>(global_object), jvm); |
Lukas Zilka | df710db | 2018-02-27 12:44:09 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Tony Mak | 6c4cc67 | 2018-09-17 11:48:50 +0100 | [diff] [blame] | 76 | } // namespace libtextclassifier3 |
Lukas Zilka | df710db | 2018-02-27 12:44:09 +0100 | [diff] [blame] | 77 | |
Tony Mak | 6c4cc67 | 2018-09-17 11:48:50 +0100 | [diff] [blame] | 78 | #endif // LIBTEXTCLASSIFIER_UTILS_JAVA_SCOPED_GLOBAL_REF_H_ |