xref: /aosp_15_r20/external/fbjni/cxx/fbjni/detail/ReferenceAllocators.h (revision 65c59e023c5336bbd4a23be7af78407e3d80e7e7)
1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
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 /**
18  * @file ReferenceAllocators.h
19  *
20  * Reference allocators are used to create and delete various classes of JNI
21  * references (local, global, and weak global).
22  */
23 
24 #pragma once
25 
26 #include "Common.h"
27 
28 namespace facebook {
29 namespace jni {
30 
31 /// Allocator that handles local references
32 class LocalReferenceAllocator {
33  public:
34   jobject newReference(jobject original) const;
35   void deleteReference(jobject reference) const noexcept;
36   bool verifyReference(jobject reference) const noexcept;
37 };
38 
39 /// Allocator that handles global references
40 class GlobalReferenceAllocator {
41  public:
42   jobject newReference(jobject original) const;
43   void deleteReference(jobject reference) const noexcept;
44   bool verifyReference(jobject reference) const noexcept;
45 };
46 
47 /// Allocator that handles weak global references
48 class WeakGlobalReferenceAllocator {
49  public:
50   jobject newReference(jobject original) const;
51   void deleteReference(jobject reference) const noexcept;
52   bool verifyReference(jobject reference) const noexcept;
53 };
54 
55 /**
56  * @return Helper based on GetObjectRefType.  Since this isn't defined
57  * on all versions of Java or Android, if the type can't be
58  * determined, this returns true.  If reference is nullptr, returns
59  * true.
60  */
61 bool isObjectRefType(jobject reference, jobjectRefType refType);
62 
63 } // namespace jni
64 } // namespace facebook
65 
66 #include "ReferenceAllocators-inl.h"
67