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 | |
Orion Hodson | ce48736 | 2020-04-22 19:41:46 +0100 | [diff] [blame] | 17 | #pragma once |
Andreas Gampe | d299c07 | 2017-10-17 10:50:00 -0700 | [diff] [blame] | 18 | |
| 19 | #include <cstddef> |
| 20 | |
Orion Hodson | 6d7238a | 2020-04-21 11:12:46 +0100 | [diff] [blame] | 21 | #include <jni.h> |
| 22 | |
Andreas Gampe | d299c07 | 2017-10-17 10:50:00 -0700 | [diff] [blame] | 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 | |
Orion Hodson | fd87143 | 2019-01-02 16:49:22 +0000 | [diff] [blame] | 32 | ScopedLocalRef(ScopedLocalRef&& s) noexcept : mEnv(s.mEnv), mLocalRef(s.release()) { |
| 33 | } |
| 34 | |
| 35 | explicit ScopedLocalRef(JNIEnv* env) : mEnv(env), mLocalRef(nullptr) { |
| 36 | } |
| 37 | |
Andreas Gampe | d299c07 | 2017-10-17 10:50:00 -0700 | [diff] [blame] | 38 | ~ScopedLocalRef() { |
| 39 | reset(); |
| 40 | } |
| 41 | |
Orion Hodson | 6d7238a | 2020-04-21 11:12:46 +0100 | [diff] [blame] | 42 | void reset(T ptr = nullptr) { |
Andreas Gampe | d299c07 | 2017-10-17 10:50:00 -0700 | [diff] [blame] | 43 | if (ptr != mLocalRef) { |
Orion Hodson | 6d7238a | 2020-04-21 11:12:46 +0100 | [diff] [blame] | 44 | if (mLocalRef != nullptr) { |
Andreas Gampe | d299c07 | 2017-10-17 10:50:00 -0700 | [diff] [blame] | 45 | mEnv->DeleteLocalRef(mLocalRef); |
| 46 | } |
| 47 | mLocalRef = ptr; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | T release() __attribute__((warn_unused_result)) { |
| 52 | T localRef = mLocalRef; |
Orion Hodson | 6d7238a | 2020-04-21 11:12:46 +0100 | [diff] [blame] | 53 | mLocalRef = nullptr; |
Andreas Gampe | d299c07 | 2017-10-17 10:50:00 -0700 | [diff] [blame] | 54 | return localRef; |
| 55 | } |
| 56 | |
| 57 | T get() const { |
| 58 | return mLocalRef; |
| 59 | } |
| 60 | |
Andreas Gampe | d299c07 | 2017-10-17 10:50:00 -0700 | [diff] [blame] | 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. |
Orion Hodson | fd87143 | 2019-01-02 16:49:22 +0000 | [diff] [blame] | 68 | ScopedLocalRef& operator=(ScopedLocalRef&& s) noexcept { |
Andreas Gampe | d299c07 | 2017-10-17 10:50:00 -0700 | [diff] [blame] | 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 | } |
Andreas Gampe | d299c07 | 2017-10-17 10:50:00 -0700 | [diff] [blame] | 83 | |
| 84 | private: |
| 85 | JNIEnv* mEnv; |
| 86 | T mLocalRef; |
| 87 | |
| 88 | DISALLOW_COPY_AND_ASSIGN(ScopedLocalRef); |
| 89 | }; |
| 90 | |