blob: cd35a88d980856c76cc124c52d195df15c484cc3 [file] [log] [blame]
Andreas Gamped299c072017-10-17 10:50:00 -07001/*
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 Hodsonce487362020-04-22 19:41:46 +010017#pragma once
Andreas Gamped299c072017-10-17 10:50:00 -070018
19#include <cstddef>
20
Orion Hodson6d7238a2020-04-21 11:12:46 +010021#include <jni.h>
22
Andreas Gamped299c072017-10-17 10:50:00 -070023#include "nativehelper_utils.h"
24
25// A smart pointer that deletes a JNI local reference when it goes out of scope.
26template<typename T>
27class ScopedLocalRef {
28public:
29 ScopedLocalRef(JNIEnv* env, T localRef) : mEnv(env), mLocalRef(localRef) {
30 }
31
Orion Hodsonfd871432019-01-02 16:49:22 +000032 ScopedLocalRef(ScopedLocalRef&& s) noexcept : mEnv(s.mEnv), mLocalRef(s.release()) {
33 }
34
35 explicit ScopedLocalRef(JNIEnv* env) : mEnv(env), mLocalRef(nullptr) {
36 }
37
Andreas Gamped299c072017-10-17 10:50:00 -070038 ~ScopedLocalRef() {
39 reset();
40 }
41
Orion Hodson6d7238a2020-04-21 11:12:46 +010042 void reset(T ptr = nullptr) {
Andreas Gamped299c072017-10-17 10:50:00 -070043 if (ptr != mLocalRef) {
Orion Hodson6d7238a2020-04-21 11:12:46 +010044 if (mLocalRef != nullptr) {
Andreas Gamped299c072017-10-17 10:50:00 -070045 mEnv->DeleteLocalRef(mLocalRef);
46 }
47 mLocalRef = ptr;
48 }
49 }
50
51 T release() __attribute__((warn_unused_result)) {
52 T localRef = mLocalRef;
Orion Hodson6d7238a2020-04-21 11:12:46 +010053 mLocalRef = nullptr;
Andreas Gamped299c072017-10-17 10:50:00 -070054 return localRef;
55 }
56
57 T get() const {
58 return mLocalRef;
59 }
60
Andreas Gamped299c072017-10-17 10:50:00 -070061
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 Hodsonfd871432019-01-02 16:49:22 +000068 ScopedLocalRef& operator=(ScopedLocalRef&& s) noexcept {
Andreas Gamped299c072017-10-17 10:50:00 -070069 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 Gamped299c072017-10-17 10:50:00 -070083
84private:
85 JNIEnv* mEnv;
86 T mLocalRef;
87
88 DISALLOW_COPY_AND_ASSIGN(ScopedLocalRef);
89};
90