Merge ktlim@zizzer.eecs.umich.edu:/bk/m5
[gem5.git] / base / callback.hh
index a1d23b5ed2577e1cae0b52c35e72bb1891c63f07..cc2a2f4295a995c89226ca0bb8766456b0ba75bd 100644 (file)
 
 #include <list>
 
-class Callback {
+/**
+ * Generic callback class.  This base class provides a virtual process
+ * function that gets called when the callback queue is processed.
+ */
+class Callback
+{
   public:
+    /**
+     * virtualize the destructor to make sure that the correct one
+     * gets called.
+     */
     virtual ~Callback() {}
+
+    /**
+     * virtual process function that is invoked when the callback
+     * queue is executed.
+     */
     virtual void process() = 0;
 };
 
 class CallbackQueue
 {
   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:
-    void add(Callback *callback) { callbacks.push_back(callback); }
+    /**
+     * Add a callback to the end of the queue
+     * @param callback the callback to be added to the queue
+     */
+    void add(Callback *callback)
+    {
+        callbacks.push_back(callback);
+    }
+
+    /**
+     * Find out if there are any callbacks in the queue
+     */
     bool empty() const { return callbacks.empty(); }
-    void processOne() {
-        Callback *c = callbacks.front();
-        callbacks.pop_front();
-        c->process();
+
+    /**
+     * process all callbacks
+     */
+    void process()
+    {
+        queue::iterator i = callbacks.begin();
+        queue::iterator end = callbacks.end();
+
+        while (i != end) {
+            (*i)->process();
+            ++i;
+        }
     }
-    void processAll() { while (!empty()) processOne(); }
+
+    /**
+     * clear the callback queue
+     */
+    void clear()
+    {
+        callbacks.clear();
+    }
+};
+
+/// Helper template class to turn a simple class member function into
+/// a callback.
+template <class T, void (T::* F)()>
+class MakeCallback : public Callback
+{
+  private:
+    T *object;
+
+  public:
+    MakeCallback(T *o)
+        : object(o)
+    { }
+
+    void process() { (object->*F)(); }
 };
 
 #endif // __CALLBACK_HH__