libsaria: Added callback framework

I have created generic functions for registering and triggering
callbacks.  New callbacks can be added by creating a new constant in the
callback_t enum.
This commit is contained in:
Bryan Schumaker 2011-08-27 12:34:19 -04:00
parent 5dea1ae42a
commit 2888e741e8
5 changed files with 38 additions and 3 deletions

View File

@ -0,0 +1,9 @@
#ifndef LIBSARIA_CALLBACKS_H
#define LIBSARIA_CALLBACKS_H
enum callback_t {
PLAY,
PAUSE,
};
#endif /* LIBSARIA_CALLBACKS_H */

View File

@ -2,18 +2,21 @@
#define LIBSARIA_H
#include <libsaria/audio.h>
#include <libsaria/callback.h>
#include <libsaria/print.h>
class Libsaria
{
private:
Audio audio;
void trigger_callback(callback_t);
public:
Libsaria(int, char**);
~Libsaria();
void load(string);
void register_callback(callback_t, void (*)());
/* Control functions */
void play();

21
libsaria/callback.cpp Normal file
View File

@ -0,0 +1,21 @@
#include <libsaria/libsaria.h>
#include <libsaria/callback.h>
#include <map>
using namespace std;
static map<callback_t, void (*)()> callbacks;
void Libsaria::trigger_callback(callback_t type)
{
map<callback_t, void (*)()>::iterator it;
it=callbacks.find(type);
if (it != callbacks.end())
it->second();
}
void Libsaria::register_callback(callback_t type, void (* func)())
{
callbacks[type] = func;
}

View File

@ -4,12 +4,14 @@
void Libsaria::play()
{
audio.play();
if(audio.play())
trigger_callback(PLAY);
}
void Libsaria::pause()
{
audio.pause();
if(audio.pause())
trigger_callback(PAUSE);
}
void Libsaria::stop()

View File

@ -15,5 +15,5 @@ Libsaria::~Libsaria()
void Libsaria::load(string filepath)
{
audio.load(filepath);
audio.play();
play();
}