c++cobjective-ccore-graphicsquartz-2d

Create an Objective-C Block in plain C++


I want to create a CGDisplayStream with the CGDisplayStreamCreateWithDispatchQueue c function, which expects an CGDisplayStreamFrameAvailableHandler as parameter. CGDisplayStreamFrameAvailableHandler is an objective-c block. I tried to use a c++ lambda but this doesnt work:

No viable conversion from '(lambda at ###RECTRACTED###)' to 'CGDisplayStreamFrameAvailableHandler' (aka 'void (^)(CGDisplayStreamFrameStatus, uint64_t, IOSurfaceRef _Nullable, CGDisplayStreamUpdateRef _Nullable)')

I found: Objective-C Blocks in C but this doesnt really help, since they didnt explain what is needed to create an objective-c block in plain c or c++.

How can i create a valid object which i can pass to CGDisplayStreamCreateWithDispatchQueue?


Solution

  • This worked for me:

    CGDisplayStreamFrameAvailableHandler handler = ^void(CGDisplayStreamFrameStatus status, uint64_t timestamp, IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef) { std::cout << "Update" << std::endl; };

    Thanks to @molbdnilo for the help.