Closed rainyl closed 1 day ago
static or factory contructors inside extensions are not supported yet, so for now, if async constructors needed, they can only be placed inside classes.
class CLAHE extends CvStruct<cvg.CLAHE> {
CLAHE._(cvg.CLAHEPtr ptr, [bool attach = true]) : super.fromPointer(ptr) {
if (attach) {
finalizer.attach(this, ptr.cast(), detach: this);
}
}
factory CLAHE.empty() {
final p = calloc<cvg.CLAHE>();
CFFI.CLAHE_Create(p);
return CLAHE._(p);
}
factory CLAHE.emptyAsync() {...}
}
However, async non-static functions can be placed in extensions:
Mat apply(Mat src, {Mat? dst}) {
dst ??= Mat.empty();
cvRun(() => CFFI.CLAHE_Apply(ref, src.ref, dst!.ref));
return dst;
}
extension CLAHEAsync on CLAHE {
Future<Mat> applyAsync(Mat src) async =>
cvRunAsync((callback) => CFFI.CLAHE_Apply_Async(ref, src.ref, callback), matCompleter);
}
Maybe this is better:
// clahe_async.dart
import '../core/base.dart';
import '../core/mat.dart';
import '../core/size.dart';
import '../opencv.g.dart' as cvg;
import 'clahe.dart';
extension CLAHEAsync on CLAHE {
static Future<CLAHE> createAsync([
double clipLimit = 40,
(int width, int height) tileGridSize = (8, 8),
]) async =>
cvRunAsync(
(callback) => CFFI.CLAHE_CreateWithParams_Async(clipLimit, tileGridSize.cvd.ref, callback),
(c, p) => c.complete(CLAHE.fromPointer(p.cast<cvg.CLAHE>())),
);
Future<Mat> applyAsync(Mat src) async =>
cvRunAsync((callback) => CFFI.CLAHE_Apply_Async(ref, src.ref, callback), matCompleter);
}
and usage:
final clahe = await cv.CLAHEAsync.createAsync();
final dst = await clahe.applyAsync(mat);
expect(dst.isEmpty, false);
https://github.com/rainyl/opencv_dart/pull/115#issuecomment-2189451022
https://github.com/rainyl/opencv_dart/pull/115#issuecomment-2190452152
https://github.com/dart-lang/language/blob/main/working/0107%20-%20implicit-constructors/feature-specification.md
https://github.com/dart-lang/language/issues/723