slot 0.0.1
A real time UI render framework
载入中...
搜索中...
未找到
ScopedLocalRef.h
浏览该文件的文档.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
12#pragma once
13
14#include <jni.h>
15#include <cstddef>
16#include <type_traits>
17
19
46template <typename T>
48 static_assert(
49 std::is_same<T, jclass>() || std::is_same<T, jobject>() ||
50 std::is_same<T, jstring>() || std::is_same<T, jthrowable>() ||
51 std::is_same<T, jbyteArray>() || std::is_same<T, jintArray>() ||
52 std::is_same<T, jshortArray>() || std::is_same<T, jcharArray>() ||
53 std::is_same<T, jlongArray>() || std::is_same<T, jfloatArray>() ||
54 std::is_same<T, jdoubleArray>() || std::is_same<T, jobjectArray>() ||
55 std::is_same<T, jbooleanArray>(),
56 "ScopedLocalRef instantiated for invalid type");
57
58 public:
64 ScopedLocalRef(JNIEnv* env, T localRef) : mEnv(env), mLocalRef(localRef) {}
65
69 explicit ScopedLocalRef(JNIEnv* env) : mEnv(env), mLocalRef(NULL) {}
70
75 : mEnv(s.mEnv), mLocalRef(s.release()) {}
76
81 reset(s.release());
82 mEnv = s.mEnv;
83 return *this;
84 }
85
87 reset();
88 }
89
94 void reset(T ptr = NULL) {
95 if (ptr != mLocalRef) {
96 if (mLocalRef != NULL) {
98 }
99 mLocalRef = ptr;
100 }
101 }
102
109 T localRef = mLocalRef;
110 mLocalRef = NULL;
111 return localRef;
112 }
113
117 T get() const {
118 return mLocalRef;
119 }
120
124 operator bool() const {
125 return mLocalRef != NULL;
126 }
127
128 ScopedLocalRef(const ScopedLocalRef& ref) = delete;
129 ScopedLocalRef& operator=(const ScopedLocalRef& other) = delete;
130
131 private:
134};
135
136template <typename T>
138 return ScopedLocalRef<T>(env, localRef);
139}
140
141} // namespace facebook::yoga::vanillajni
Definition ScopedLocalRef.h:47
~ScopedLocalRef()
Definition ScopedLocalRef.h:86
ScopedLocalRef(JNIEnv *env)
Definition ScopedLocalRef.h:69
ScopedLocalRef(const ScopedLocalRef &ref)=delete
JNIEnv * mEnv
Definition ScopedLocalRef.h:132
ScopedLocalRef(JNIEnv *env, T localRef)
Definition ScopedLocalRef.h:64
T get() const
Definition ScopedLocalRef.h:117
ScopedLocalRef & operator=(const ScopedLocalRef &other)=delete
ScopedLocalRef & operator=(ScopedLocalRef &&s) noexcept
Definition ScopedLocalRef.h:80
T release()
Definition ScopedLocalRef.h:108
T mLocalRef
Definition ScopedLocalRef.h:133
ScopedLocalRef(ScopedLocalRef &&s) noexcept
Definition ScopedLocalRef.h:74
void reset(T ptr=NULL)
Definition ScopedLocalRef.h:94
Definition common.cpp:10
ScopedLocalRef< T > make_local_ref(JNIEnv *env, T localRef)
Definition ScopedLocalRef.h:137
Definition jni.h:150
void(* DeleteLocalRef)(JNIEnv *, jobject)
Definition jni.h:185