blob: de0608ef674813d8787369354bca74a896d422a2 [file] [log] [blame]
Lukas Zilkadf710db2018-02-27 12:44:09 +01001/*
Tony Mak6c4cc672018-09-17 11:48:50 +01002 * Copyright (C) 2018 The Android Open Source Project
Lukas Zilkadf710db2018-02-27 12:44:09 +01003 *
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 Mak6c4cc672018-09-17 11:48:50 +010017#ifndef LIBTEXTCLASSIFIER_UTILS_JAVA_SCOPED_GLOBAL_REF_H_
18#define LIBTEXTCLASSIFIER_UTILS_JAVA_SCOPED_GLOBAL_REF_H_
Lukas Zilkadf710db2018-02-27 12:44:09 +010019
20#include <jni.h>
21#include <memory>
22#include <type_traits>
23
Tony Mak6c4cc672018-09-17 11:48:50 +010024#include "utils/base/logging.h"
Lukas Zilkadf710db2018-02-27 12:44:09 +010025
Tony Mak6c4cc672018-09-17 11:48:50 +010026namespace libtextclassifier3 {
Lukas Zilkadf710db2018-02-27 12:44:09 +010027
28// A deleter to be used with std::unique_ptr to delete JNI global references.
29class 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 Mak6c4cc672018-09-17 11:48:50 +010041 TC3_CHECK_EQ(jvm_, rhs.jvm_);
Lukas Zilkadf710db2018-02-27 12:44:09 +010042 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);
63template <typename T>
64using ScopedGlobalRef =
65 std::unique_ptr<typename std::remove_pointer<T>::type, GlobalRefDeleter>;
66
Tony Mak6c4cc672018-09-17 11:48:50 +010067// A helper to create global references. Assumes the object has a local
68// reference, which it deletes.
Lukas Zilkadf710db2018-02-27 12:44:09 +010069template <typename T>
70ScopedGlobalRef<T> MakeGlobalRef(T object, JNIEnv* env, JavaVM* jvm) {
Tony Mak6c4cc672018-09-17 11:48:50 +010071 const jobject global_object = env->NewGlobalRef(object);
72 env->DeleteLocalRef(object);
73 return ScopedGlobalRef<T>(reinterpret_cast<T>(global_object), jvm);
Lukas Zilkadf710db2018-02-27 12:44:09 +010074}
75
Tony Mak6c4cc672018-09-17 11:48:50 +010076} // namespace libtextclassifier3
Lukas Zilkadf710db2018-02-27 12:44:09 +010077
Tony Mak6c4cc672018-09-17 11:48:50 +010078#endif // LIBTEXTCLASSIFIER_UTILS_JAVA_SCOPED_GLOBAL_REF_H_