blob: f8ac7d48fd1c73c42970fc3b8985fff51e2699ba [file] [log] [blame]
// 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.
library FfiTest;
import 'dart:ffi' as ffi;
/// Sample struct for dart:ffi library.
@ffi.struct
class Coordinate extends ffi.Pointer<ffi.Void> {
@ffi.Double()
double x;
@ffi.Double()
double y;
@ffi.Pointer()
Coordinate next;
// Implementation generated by @ffi.struct annotation.
external static int sizeOf();
Coordinate offsetBy(int offsetInBytes) =>
super.offsetBy(offsetInBytes).cast();
Coordinate elementAt(int index) => offsetBy(sizeOf() * index);
static Coordinate allocate({int count: 1}) =>
ffi.allocate<ffi.Uint8>(count: count * sizeOf()).cast();
/// Allocate a new [Coordinate] in C memory and populate its fields.
factory Coordinate(double x, double y, Coordinate next) {
Coordinate result = Coordinate.allocate()
..x = x
..y = y
..next = next;
return result;
}
}