-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsum.cpp
288 lines (237 loc) · 7.3 KB
/
sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
extern "C" int sum(int a, int b) { return a + b; }
extern "C" double doubleSum(double a, double b) { return a + b; }
extern "C" double floatSum(float a, float b) { return a + b; }
extern "C" int64_t testbigint(int64_t a)
{
return a;
}
extern "C" const char *concatenateStrings(const char *str1, const char *str2)
{
std::string result = std::string(str1) + std::string(str2);
char *cstr = new char[result.length() + 1];
strcpy(cstr, result.c_str());
return cstr;
}
extern "C" void *returnNullPointer()
{
return NULL;
}
extern "C" const wchar_t *concatenateWideStrings(const wchar_t *str1,
const wchar_t *str2)
{
std::wstring result = std::wstring(str1) + std::wstring(str2);
wchar_t *wcstr = new wchar_t[result.length() + 1];
wcscpy(wcstr, result.c_str());
return wcstr;
}
extern "C" char *getStringFromPtr(void *ptr) { return (char *)ptr; };
extern "C" int getValueFromDoublePointer(int **ptr) { return *ptr[0]; };
extern "C" void noRet() { printf("%s", "hello world\n"); }
extern "C" int *createArrayi32(const int *arr, int size)
{
int *vec = (int *)malloc((size) * sizeof(int));
for (int i = 0; i < size; i++)
{
vec[i] = arr[i];
}
return vec;
}
extern "C" double *createArrayDouble(const double *arr, int size)
{
double *vec = (double *)malloc((size) * sizeof(double));
for (int i = 0; i < size; i++)
{
vec[i] = arr[i];
}
return vec;
}
extern "C" double *createArrayFloat(const float *arr, int size)
{
double *vec = (double *)malloc((size) * sizeof(double));
for (int i = 0; i < size; i++)
{
vec[i] = arr[i];
}
return vec;
}
extern "C" char **createArrayString(char **arr, int size)
{
char **vec = (char **)malloc(size * sizeof(char *));
if (vec == NULL)
{
return NULL;
}
for (int i = 0; i < size; i++)
{
vec[i] = strdup(arr[i]);
}
return vec;
}
extern "C" bool return_opposite(bool input) { return !input; }
typedef struct stackStruct
{
int age;
} stackStruct;
typedef struct Person
{
int age;
double *doubleArray;
Person *parent;
double doubleProps;
const char *name;
char **stringArray;
int *i32Array;
uint8_t staticBytes[16];
bool boolTrue;
bool boolFalse;
int64_t longVal;
struct stackStruct stackStruct;
char byte;
char *byteArray;
} Person;
extern "C" Person *getStruct(Person *person) { return person; }
extern "C" Person *createPerson()
{
Person *person = (Person *)malloc(sizeof(Person));
// Allocate and initialize doubleArray
double initDoubleArray[] = {1.1, 2.2, 3.3};
person->doubleArray = (double *)malloc(sizeof(initDoubleArray));
memcpy(person->doubleArray, initDoubleArray, sizeof(initDoubleArray));
// Initialize age and doubleProps
person->age = 23;
person->doubleProps = 1.1;
person->byte = 'A';
uint8_t bytes[16];
memset(bytes, 99, sizeof(bytes));
memcpy(person->staticBytes, bytes, sizeof(bytes));
// Allocate and initialize name
person->name = strdup("tom");
char *stringArray[] = {strdup("tom")};
person->stringArray = (char **)malloc(sizeof(stringArray));
memcpy(person->stringArray, stringArray, sizeof(stringArray));
person->stackStruct.age = 16;
// Allocate and initialize byteArray
char initByteArray[] = {101, 102};
person->byteArray = (char *)malloc(sizeof(initByteArray));
memcpy(person->byteArray, initByteArray, sizeof(initByteArray));
int initI32Array[] = {1, 2, 3, 4};
person->i32Array = (int *)malloc(sizeof(initI32Array));
memcpy(person->i32Array, initI32Array, sizeof(initI32Array));
person->boolTrue = true;
person->boolFalse = false;
person->longVal = 4294967296;
// Allocate and initialize parent
person->parent = (Person *)malloc(sizeof(Person));
person->parent->stackStruct.age = 22;
double parentDoubleArray[] = {1.1, 2.2, 3.3};
person->parent->doubleArray = (double *)malloc(sizeof(parentDoubleArray));
memcpy(person->parent->doubleArray, parentDoubleArray,
sizeof(parentDoubleArray));
person->parent->age = 43;
person->parent->doubleProps = 3.3;
person->parent->name = strdup("tom father");
uint8_t pbytes[16];
memset(pbytes, 88, sizeof(bytes));
memcpy(person->parent->staticBytes, pbytes, sizeof(pbytes));
char *pstringArray[] = {strdup("tom"), strdup("father")};
person->parent->stringArray = (char **)malloc(sizeof(pstringArray));
memcpy(person->parent->stringArray, pstringArray, sizeof(pstringArray));
int parentI32Array[] = {5, 6, 7};
person->parent->i32Array = (int *)malloc(sizeof(parentI32Array));
memcpy(person->parent->i32Array, parentI32Array, sizeof(parentI32Array));
person->parent->boolTrue = true;
person->parent->boolFalse = false;
person->parent->longVal = 5294967296;
person->parent->byte = 'B';
char parentByteArray[] = {103, 104};
person->parent->byteArray = (char *)malloc(sizeof(parentByteArray));
memcpy(person->parent->byteArray, parentByteArray, sizeof(parentByteArray));
return person;
}
typedef const int (*FunctionPointer)(int a, bool b, char *c, double d, char **e,
int *f, Person *g);
extern "C" void callFunction(FunctionPointer func)
{
int a = 100;
bool b = false;
double d = 100.11;
char *c = (char *)malloc(14 * sizeof(char));
strcpy(c, "Hello, World!");
char **stringArray = (char **)malloc(sizeof(char *) * 2);
stringArray[0] = strdup("Hello");
stringArray[1] = strdup("world");
int *i32Array = (int *)malloc(sizeof(int) * 3);
i32Array[0] = 101;
i32Array[1] = 202;
i32Array[2] = 303;
Person *p = createPerson();
// To get the return value, use ffi-rs.runInNewThread or spawn a new thread like
// std::thread t(threadFunction, func); t.detach();
int res = func(a, b, c, d, stringArray, i32Array, p);
printf("function ret %d\n", res);
}
// 定义 C++ 类
class MyClass
{
public:
std::string name;
int age;
MyClass(std::string n, int a) : name(n), age(a) {}
void print()
{
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
};
MyClass *createMyClass(std::string name, int age)
{
return new MyClass(name, age);
}
extern "C" MyClass *createMyClassFromC(const char *name, int age)
{
return createMyClass(std::string(name), age);
}
extern "C" void printMyClass(MyClass *instance) { instance->print(); }
typedef struct
{
short x;
short y;
short dir;
unsigned char kind;
} MINUTIA;
typedef struct
{
short nNumber;
MINUTIA item[3];
} MINUTIAE;
extern "C" MINUTIAE *printAndReturnMinutiae(MINUTIAE *minutiae)
{
MINUTIAE *result = new MINUTIAE;
result->nNumber = minutiae->nNumber;
printf("nNumber=%d\n", minutiae->nNumber);
for (int i = 0; i < result->nNumber; i++)
{
result->item[i] = minutiae->item[i];
printf("Result item[%d]: x=%d, y=%d, dir=%d, kind=%d\n",
i, result->item[i].x, result->item[i].y,
result->item[i].dir, (int)result->item[i].kind);
}
return result;
}
// typedef void (*CallbackType)(const char *);
// extern "C" void call_callback_async() {
// dispatch_async(dispatch_get_main_queue(), ^{
// printf("dispatch_async\n");
// // callback("Hello from dispatched block");
// });
// // dispatch_main();
// }
// int call_callback_async(CallbackType callback) {
// std::async(std::launch::async, [=]() { callback("Hello from async task");
// }); return 0;
// }