slot 0.0.1
A real time UI render framework
载入中...
搜索中...
未找到
ScopedGlobalRef.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
8#pragma once
9
10#include <jni.h>
11#include <cstddef>
12#include <type_traits>
13#include "corefunctions.h"
14
16
48template <typename T>
50 static_assert(
51 std::is_same<T, jclass>() || std::is_same<T, jobject>() ||
52 std::is_same<T, jstring>() || std::is_same<T, jthrowable>() ||
53 std::is_same<T, jbyteArray>() || std::is_same<T, jintArray>() ||
54 std::is_same<T, jshortArray>() || std::is_same<T, jcharArray>() ||
55 std::is_same<T, jlongArray>() || std::is_same<T, jfloatArray>() ||
56 std::is_same<T, jdoubleArray>() || std::is_same<T, jobjectArray>() ||
57 std::is_same<T, jbooleanArray>(),
58 "ScopedGlobalRef instantiated for invalid type");
59
60 public:
66 explicit ScopedGlobalRef(T globalRef) : mGlobalRef(globalRef) {}
67
71 explicit ScopedGlobalRef() : mGlobalRef(NULL) {}
72
76 ScopedGlobalRef(ScopedGlobalRef&& s) noexcept : mGlobalRef(s.release()) {}
77
82 reset(s.release());
83 return *this;
84 }
85
87 reset();
88 }
89
94 void reset(T ptr = NULL) {
95 if (ptr != mGlobalRef) {
96 if (mGlobalRef != NULL) {
98 }
99 mGlobalRef = ptr;
100 }
101 }
102
109 T globalRef = mGlobalRef;
110 mGlobalRef = NULL;
111 return globalRef;
112 }
113
117 T get() const {
118 return mGlobalRef;
119 }
120
124 operator bool() const {
125 return mGlobalRef != NULL;
126 }
127
128 ScopedGlobalRef(const ScopedGlobalRef& ref) = delete;
130
131 private:
133};
134
135template <typename T>
137 return ScopedGlobalRef<T>(globalRef);
138}
139
140} // namespace facebook::yoga::vanillajni
Definition ScopedGlobalRef.h:49
void reset(T ptr=NULL)
Definition ScopedGlobalRef.h:94
ScopedGlobalRef(T globalRef)
Definition ScopedGlobalRef.h:66
T get() const
Definition ScopedGlobalRef.h:117
ScopedGlobalRef & operator=(ScopedGlobalRef &&s) noexcept
Definition ScopedGlobalRef.h:81
ScopedGlobalRef(const ScopedGlobalRef &ref)=delete
ScopedGlobalRef(ScopedGlobalRef &&s) noexcept
Definition ScopedGlobalRef.h:76
ScopedGlobalRef()
Definition ScopedGlobalRef.h:71
T release()
Definition ScopedGlobalRef.h:108
~ScopedGlobalRef()
Definition ScopedGlobalRef.h:86
ScopedGlobalRef & operator=(const ScopedGlobalRef &other)=delete
T mGlobalRef
Definition ScopedGlobalRef.h:132
Definition common.cpp:10
ScopedGlobalRef< T > make_global_ref(T globalRef)
Definition ScopedGlobalRef.h:136
JNIEXPORT JNIEnv * getCurrentEnv()
Definition corefunctions.cpp:45
void(* DeleteGlobalRef)(JNIEnv *, jobject)
Definition jni.h:184