libsaria: Prepare for a single callback handler function

Rather than keeping a map of function pointers, I want the UI to
register a single function that takes a callback_t as an argument.  From
there, the UI can decide the best way to handle callback lookups (such
as grabbing a lock before changing anything).  At the very least, a
switch statement or array lookup should be faster than searching a map.

Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
Bryan Schumaker 2012-01-21 21:33:01 -05:00
parent 12f6723111
commit 7150e8bdee
2 changed files with 9 additions and 0 deletions

View File

@ -13,6 +13,7 @@ enum callback_t {
};
void register_callback(callback_t, void (*)());
void set_cb_handler(void (*)(callback_t));
void trigger_callback(callback_t);
#endif /* LIBSARIA_CALLBACKS_H */

View File

@ -6,6 +6,7 @@
using namespace std;
static map<callback_t, void (*)()> callbacks;
static void (*handler_func)(callback_t);
void trigger_callback(callback_t type)
{
@ -13,9 +14,16 @@ void trigger_callback(callback_t type)
it=callbacks.find(type);
if (it != callbacks.end())
it->second();
if (handler_func)
handler_func(type);
}
void register_callback(callback_t type, void (* func)())
{
callbacks[type] = func;
}
void set_cb_handler(void (*func)(callback_t))
{
handler_func = func;
}