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 #include "bridge/cj_frontend/interfaces/cj_ffi/cj_collection_ffi.h"
17 
18 #include "cj_lambda.h"
19 
20 #include <string>
21 #include <vector>
22 
NavigationItem(const NavigationItemFFI & source)23 NavigationItem::NavigationItem(const NavigationItemFFI& source)
24 {
25     value = source.value;
26     icon = source.icon;
27     builderFFI = source.builder;
28     builder = CJLambda::Create(reinterpret_cast<void(*)()>(builderFFI));
29 }
30 
31 extern "C" {
32 #define VECTOR_FUNC_H(cjType, cType)                                       \
33     Vector##cjType##Handle FFICJCommonCreateVector##cjType(int64_t size)   \
34     {                                                                      \
35         return new std::vector<cType>(size);                               \
36     }                                                                      \
37                                                                            \
38     void FFICJCommonVector##cjType##Delete(Vector##cjType##Handle vec)     \
39     {                                                                      \
40         auto actualVec = reinterpret_cast<std::vector<cType>*>(vec);       \
41         delete actualVec;                                                  \
42     }                                                                      \
43                                                                            \
44     int64_t FFICJCommonVector##cjType##GetSize(Vector##cjType##Handle vec) \
45     {                                                                      \
46         auto actualVec = reinterpret_cast<std::vector<cType>*>(vec);       \
47         return actualVec->size();                                          \
48     }
49 
VECTOR_FUNC_H(String,std::string)50 VECTOR_FUNC_H(String, std::string)
51 VECTOR_FUNC_H(Int32, int32_t)
52 VECTOR_FUNC_H(Int64, int64_t)
53 VECTOR_FUNC_H(UInt8, uint8_t)
54 VECTOR_FUNC_H(UInt32, uint32_t)
55 VECTOR_FUNC_H(Float32, float)
56 VECTOR_FUNC_H(Float64, double)
57 VECTOR_FUNC_H(Bool, bool)
58 VECTOR_FUNC_H(NavigationItem, NavigationItem)
59 
60 #undef VECTOR_FUNC_H
61 
62 // This is for string.
63 void FFICJCommonVectorStringSetElement(VectorStringHandle vec, int64_t index, const char* value)
64 {
65     auto actualVec = reinterpret_cast<std::vector<std::string>*>(vec);
66     (*actualVec)[index] = value;
67 }
68 
69 // never free return value in cj.
FFICJCommonVectorStringGetElement(VectorStringHandle vec,int64_t index)70 const char* FFICJCommonVectorStringGetElement(VectorStringHandle vec, int64_t index)
71 {
72     auto actualVec = reinterpret_cast<std::vector<std::string>*>(vec);
73     return (*actualVec)[index].c_str();
74 }
75 
76 // This is for int.
FFICJCommonVectorInt32SetElement(VectorInt32Handle vec,int64_t index,int32_t value)77 void FFICJCommonVectorInt32SetElement(VectorInt32Handle vec, int64_t index, int32_t value)
78 {
79     auto actualVec = reinterpret_cast<std::vector<int32_t>*>(vec);
80     (*actualVec)[index] = value;
81 }
82 
FFICJCommonVectorInt32GetElement(VectorInt32Handle vec,int64_t index)83 int32_t FFICJCommonVectorInt32GetElement(VectorInt32Handle vec, int64_t index)
84 {
85     auto actualVec = reinterpret_cast<std::vector<int32_t>*>(vec);
86     return (*actualVec)[index];
87 }
88 
89 // This is for long long.
FFICJCommonVectorInt64SetElement(VectorInt64Handle vec,int64_t index,int64_t value)90 void FFICJCommonVectorInt64SetElement(VectorInt64Handle vec, int64_t index, int64_t value)
91 {
92     auto actualVec = reinterpret_cast<std::vector<int64_t>*>(vec);
93     (*actualVec)[index] = value;
94 }
95 
FFICJCommonVectorInt64GetElement(VectorInt64Handle vec,int64_t index)96 int64_t FFICJCommonVectorInt64GetElement(VectorInt64Handle vec, int64_t index)
97 {
98     auto actualVec = reinterpret_cast<std::vector<int64_t>*>(vec);
99     return (*actualVec)[index];
100 }
101 
102 // This is for uint8_t.
FFICJCommonVectorUInt8SetElement(VectorUInt8Handle vec,int64_t index,uint8_t value)103 void FFICJCommonVectorUInt8SetElement(VectorUInt8Handle vec, int64_t index, uint8_t value)
104 {
105     auto actualVec = reinterpret_cast<std::vector<uint8_t>*>(vec);
106     (*actualVec)[index] = value;
107 }
108 
FFICJCommonVectorUInt8GetElement(VectorUInt8Handle vec,int64_t index)109 uint8_t FFICJCommonVectorUInt8GetElement(VectorUInt8Handle vec, int64_t index)
110 {
111     auto actualVec = reinterpret_cast<std::vector<uint8_t>*>(vec);
112     return (*actualVec)[index];
113 }
114 
115 // This is for uint32_t.
FFICJCommonVectorUInt32SetElement(VectorUInt32Handle vec,int64_t index,uint32_t value)116 void FFICJCommonVectorUInt32SetElement(VectorUInt32Handle vec, int64_t index, uint32_t value)
117 {
118     auto actualVec = reinterpret_cast<std::vector<uint32_t>*>(vec);
119     (*actualVec)[index] = value;
120 }
121 
FFICJCommonVectorUInt32GetElement(VectorUInt32Handle vec,int64_t index)122 uint32_t FFICJCommonVectorUInt32GetElement(VectorUInt32Handle vec, int64_t index)
123 {
124     auto actualVec = reinterpret_cast<std::vector<uint32_t>*>(vec);
125     return (*actualVec)[index];
126 }
127 
128 // This is for float.
FFICJCommonVectorFloat32SetElement(VectorFloat32Handle vec,int64_t index,float value)129 void FFICJCommonVectorFloat32SetElement(VectorFloat32Handle vec, int64_t index, float value)
130 {
131     auto actualVec = reinterpret_cast<std::vector<float>*>(vec);
132     (*actualVec)[index] = value;
133 }
134 
FFICJCommonVectorFloat32GetElement(VectorFloat32Handle vec,int64_t index)135 float FFICJCommonVectorFloat32GetElement(VectorFloat32Handle vec, int64_t index)
136 {
137     auto actualVec = reinterpret_cast<std::vector<float>*>(vec);
138     return (*actualVec)[index];
139 }
140 
141 // This is for double.
FFICJCommonVectorFloat64SetElement(VectorFloat64Handle vec,int64_t index,double value)142 void FFICJCommonVectorFloat64SetElement(VectorFloat64Handle vec, int64_t index, double value)
143 {
144     auto actualVec = reinterpret_cast<std::vector<double>*>(vec);
145     (*actualVec)[index] = value;
146 }
147 
FFICJCommonVectorFloat64GetElement(VectorFloat64Handle vec,int64_t index)148 double FFICJCommonVectorFloat64GetElement(VectorFloat64Handle vec, int64_t index)
149 {
150     auto actualVec = reinterpret_cast<std::vector<double>*>(vec);
151     return (*actualVec)[index];
152 }
153 
154 // This is for bool.
FFICJCommonVectorBoolSetElement(VectorBoolHandle vec,int64_t index,bool value)155 void FFICJCommonVectorBoolSetElement(VectorBoolHandle vec, int64_t index, bool value)
156 {
157     auto actualVec = reinterpret_cast<std::vector<bool>*>(vec);
158     (*actualVec)[index] = value;
159 }
160 
FFICJCommonVectorBoolGetElement(VectorBoolHandle vec,int64_t index)161 bool FFICJCommonVectorBoolGetElement(VectorBoolHandle vec, int64_t index)
162 {
163     auto actualVec = reinterpret_cast<std::vector<bool>*>(vec);
164     return (*actualVec)[index];
165 }
166 
167 // This is for NavigationItem.
FFICJCommonVectorNavigationItemSetElement(VectorNavigationItemHandle vec,int64_t index,NavigationItemFFI value)168 void FFICJCommonVectorNavigationItemSetElement(VectorNavigationItemHandle vec, int64_t index, NavigationItemFFI value)
169 {
170     auto actualVec = reinterpret_cast<std::vector<NavigationItem>*>(vec);
171     (*actualVec)[index] = NavigationItem(value);
172 }
173 
FFICJCommonVectorNavigationItemGetElement(VectorNavigationItemHandle vec,int64_t index)174 NavigationItemFFI FFICJCommonVectorNavigationItemGetElement(VectorNavigationItemHandle vec, int64_t index)
175 {
176     auto actualVec = reinterpret_cast<std::vector<NavigationItem>*>(vec);
177     return (*actualVec)[index].ToFFI();
178 }
179 }