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 #pragma once 18 19 #include <cstddef> 20 21 #include <jni.h> 22 23 #include "nativehelper_utils.h" 24 25 // A smart pointer that deletes a JNI local reference when it goes out of scope. 26 template<typename T> 27 class ScopedLocalRef { 28 public: ScopedLocalRef(JNIEnv * env,T localRef)29 ScopedLocalRef(JNIEnv* env, T localRef) : mEnv(env), mLocalRef(localRef) { 30 } 31 ScopedLocalRef(ScopedLocalRef && s)32 ScopedLocalRef(ScopedLocalRef&& s) noexcept : mEnv(s.mEnv), mLocalRef(s.release()) { 33 } 34 ScopedLocalRef(JNIEnv * env)35 explicit ScopedLocalRef(JNIEnv* env) : mEnv(env), mLocalRef(nullptr) { 36 } 37 ~ScopedLocalRef()38 ~ScopedLocalRef() { 39 reset(); 40 } 41 42 void reset(T ptr = nullptr) { 43 if (ptr != mLocalRef) { 44 if (mLocalRef != nullptr) { 45 mEnv->DeleteLocalRef(mLocalRef); 46 } 47 mLocalRef = ptr; 48 } 49 } 50 release()51 T release() __attribute__((warn_unused_result)) { 52 T localRef = mLocalRef; 53 mLocalRef = nullptr; 54 return localRef; 55 } 56 get()57 T get() const { 58 return mLocalRef; 59 } 60 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 { 76 return mLocalRef == nullptr; 77 } 78 79 // Allows "if (scoped_ref != nullptr)" 80 bool operator!=(std::nullptr_t) const { 81 return mLocalRef != nullptr; 82 } 83 84 private: 85 JNIEnv* mEnv; 86 T mLocalRef; 87 88 DISALLOW_COPY_AND_ASSIGN(ScopedLocalRef); 89 }; 90 91