1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_ACE_FRAMEWORK_FFI_CJ_COLLECTION_H
17 #define OHOS_ACE_FRAMEWORK_FFI_CJ_COLLECTION_H
18 
19 #include <cstdint>
20 #include <string>
21 #include <functional>
22 
23 #include "bridge/cj_frontend/interfaces/cj_ffi/cj_macro.h"
24 
25 extern "C" struct NavigationItemFFI {
26     const char *value;
27     const char *icon;
28     int64_t builder;
29 };
30 
31 struct NavigationItem {
32     std::string value;
33     std::string icon;
34     int64_t builderFFI;
35     std::function<void()> builder;
36     NavigationItem() = default;
37     explicit NavigationItem(const NavigationItemFFI& source);
ToFFINavigationItem38     NavigationItemFFI ToFFI()
39     {
40         return NavigationItemFFI {.value = value.c_str(), .icon = icon.c_str(), .builder = builderFFI};
41     }
42 };
43 
44 #define VECTOR_H(typeName)                                                                \
45     using Vector##typeName##Handle = void*;                                               \
46     extern "C" {                                                                          \
47     CJ_EXPORT Vector##typeName##Handle FFICJCommonCreateVector##typeName(int64_t size);   \
48     CJ_EXPORT void FFICJCommonVector##typeName##Delete(Vector##typeName##Handle vec);     \
49     CJ_EXPORT int64_t FFICJCommonVector##typeName##GetSize(Vector##typeName##Handle vec); \
50     }
51 
52 VECTOR_H(String)
VECTOR_H(Int32)53 VECTOR_H(Int32)
54 VECTOR_H(Int64)
55 VECTOR_H(UInt8)
56 VECTOR_H(UInt32)
57 VECTOR_H(Float32)
58 VECTOR_H(Float64)
59 VECTOR_H(Bool)
60 VECTOR_H(NavigationItem)
61 
62 #undef VECTOR_H
63 
64 extern "C" {
65 CJ_EXPORT void FFICJCommonVectorStringSetElement(VectorStringHandle vec, int64_t index, const char* value);
66 CJ_EXPORT const char* FFICJCommonVectorStringGetElement(VectorStringHandle vec, int64_t index);
67 
68 CJ_EXPORT void FFICJCommonVectorInt32SetElement(VectorInt32Handle vec, int64_t index, int32_t value);
69 CJ_EXPORT int32_t FFICJCommonVectorInt32GetElement(VectorInt32Handle vec, int64_t index);
70 
71 CJ_EXPORT void FFICJCommonVectorInt64SetElement(VectorInt64Handle vec, int64_t index, int64_t value);
72 CJ_EXPORT int64_t FFICJCommonVectorInt64GetElement(VectorInt64Handle vec, int64_t index);
73 
74 CJ_EXPORT void FFICJCommonVectorUInt8SetElement(VectorUInt8Handle vec, int64_t index, uint8_t value);
75 CJ_EXPORT uint8_t FFICJCommonVectorUInt8GetElement(VectorUInt8Handle vec, int64_t index);
76 
77 CJ_EXPORT void FFICJCommonVectorUInt32SetElement(VectorUInt32Handle vec, int64_t index, uint32_t value);
78 CJ_EXPORT uint32_t FFICJCommonVectorUInt32GetElement(VectorUInt32Handle vec, int64_t index);
79 
80 CJ_EXPORT void FFICJCommonVectorFloat32SetElement(VectorFloat32Handle vec, int64_t index, float value);
81 CJ_EXPORT float FFICJCommonVectorFloat32GetElement(VectorFloat32Handle vec, int64_t index);
82 
83 CJ_EXPORT void FFICJCommonVectorFloat64SetElement(VectorFloat64Handle vec, int64_t index, double value);
84 CJ_EXPORT double FFICJCommonVectorFloat64GetElement(VectorFloat64Handle vec, int64_t index);
85 
86 CJ_EXPORT void FFICJCommonVectorBoolSetElement(VectorBoolHandle vec, int64_t index, bool value);
87 CJ_EXPORT bool FFICJCommonVectorBoolGetElement(VectorBoolHandle vec, int64_t index);
88 
89 CJ_EXPORT void FFICJCommonVectorNavigationItemSetElement(VectorNavigationItemHandle vec, int64_t index,
90                                                          NavigationItemFFI value);
91 CJ_EXPORT NavigationItemFFI
92 FFICJCommonVectorNavigationItemGetElement(VectorNavigationItemHandle vec, int64_t index);
93 };
94 
95 #endif // OHOS_ACE_FRAMEWORK_FFI_CJ_COLLECTION_H