I am exploring and trying to learn C++ Coroutines (added in C++20). An SDK I am using has asynchronous API calls which all take a callback, the callbacks are invoked on some background thread managed by the SDK.
namespace third_party {
bool api_call(const std::string& some_parameter, const std::function<void(std::error_code)>& callback);
} // namespace third_party
I would like wrap this API call into something which can be awaited instead:
namespace my_third_party_sdk_wrapper {
cppcoro::task<std::error_code> api_call(const std::string& some_parameter);
cppcoro::task<std::error_code> api_call(const std::string& some_parameter, cppcoro::cancellation_token token);
} // namespace my_third_party_sdk_wrapper
I am considering using the cppcoro lib but that isn't a requirement unless the implementation of the wrapper gets much simpler by doing so.
The problem is I cannot figure out how implement the wrapper.
There's a really good article by Raymond Chen, you can find it here.
In your case, you can do something like this.
This should do what you want.