-
Notifications
You must be signed in to change notification settings - Fork 230
/
Copy pathstructs.dart
121 lines (103 loc) · 3.91 KB
/
structs.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
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:ffi';
import 'dart:io' show Directory, Platform;
import 'package:ffi/ffi.dart';
import 'package:path/path.dart' as path;
// Example of handling a simple C struct
final class Coordinate extends Struct {
@Double()
external double latitude;
@Double()
external double longitude;
}
// Example of a complex struct (contains a string and a nested struct)
final class Place extends Struct {
external Pointer<Utf8> name;
external Coordinate coordinate;
}
// C function: char *hello_world();
// There's no need for two typedefs here, as both the
// C and Dart functions have the same signature
typedef HelloWorld = Pointer<Utf8> Function();
// C function: char *reverse(char *str, int length)
typedef ReverseNative = Pointer<Utf8> Function(Pointer<Utf8> str, Int32 length);
typedef Reverse = Pointer<Utf8> Function(Pointer<Utf8> str, int length);
// C function: void free_string(char *str)
typedef FreeStringNative = Void Function(Pointer<Utf8> str);
typedef FreeString = void Function(Pointer<Utf8> str);
// C function: struct Coordinate create_coordinate(double latitude, double longitude)
typedef CreateCoordinateNative =
Coordinate Function(Double latitude, Double longitude);
typedef CreateCoordinate =
Coordinate Function(double latitude, double longitude);
// C function: struct Place create_place(char *name, double latitude, double longitude)
typedef CreatePlaceNative =
Place Function(Pointer<Utf8> name, Double latitude, Double longitude);
typedef CreatePlace =
Place Function(Pointer<Utf8> name, double latitude, double longitude);
typedef DistanceNative = Double Function(Coordinate p1, Coordinate p2);
typedef Distance = double Function(Coordinate p1, Coordinate p2);
void main() {
// Open the dynamic library
var libraryPath = path.join(
Directory.current.path,
'structs_library',
'libstructs.so',
);
if (Platform.isMacOS) {
libraryPath = path.join(
Directory.current.path,
'structs_library',
'libstructs.dylib',
);
}
if (Platform.isWindows) {
libraryPath = path.join(
Directory.current.path,
'structs_library',
'Debug',
'structs.dll',
);
}
final dylib = DynamicLibrary.open(libraryPath);
final helloWorld = dylib.lookupFunction<HelloWorld, HelloWorld>(
'hello_world',
);
final message = helloWorld().toDartString();
print(message);
final reverse = dylib.lookupFunction<ReverseNative, Reverse>('reverse');
final backwards = 'backwards';
final backwardsUtf8 = backwards.toNativeUtf8();
final reversedMessageUtf8 = reverse(backwardsUtf8, backwards.length);
final reversedMessage = reversedMessageUtf8.toDartString();
calloc.free(backwardsUtf8);
print('$backwards reversed is $reversedMessage');
final freeString = dylib.lookupFunction<FreeStringNative, FreeString>(
'free_string',
);
freeString(reversedMessageUtf8);
final createCoordinate = dylib
.lookupFunction<CreateCoordinateNative, CreateCoordinate>(
'create_coordinate',
);
final coordinate = createCoordinate(3.5, 4.6);
print(
'Coordinate is lat ${coordinate.latitude}, long ${coordinate.longitude}',
);
final myHomeUtf8 = 'My Home'.toNativeUtf8();
final createPlace = dylib.lookupFunction<CreatePlaceNative, CreatePlace>(
'create_place',
);
final place = createPlace(myHomeUtf8, 42.0, 24.0);
final name = place.name.toDartString();
calloc.free(myHomeUtf8);
final coord = place.coordinate;
print(
'The name of my place is $name at ${coord.latitude}, ${coord.longitude}',
);
final distance = dylib.lookupFunction<DistanceNative, Distance>('distance');
final dist = distance(createCoordinate(2.0, 2.0), createCoordinate(5.0, 6.0));
print("distance between (2,2) and (5,6) = $dist");
}