blob: 97504860d80c3eddac7ac8dc75f7fcd03213840c [file]
// AUTO GENERATED FILE, DO NOT EDIT.
//
// Generated by `package:ffigen`.
// ignore_for_file: type=lint, unused_import, unused_element, deprecated_member_use_from_same_package
@ffi.DefaultAsset('package:ffigen/cpp_test')
library;
import 'dart:ffi' as ffi;
class NonPodCounter implements ffi.Finalizable {
ffi.Pointer<ffi.Void> _ptr;
static final _defaultFinalizer = ffi.NativeFinalizer(
ffi.Native.addressOf<
ffi.NativeFunction<ffi.Void Function(ffi.Pointer<ffi.Void>)>
>(_NonPodCounter_delete),
);
/// The finalizer currently attached for this instance, or [null] if this
/// object does not own its pointer.
ffi.NativeFinalizer? _activeFinalizer;
/// The native function pointer used by [_activeFinalizer], stored so that
/// [dispose] can call the correct destructor directly.
ffi.Pointer<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<ffi.Void>)>>?
_activeFinalizerFn;
NonPodCounter.fromPointer(this._ptr, {bool takeOwnership = false}) {
if (takeOwnership) {
_defaultFinalizer.attach(this, _ptr.cast(), detach: this);
_activeFinalizer = _defaultFinalizer;
_activeFinalizerFn =
ffi.Native.addressOf<
ffi.NativeFunction<ffi.Void Function(ffi.Pointer<ffi.Void>)>
>(_NonPodCounter_delete);
}
}
/// Attaches a finalizer so this object takes ownership of the underlying
/// C++ pointer. If [customFinalizer] is provided it is used instead of the
/// default `delete` finalizer, which is useful when the object was not
/// allocated with `new` (e.g. `malloc` or a custom allocator).
///
/// Both [customFinalizer] and [customFinalizerFn] must be provided together.
///
/// Throws a [StateError] if the object has already been disposed, or if
/// this object already owns the pointer.
void retainOwnership([
ffi.NativeFinalizer? customFinalizer,
ffi.Pointer<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<ffi.Void>)>>?
customFinalizerFn,
]) {
if (_ptr == ffi.nullptr) {
throw StateError('This object has already been disposed.');
}
if (_activeFinalizer != null) {
throw StateError('This object already owns its pointer.');
}
if ((customFinalizer == null) != (customFinalizerFn == null)) {
throw ArgumentError(
'Both customFinalizer and customFinalizerFn must be provided together.',
);
}
final fin = customFinalizer ?? _defaultFinalizer;
final fnPtr =
customFinalizerFn ??
ffi.Native.addressOf<
ffi.NativeFunction<ffi.Void Function(ffi.Pointer<ffi.Void>)>
>(_NonPodCounter_delete);
fin.attach(this, _ptr.cast(), detach: this);
_activeFinalizer = fin;
_activeFinalizerFn = fnPtr;
}
/// Detaches the finalizer so this object releases ownership of the
/// underlying C++ pointer. The caller becomes responsible for freeing
/// the memory.
///
/// Throws a [StateError] if the object has already been disposed, or if
/// this object does not own the pointer.
void releaseOwnership() {
if (_ptr == ffi.nullptr) {
throw StateError('This object has already been disposed.');
}
if (_activeFinalizer == null) {
throw StateError('This object does not own its pointer.');
}
_activeFinalizer!.detach(this);
_activeFinalizer = null;
_activeFinalizerFn = null;
}
/// Detaches the finalizer and invalidates this object, returning the
/// underlying C++ pointer.
///
/// Throws a [StateError] if the object has already been disposed, or if
/// this object does not own the pointer.
ffi.Pointer<ffi.Void> detachPointer() {
final rawPtr = _ptr;
releaseOwnership();
_ptr = ffi.nullptr;
return rawPtr;
}
factory NonPodCounter(int start) {
return NonPodCounter.fromPointer(
_NonPodCounter_new(start),
takeOwnership: true,
);
}
int next() {
if (_ptr == ffi.nullptr) {
throw StateError('This object has already been disposed.');
}
return _NonPodCounter_next(_ptr);
}
void dispose() {
if (_ptr == ffi.nullptr) {
throw StateError('This object has already been disposed.');
}
if (_activeFinalizer == null) {
throw StateError(
'Cannot dispose a non-owning wrapper. '
'Call retainOwnership() first to take ownership.',
);
}
_activeFinalizer!.detach(this);
_activeFinalizer = null;
_activeFinalizerFn?.asFunction<void Function(ffi.Pointer<ffi.Void>)>()(
_ptr,
);
_activeFinalizerFn = null;
_ptr = ffi.nullptr;
}
}
@ffi.Native<ffi.Pointer<ffi.Void> Function(ffi.Int)>(
symbol: 'NonPodCounter_new',
)
external ffi.Pointer<ffi.Void> _NonPodCounter_new(int start);
@ffi.Native<ffi.Int Function(ffi.Pointer<ffi.Void>)>(
symbol: 'NonPodCounter_next',
)
external int _NonPodCounter_next(ffi.Pointer<ffi.Void> self);
@ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>)>(
symbol: 'NonPodCounter_delete',
)
external void _NonPodCounter_delete(ffi.Pointer<ffi.Void> self);
final class PodPair extends ffi.Struct {
@ffi.Int()
external int a;
@ffi.Int()
external int b;
static ffi.Pointer<PodPair> $allocate(
ffi.Allocator $allocator, {
required int a,
required int b,
}) => $allocator<PodPair>()
..ref.a = a
..ref.b = b;
}
final class PodPoint extends ffi.Struct {
@ffi.Int()
external int x;
@ffi.Int()
external int y;
static ffi.Pointer<PodPoint> $allocate(
ffi.Allocator $allocator, {
required int x,
required int y,
}) => $allocator<PodPoint>()
..ref.x = x
..ref.y = y;
}