| // 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 Animal 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>)> |
| >(_Animal_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; |
| |
| Animal.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>)> |
| >(_Animal_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>)> |
| >(_Animal_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; |
| } |
| |
| factory Animal(int age) { |
| return Animal.fromPointer(_Animal_new(age), takeOwnership: true); |
| } |
| void speak() { |
| if (_ptr == ffi.nullptr) { |
| throw StateError('This object has already been disposed.'); |
| } |
| |
| return _Animal_speak(_ptr); |
| } |
| |
| int getAge() { |
| if (_ptr == ffi.nullptr) { |
| throw StateError('This object has already been disposed.'); |
| } |
| |
| return _Animal_getAge(_ptr); |
| } |
| |
| static int getCount() { |
| return _Animal_getCount(); |
| } |
| |
| static void Animal_new() { |
| return _Animal_Animal_new(); |
| } |
| |
| static void Animal_delete() { |
| return _Animal_Animal_delete(); |
| } |
| |
| bool isMammalClass() { |
| if (_ptr == ffi.nullptr) { |
| throw StateError('This object has already been disposed.'); |
| } |
| |
| return _Animal_isMammalClass(_ptr); |
| } |
| |
| double getWeight(double multiplier) { |
| if (_ptr == ffi.nullptr) { |
| throw StateError('This object has already been disposed.'); |
| } |
| |
| return _Animal_getWeight(_ptr, multiplier); |
| } |
| |
| int addAges(int otherAge, double scale) { |
| if (_ptr == ffi.nullptr) { |
| throw StateError('This object has already been disposed.'); |
| } |
| |
| return _Animal_addAges(_ptr, otherAge, scale); |
| } |
| |
| static int sum(int a, int b) { |
| return _Animal_sum(a, b); |
| } |
| |
| 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: 'Animal_new') |
| external ffi.Pointer<ffi.Void> _Animal_new(int age); |
| |
| @ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>)>(symbol: 'Animal_speak') |
| external void _Animal_speak(ffi.Pointer<ffi.Void> self); |
| |
| @ffi.Native<ffi.Int Function(ffi.Pointer<ffi.Void>)>(symbol: 'Animal_getAge') |
| external int _Animal_getAge(ffi.Pointer<ffi.Void> self); |
| |
| @ffi.Native<ffi.Int Function()>(symbol: 'Animal_getCount') |
| external int _Animal_getCount(); |
| |
| @ffi.Native<ffi.Void Function()>(symbol: 'Animal_Animal_new') |
| external void _Animal_Animal_new(); |
| |
| @ffi.Native<ffi.Void Function()>(symbol: 'Animal_Animal_delete') |
| external void _Animal_Animal_delete(); |
| |
| @ffi.Native<ffi.Bool Function(ffi.Pointer<ffi.Void>)>( |
| symbol: 'Animal_isMammalClass', |
| ) |
| external bool _Animal_isMammalClass(ffi.Pointer<ffi.Void> self); |
| |
| @ffi.Native<ffi.Double Function(ffi.Pointer<ffi.Void>, ffi.Double)>( |
| symbol: 'Animal_getWeight', |
| ) |
| external double _Animal_getWeight( |
| ffi.Pointer<ffi.Void> self, |
| double multiplier, |
| ); |
| |
| @ffi.Native<ffi.Int Function(ffi.Pointer<ffi.Void>, ffi.Int, ffi.Float)>( |
| symbol: 'Animal_addAges', |
| ) |
| external int _Animal_addAges( |
| ffi.Pointer<ffi.Void> self, |
| int otherAge, |
| double scale, |
| ); |
| |
| @ffi.Native<ffi.Int Function(ffi.Int, ffi.Int)>(symbol: 'Animal_sum') |
| external int _Animal_sum(int a, int b); |
| |
| @ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>)>(symbol: 'Animal_delete') |
| external void _Animal_delete(ffi.Pointer<ffi.Void> self); |
| |
| class FinalizerTestSubject 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>)> |
| >(_FinalizerTestSubject_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; |
| |
| FinalizerTestSubject.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>)> |
| >(_FinalizerTestSubject_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>)> |
| >(_FinalizerTestSubject_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; |
| } |
| |
| factory FinalizerTestSubject(ffi.Pointer<ffi.Int> counter) { |
| return FinalizerTestSubject.fromPointer( |
| _FinalizerTestSubject_new(counter), |
| takeOwnership: true, |
| ); |
| } |
| 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.Pointer<ffi.Int>)>( |
| symbol: 'FinalizerTestSubject_new', |
| ) |
| external ffi.Pointer<ffi.Void> _FinalizerTestSubject_new( |
| ffi.Pointer<ffi.Int> counter, |
| ); |
| |
| @ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>)>( |
| symbol: 'FinalizerTestSubject_delete', |
| ) |
| external void _FinalizerTestSubject_delete(ffi.Pointer<ffi.Void> self); |