summaryrefslogtreecommitdiff
path: root/src/base/callback.hh
diff options
context:
space:
mode:
authorNathan Binkert <nate@binkert.org>2010-04-18 13:23:25 -0700
committerNathan Binkert <nate@binkert.org>2010-04-18 13:23:25 -0700
commit50bf3895b048a35884128662149dfb642d153670 (patch)
treeba3135d686c9b90691cdb84ff727318c2056f7c8 /src/base/callback.hh
parent12fc22571c0445ed42a89e6de672f774ab7e3e10 (diff)
downloadgem5-50bf3895b048a35884128662149dfb642d153670.tar.xz
callback: Make helper functions that create callback objects for you
clean up callback stuff a little bit while we're at it.
Diffstat (limited to 'src/base/callback.hh')
-rw-r--r--src/base/callback.hh76
1 files changed, 54 insertions, 22 deletions
diff --git a/src/base/callback.hh b/src/base/callback.hh
index a39d7df20..74bb0d2a0 100644
--- a/src/base/callback.hh
+++ b/src/base/callback.hh
@@ -28,10 +28,11 @@
* Authors: Nathan Binkert
*/
-#ifndef __CALLBACK_HH__
-#define __CALLBACK_HH__
+#ifndef __BASE_CALLBACK_HH__
+#define __BASE_CALLBACK_HH__
#include <list>
+#include <string>
/**
* Generic callback class. This base class provides a virtual process
@@ -39,6 +40,10 @@
*/
class Callback
{
+ protected:
+ friend class CallbackQueue;
+ virtual void autoDestruct() {}
+
public:
/**
* virtualize the destructor to make sure that the correct one
@@ -53,6 +58,29 @@ class Callback
virtual void process() = 0;
};
+/// Helper template class to turn a simple class member function into
+/// a callback.
+template <class T, void (T::* F)()>
+class MakeCallback : public Callback
+{
+ protected:
+ T *object;
+ const bool autoDestroy;
+
+ void autoDestruct() { if (autoDestroy) delete this; }
+
+ public:
+ MakeCallback(T *o, bool auto_destroy = false)
+ : object(o), autoDestroy(auto_destroy)
+ { }
+
+ MakeCallback(T &o, bool auto_destroy = false)
+ : object(&o), autoDestroy(auto_destroy)
+ { }
+
+ void process() { (object->*F)(); }
+};
+
class CallbackQueue
{
protected:
@@ -68,15 +96,33 @@ class CallbackQueue
queue callbacks;
public:
+ ~CallbackQueue();
+ std::string name() const { return "CallbackQueue"; }
+
/**
* Add a callback to the end of the queue
* @param callback the callback to be added to the queue
*/
- void add(Callback *callback)
+ void
+ add(Callback *callback)
{
callbacks.push_back(callback);
}
+ template <class T, void (T::* F)()>
+ void
+ add(T *obj)
+ {
+ add(new MakeCallback<T, F>(obj, true));
+ }
+
+ template <class T, void (T::* F)()>
+ void
+ add(T &obj)
+ {
+ add(new MakeCallback<T, F>(&obj, true));
+ }
+
/**
* Find out if there are any callbacks in the queue
*/
@@ -85,7 +131,8 @@ class CallbackQueue
/**
* process all callbacks
*/
- void process()
+ void
+ process()
{
queue::iterator i = callbacks.begin();
queue::iterator end = callbacks.end();
@@ -99,26 +146,11 @@ class CallbackQueue
/**
* clear the callback queue
*/
- void clear()
+ 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__
+#endif // __BASE_CALLBACK_HH__