Skip to content

Instantly share code, notes, and snippets.

@adamski
Created September 14, 2020 15:59
Show Gist options
  • Save adamski/dc40aef4a8f4bb2d48e2d5eaf09109ea to your computer and use it in GitHub Desktop.
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.
/**
* 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