I'm currently trying to include the PlayFab C ++ SDK in my application. This SDK is mainly intended for the Cocos2d-x game engine, but can be used for any C ++ application.
This is just REST, so you send requests to your servers and wait for a response. That would be ideal for using lambda.
They declare this callback, which is called when the request is successful:
template<typename ResType> using ProcessApiCallback = void(*)(const ResType& result, void* userData);
Unfortunately, they do not use std :: function, but a function pointer. Therefore, you cannot use lambdas that capture variables.
Therefore, I thought I could just replace the function callback with the std :: function callback as follows:
template<typename ResType> using ProcessApiCallback = std::function<void(const ResType& result, void* userData)>;
Unfortunately, it's not just that they store function pointers using the ugly reinterpret_casts, here is an example (remove unnecessary parts so that it is short):
void PlayFabClientAPI::LoginWithAndroidDeviceID(
LoginWithAndroidDeviceIDRequest& request,
ProcessApiCallback<LoginResult> callback,
ErrorCallback errorCallback,
void* userData
)
{
httpRequest->SetResultCallback(reinterpret_cast<void*>(callback));
httpRequest->SetErrorCallback(errorCallback);
httpRequest->SetUserData(userData);
PlayFabSettings::httpRequester->AddRequest(httpRequest, OnLoginWithAndroidDeviceIDResult, userData);
}
Later, when the request was successful, they do the following:
if (request->GetResultCallback() != nullptr)
{
ProcessApiCallback<LoginResult> successCallback = reinterpret_cast<ProcessApiCallback<LoginResult>>(request->GetResultCallback());
successCallback(outResult, request->GetUserData());
}
The HttpRequest class has this field:
void* mResultCallback;
The problem is that I don’t know how to store arbitrary std :: function pointers in the HttpRequest class and then discard them later. I tried a lot of things, including the very ugly reinterpret_casting, but nothing worked.
I am open to any changes to their SDK. I also reported this as a bad design, and they agreed, but they don’t have time to improve it, but they will accept the pull request if a good solution can be found.