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 //
27 // For creating a `ScopedLocalRef<jstring>`, consider using `CREATE_UTF_OR_RETURN`.
28 template<typename T>
29 class ScopedLocalRef {
30 public:
ScopedLocalRef(JNIEnv * env,T localRef)31     ScopedLocalRef(JNIEnv* env, T localRef) : mEnv(env), mLocalRef(localRef) {
32     }
33 
ScopedLocalRef(ScopedLocalRef && s)34     ScopedLocalRef(ScopedLocalRef&& s) noexcept : mEnv(s.mEnv), mLocalRef(s.release()) {
35     }
36 
ScopedLocalRef(JNIEnv * env)37     explicit ScopedLocalRef(JNIEnv* env) : mEnv(env), mLocalRef(nullptr) {
38     }
39 
~ScopedLocalRef()40     ~ScopedLocalRef() {
41         reset();
42     }
43 
44     void reset(T ptr = nullptr) {
45         if (ptr != mLocalRef) {
46             if (mLocalRef != nullptr) {
47                 mEnv->DeleteLocalRef(mLocalRef);
48             }
49             mLocalRef = ptr;
50         }
51     }
52 
release()53     T release() __attribute__((warn_unused_result)) {
54         T localRef = mLocalRef;
55         mLocalRef = nullptr;
56         return localRef;
57     }
58 
get()59     T get() const {
60         return mLocalRef;
61     }
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) noexcept {
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 
86 private:
87     JNIEnv* mEnv;
88     T mLocalRef;
89 
90     DISALLOW_COPY_AND_ASSIGN(ScopedLocalRef);
91 };
92 
93