Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Async and Sync Organization #118

Closed
rainyl opened this issue Jun 26, 2024 · 2 comments
Closed

Better Async and Sync Organization #118

rainyl opened this issue Jun 26, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@rainyl rainyl added the enhancement New feature or request label Jun 26, 2024
@rainyl
Copy link
Owner Author

rainyl commented Jun 27, 2024

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);
}

@rainyl
Copy link
Owner Author

rainyl commented Jun 27, 2024

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant