Here is the function declaration:
GLFWcharfun glfwSetCharCallback ( GLFWwindow * window,
GLFWcharfun cbfun
)
Where GLFWcharfun is defined as typedef void(* GLFWcharfun) (GLFWwindow *, unsigned int)
There is an obvious problem here in that you do not get the opportunity to pass in a 'context' object which will automatically map the callback back to an instance of InputManager. So you will have to perform the mapping manually using the only key you have available - the window pointer.
Here is one strategy...
#include <map>
#include <mutex>
struct GLFWwindow {};
typedef void(* GLFWcharfun) (GLFWwindow *, unsigned int);
GLFWcharfun glfwSetCharCallback ( GLFWwindow * window,
GLFWcharfun cbfun
);
struct InputManager;
struct WindowToInputManager
{
struct impl
{
void associate(GLFWwindow* window, InputManager* manager)
{
auto lock = std::unique_lock<std::mutex>(mutex_);
mapping_[window] = manager;
}
void disassociate(GLFWwindow* window, InputManager* manager)
{
auto lock = std::unique_lock<std::mutex>(mutex_);
mapping_.erase(window);
}
InputManager* find(GLFWwindow* window) const
{
auto lock = std::unique_lock<std::mutex>(mutex_);
auto i = mapping_.find(window);
if (i == mapping_.end())
return nullptr;
else
return i->second;
}
mutable std::mutex mutex_;
std::map<GLFWwindow*, InputManager*> mapping_;
};
static impl& get_impl() {
static impl i {};
return i;
}
void associate(GLFWwindow* window, InputManager* manager)
{
get_impl().associate(window, manager);
}
void disassociate(GLFWwindow* window, InputManager* manager)
{
get_impl().disassociate(window, manager);
}
InputManager* find(GLFWwindow* window)
{
return get_impl().find(window);
}
};
struct InputManager
{
void init()
{
// how to set up the callback?
// first, associate the window with this input manager
callback_mapper_.associate(window_, this);
// now use a proxy as the callback
glfwSetCharCallback(window_, &InputManager::handleCharCallback);
}
static void handleCharCallback(GLFWwindow * window,
unsigned int ch)
{
// proxy locates the handler
if(auto self = callback_mapper_.find(window))
{
self->charInputCallback(window, ch);
}
}
void charInputCallback(GLFWwindow * window,
int ch)
{
// do something here
}
GLFWwindow* window_;
static WindowToInputManager callback_mapper_;
};
Or if you prefer closures:
#include <map>
#include <mutex>
struct GLFWwindow {};
typedef void(* GLFWcharfun) (GLFWwindow *, unsigned int);
GLFWcharfun glfwSetCharCallback ( GLFWwindow * window,
GLFWcharfun cbfun
);
struct InputManager;
struct WindowToInputManager
{
using sig_type = void (GLFWwindow *, unsigned int);
using func_type = std::function<sig_type>;
struct impl
{
void associate(GLFWwindow* window, func_type func)
{
auto lock = std::unique_lock<std::mutex>(mutex_);
mapping_[window] = std::move(func);
}
void disassociate(GLFWwindow* window)
{
auto lock = std::unique_lock<std::mutex>(mutex_);
mapping_.erase(window);
}
const func_type* find(GLFWwindow* window) const
{
auto lock = std::unique_lock<std::mutex>(mutex_);
auto i = mapping_.find(window);
if (i == mapping_.end())
return nullptr;
else
return std::addressof(i->second);
}
mutable std::mutex mutex_;
std::map<GLFWwindow*, func_type> mapping_;
};
static impl& get_impl() {
static impl i {};
return i;
}
template<class F>
void associate(GLFWwindow* window, F&& f)
{
get_impl().associate(window, std::forward<F>(f));
glfwSetCharCallback(window, &WindowToInputManager::handleCharCallback);
}
void disassociate(GLFWwindow* window)
{
// call whatever is the reverse of glfwSetCharCallback here
//
// then remove from the map
get_impl().disassociate(window);
}
const func_type* find(GLFWwindow* window)
{
return get_impl().find(window);
}
static void handleCharCallback(GLFWwindow* w, unsigned int ch)
{
auto f = get_impl().find(w);
// note - possible race here if handler calls disasociate. better to return a copy of the function?
if (f) {
(*f)(w, ch);
}
}
};
struct InputManager
{
void init()
{
callback_mapper_.associate(window_, [this](auto* window, int ch) { this->charInputCallback(window, ch); });
}
void charInputCallback(GLFWwindow * window,
int ch)
{
// do something here
}
GLFWwindow* window_;
WindowToInputManager callback_mapper_;
};