Created
September 14, 2020 15:59
-
-
Save adamski/dc40aef4a8f4bb2d48e2d5eaf09109ea to your computer and use it in GitHub Desktop.
Convert a std::function to a function pointer and context pointer, for use with C API callbacks.
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
/** | |
* Usage: | |
* ``` | |
* std::function<void(int32)> callback; // This should be stored as e.g. class member | |
* ... | |
* addInt32Callback (wrapCallable<int32>(callback), &callback); | |
* ``` | |
* @tparam T | |
* @tparam Callable | |
* @return function pointer | |
*/ | |
template<typename... T, typename Callable> | |
auto wrapCallable(Callable const&) -> void (*)(T..., void*) | |
{ | |
return +[](T... data, void* context) | |
{ | |
(*static_cast<Callable*>(context))(data...); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment