Last active
December 19, 2020 19:10
-
-
Save iUltimateLP/d2316eb422f13e28cb71c50e99f39c98 to your computer and use it in GitHub Desktop.
This adds a prototype for a custom CCallResult class which allows you to use lambdas instead of class member functions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a custom Steam CallResult to support C++ lambdas | |
//----------------------------------------------------------------------------- | |
// Purpose: maps a steam async call result to a lambda function | |
// template params: P = parameter struct | |
//----------------------------------------------------------------------------- | |
template<class P> | |
class CLambdaCallResult : private CCallbackBase | |
{ | |
public: | |
typedef void (*func_t)(P*, bool); | |
CLambdaCallResult(); | |
~CLambdaCallResult(); | |
void Set(SteamAPICall_t hAPICall, func_t func); | |
bool IsActive() const; | |
void Cancel(); | |
void SetGameserverFlag() { m_nCallbackFlags |= k_ECallbackFlagsGameServer; } | |
private: | |
virtual void Run(void *pvParm); | |
virtual void Run(void *pvParam, bool bIOFailure, SteamAPICall_t hSteamAPICall); | |
virtual int GetCallbackSizeBytes() { return sizeof(P); } | |
SteamAPICall_t m_hAPICall; | |
func_t m_Func; | |
}; | |
//----------------------------------------------------------------------------- | |
// Purpose: maps a steam async call result to a lambda function | |
// template params: P = parameter struct | |
//----------------------------------------------------------------------------- | |
template<class P> | |
inline CLambdaCallResult<P>::CLambdaCallResult() | |
{ | |
m_hAPICall = k_uAPICallInvalid; | |
m_Func = NULL; | |
m_iCallback = P::k_iCallback; | |
} | |
template<class P> | |
inline void CLambdaCallResult<P>::Set(SteamAPICall_t hAPICall, func_t func) | |
{ | |
if (m_hAPICall) | |
SteamAPI_UnregisterCallResult(this, m_hAPICall); | |
m_hAPICall = hAPICall; | |
m_Func = func; | |
if (hAPICall) | |
SteamAPI_RegisterCallResult(this, hAPICall); | |
} | |
template<class P> | |
inline bool CLambdaCallResult<P>::IsActive() const | |
{ | |
return (m_hAPICall != k_uAPICallInvalid); | |
} | |
template<class P> | |
inline void CLambdaCallResult<P>::Cancel() | |
{ | |
if (m_hAPICall != k_uAPICallInvalid) | |
{ | |
SteamAPI_UnregisterCallResult(this, m_hAPICall); | |
m_hAPICall = k_uAPICallInvalid; | |
} | |
} | |
template<class P> | |
inline CLambdaCallResult<P>::~CLambdaCallResult() | |
{ | |
Cancel(); | |
} | |
template<class P> | |
inline void CLambdaCallResult<P>::Run(void *pvParam) | |
{ | |
m_hAPICall = k_uAPICallInvalid; // caller unregisters for us | |
m_Func((P*)pvParam, false); | |
} | |
template<class P> | |
inline void CLambdaCallResult<P>::Run(void *pvParam, bool bIOFailure, SteamAPICall_t hSteamAPICall) | |
{ | |
if (hSteamAPICall == m_hAPICall) | |
{ | |
m_hAPICall = k_uAPICallInvalid; // caller unregisters for us | |
m_Func((P*)pvParam, bIOFailure); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment