These are chat archives for SmingHub/Sming

Mar 2018
Mar 02 2018 08:41

I remember being able to use lambdas using something like this:

client->sendRequest(HTTP_GET, "", requestHeaders, (RequestCompletedDelegate)([](HttpConnection& client, bool successful) -> int {
        printf("onRequestComplete: URL: %s\n" , client.getRequest()->uri.toString().c_str());
        if(successful) {
//            client.sendRequest("", onRequestComplete);
            // TODO:
            return 0;

        // do something else

        return 0;

But haven't tried it with capture...

Mar 02 2018 12:32

@slaff yes that works because lambdas that don't capture anything can be cast to a function pointer (btw you don't even need the explicit cast to a Delegate). but lambdas that use capture do need to preserve a state so they can't be casted to plane function pointers.

it should be pretty easy to add a constructor to Delegate that takes ether a std::function or even just a simple templated functor object. both options would not only be able to take a capturing lambda but be way more generic in general and therefor useable with more different ways to represent a callback. sadly i don't fully understand some of the design decisions made by whoever wrote the Delegator class so I don't feel comfortable modifying it.