Andreas Gampe | d299c07 | 2017-10-17 10:50:00 -0700 | [diff] [blame] | 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 SCOPED_LOCAL_REF_H_ |
| 18 | #define SCOPED_LOCAL_REF_H_ |
| 19 | |
| 20 | #include <cstddef> |
| 21 | |
| 22 | #include "jni.h" |
| 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: |
| 29 | ScopedLocalRef(JNIEnv* env, T localRef) : mEnv(env), mLocalRef(localRef) { |
| 30 | } |
| 31 | |
| 32 | ~ScopedLocalRef() { |
| 33 | reset(); |
| 34 | } |
| 35 | |
| 36 | void reset(T ptr = NULL) { |
| 37 | if (ptr != mLocalRef) { |
| 38 | if (mLocalRef != NULL) { |
| 39 | mEnv->DeleteLocalRef(mLocalRef); |
| 40 | } |
| 41 | mLocalRef = ptr; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | T release() __attribute__((warn_unused_result)) { |
| 46 | T localRef = mLocalRef; |
| 47 | mLocalRef = NULL; |
| 48 | return localRef; |
| 49 | } |
| 50 | |
| 51 | T get() const { |
| 52 | return mLocalRef; |
| 53 | } |
| 54 | |
| 55 | // Some better C++11 support. |
| 56 | #if __cplusplus >= 201103L |
| 57 | // Move constructor. |
| 58 | ScopedLocalRef(ScopedLocalRef&& s) : mEnv(s.mEnv), mLocalRef(s.release()) { |
| 59 | } |
| 60 | |
| 61 | explicit ScopedLocalRef(JNIEnv* env) : mEnv(env), mLocalRef(nullptr) { |
| 62 | } |
| 63 | |
| 64 | // We do not expose an empty constructor as it can easily lead to errors |
| 65 | // using common idioms, e.g.: |
| 66 | // ScopedLocalRef<...> ref; |
| 67 | // ref.reset(...); |
| 68 | |
| 69 | // Move assignment operator. |
| 70 | ScopedLocalRef& operator=(ScopedLocalRef&& s) { |
| 71 | reset(s.release()); |
| 72 | mEnv = s.mEnv; |
| 73 | return *this; |
| 74 | } |
| 75 | |
| 76 | // Allows "if (scoped_ref == nullptr)" |
| 77 | bool operator==(std::nullptr_t) const { |
| 78 | return mLocalRef == nullptr; |
| 79 | } |
| 80 | |
| 81 | // Allows "if (scoped_ref != nullptr)" |
| 82 | bool operator!=(std::nullptr_t) const { |
| 83 | return mLocalRef != nullptr; |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | private: |
| 88 | JNIEnv* mEnv; |
| 89 | T mLocalRef; |
| 90 | |
| 91 | DISALLOW_COPY_AND_ASSIGN(ScopedLocalRef); |
| 92 | }; |
| 93 | |
| 94 | #endif // SCOPED_LOCAL_REF_H_ |