Clean up callbacks

base/callback.hh:
    Don't remove a callback when it is processed.
    Document the callback class

--HG--
extra : convert_revision : 9d15500434fe0e5d623c624aac86976708adf0eb
This commit is contained in:
Nathan Binkert 2003-10-20 21:38:32 -04:00
parent e0b065ff7c
commit df488c0e70

View file

@ -31,26 +31,76 @@
#include <list> #include <list>
class Callback { /**
* Generic callback class. This base class provides a virutal process
* function that gets called when the callback queue is processed.
*/
class Callback
{
public: public:
/**
* virtualize the destructor to make sure that the correct one
* gets called.
*/
virtual ~Callback() {} virtual ~Callback() {}
/**
* virtual process function that is invoked when the callback
* queue is executed.
*/
virtual void process() = 0; virtual void process() = 0;
}; };
class CallbackQueue class CallbackQueue
{ {
protected: protected:
std::list<Callback *> callbacks; /**
* Simple typedef for the data structure that stores all of the
* callbacks.
*/
typedef std::list<Callback *> queue;
/**
* List of all callbacks. To be called in fifo order.
*/
queue callbacks;
public: public:
void add(Callback *callback) { callbacks.push_back(callback); } /**
bool empty() const { return callbacks.empty(); } * Add a callback to the end of the queue
void processOne() { * @param callback the callback to be added to the queue
Callback *c = callbacks.front(); */
callbacks.pop_front(); void add(Callback *callback)
c->process(); {
callbacks.push_back(callback);
}
/**
* Find out if there are any callbacks in the queue
*/
bool empty() const { return callbacks.empty(); }
/**
* process all callbacks
*/
void process()
{
queue::iterator i = callbacks.begin();
queue::iterator end = callbacks.end();
while (i != end) {
(*i)->process();
++i;
}
}
/**
* clear the callback queue
*/
void clear()
{
callbacks.clear();
} }
void processAll() { while (!empty()) processOne(); }
}; };
#endif // __CALLBACK_HH__ #endif // __CALLBACK_HH__