1 /* 2 * Copyright (C) 2010 The Android Open Source Project 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 17 #ifndef SRC_ANDROID_SDK_NATIVEHELPER_SCOPED_LOCAL_REF_H_ 18 #define SRC_ANDROID_SDK_NATIVEHELPER_SCOPED_LOCAL_REF_H_ 19 20 // Copied from 21 // https://cs.android.com/android/platform/superproject/main/+/main:libnativehelper/header_only_include/nativehelper/scoped_local_ref.h;drc=4be05051ef76b2c24d8385732a892401eb45d911 22 23 #include <cstddef> 24 25 #include <jni.h> 26 27 #include "src/android_sdk/nativehelper/nativehelper_utils.h" 28 29 // A smart pointer that deletes a JNI local reference when it goes out of scope. 30 // 31 // For creating a `ScopedLocalRef<jstring>`, consider using 32 // `CREATE_UTF_OR_RETURN`. 33 template <typename T> 34 class ScopedLocalRef { 35 public: ScopedLocalRef(JNIEnv * env,T localRef)36 ScopedLocalRef(JNIEnv* env, T localRef) : mEnv(env), mLocalRef(localRef) {} 37 ScopedLocalRef(ScopedLocalRef && s)38 ScopedLocalRef(ScopedLocalRef&& s) noexcept 39 : mEnv(s.mEnv), mLocalRef(s.release()) {} 40 ScopedLocalRef(JNIEnv * env)41 explicit ScopedLocalRef(JNIEnv* env) : mEnv(env), mLocalRef(nullptr) {} 42 ~ScopedLocalRef()43 ~ScopedLocalRef() { reset(); } 44 45 void reset(T ptr = nullptr) { 46 if (ptr != mLocalRef) { 47 if (mLocalRef != nullptr) { 48 mEnv->DeleteLocalRef(mLocalRef); 49 } 50 mLocalRef = ptr; 51 } 52 } 53 release()54 T release() __attribute__((warn_unused_result)) { 55 T localRef = mLocalRef; 56 mLocalRef = nullptr; 57 return localRef; 58 } 59 get()60 T get() const { return mLocalRef; } 61 62 // We do not expose an empty constructor as it can easily lead to errors 63 // using common idioms, e.g.: 64 // ScopedLocalRef<...> ref; 65 // ref.reset(...); 66 67 // Move assignment operator. 68 ScopedLocalRef& operator=(ScopedLocalRef&& s) noexcept { 69 reset(s.release()); 70 mEnv = s.mEnv; 71 return *this; 72 } 73 74 // Allows "if (scoped_ref == nullptr)" 75 bool operator==(std::nullptr_t) const { return mLocalRef == nullptr; } 76 77 // Allows "if (scoped_ref != nullptr)" 78 bool operator!=(std::nullptr_t) const { return mLocalRef != nullptr; } 79 80 private: 81 JNIEnv* mEnv; 82 T mLocalRef; 83 84 DISALLOW_COPY_AND_ASSIGN(ScopedLocalRef); 85 }; 86 87 #endif // SRC_ANDROID_SDK_NATIVEHELPER_SCOPED_LOCAL_REF_H_ 88