-
Notifications
You must be signed in to change notification settings - Fork 0
/
_03_struct.dart
127 lines (97 loc) · 3.53 KB
/
_03_struct.dart
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
import 'dart:ffi';
import 'package:connect_dart_with_others/color_printer.dart';
import 'package:connect_dart_with_others/lib_builder.dart';
import 'package:ffi/ffi.dart';
void main() async {
final lib = await StructLib.load();
printGreen('Output:');
final name = lib.getName();
print('name: $name');
final reversedStr = lib.reverse('ilopX');
print('reversedStr: $reversedStr');
final coordinatePointer = lib.createCoordinate(100, 200);
final coordinate = coordinatePointer.ref;
calloc.free(coordinatePointer);
print('create_coordinate() \n\t'
'latitude: ${coordinate.latitude}\n\t'
'longitude: ${coordinate.longitude}');
final placePointer = lib.createPlace('Oslo'.toNativeUtf8(), 11, 22);
final place = placePointer.ref;
calloc.free(placePointer.ref.coordinate);
calloc.free(placePointer);
print('create place()\n\t'
'name: ${place.name.toDartString()}\n\t'
'latitude: ${place.coordinate.ref.latitude} \n\t'
'longitude: ${place.coordinate.ref.longitude} \n\t');
}
class StructLib {
final Dart_Get_Name _getName;
final Dart_Reverse _reverse;
final Dart_Create_Coordinate createCoordinate;
final Dart_Create_Place createPlace;
static Future<StructLib> load() async {
final structLibFileName = await buildAndGetFileNameLib('struct_lib');
final structLib = DynamicLibrary.open(structLibFileName);
final getName =
structLib.lookupFunction<
CXX_Get_Name,
Dart_Get_Name>('getName');
final reverse =
structLib.lookupFunction<CXX_Reverse,
Dart_Reverse>('reverse');
final createCoordinate =
structLib.lookupFunction<
CXX_Create_Coordinate,
Dart_Create_Coordinate>('create_coordinate');
final create_place = structLib.lookupFunction<
CXX_Create_Place,
Dart_Create_Place>('create_place');
return StructLib._internal(getName, reverse, createCoordinate,
create_place);
}
StructLib._internal(this._getName, this._reverse, this.createCoordinate,
this.createPlace);
String getName() {
return _getName().toDartString();
}
String reverse(String name) {
final pointerName = name.toNativeUtf8();
final result = _reverse(pointerName, name.length);
final dartResult = result.toDartString();
calloc.free( pointerName);
calloc.free(result);
return dartResult;
}
}
typedef CXX_Get_Name = Pointer<Utf8> Function();
typedef Dart_Get_Name = Pointer<Utf8> Function();
typedef CXX_Reverse = Pointer<Utf8> Function(Pointer<Utf8>, Int32);
typedef Dart_Reverse = Pointer<Utf8> Function(Pointer<Utf8> str, int length);
class Coordinate extends Struct {
@Double()
external double latitude;
@Double()
external double longitude;
factory Coordinate.allocate(double latitude, double longitude) {
return calloc<Coordinate>().ref
..latitude = latitude
..longitude = longitude;
}
}
typedef CXX_Create_Coordinate = Pointer<Coordinate> Function(
Double latitude, Double longitude);
typedef Dart_Create_Coordinate = Pointer<Coordinate> Function(
double latitude, double longitude);
class Place extends Struct {
external Pointer<Utf8> name;
external Pointer<Coordinate> coordinate;
factory Place.allocate(Pointer<Utf8> name, Pointer<Coordinate> coordinate) {
return calloc<Place>().ref
..coordinate = coordinate
..name = name;
}
}
typedef CXX_Create_Place = Pointer<Place> Function(
Pointer<Utf8>, Double latitude, Double longitude);
typedef Dart_Create_Place = Pointer<Place> Function(Pointer<
Utf8> name, double latitude, double longitude);