]> git.tdb.fi Git - libs/core.git/blobdiff - source/io/eventdispatcher.h
Move files to prepare for assimilation into core
[libs/core.git] / source / io / eventdispatcher.h
diff --git a/source/io/eventdispatcher.h b/source/io/eventdispatcher.h
new file mode 100644 (file)
index 0000000..7e89d47
--- /dev/null
@@ -0,0 +1,55 @@
+#ifndef EVENTDISPATCHER_H_
+#define EVENTDISPATCHER_H_
+
+#include <sigc++/connection.h>
+#include <sigc++/trackable.h>
+#include "poll.h"
+
+namespace Msp {
+namespace IO {
+
+/**
+Put your I/O objects inside one of these to get signaled when something happens
+on some of them.
+*/
+class EventDispatcher: public sigc::trackable
+{
+private:
+       struct Slot
+       {
+               Base *obj;
+               sigc::connection evch_conn;
+               sigc::connection del_conn;
+
+               Slot(Base *o): obj(o) { }
+       };
+
+       typedef std::map<Base *, Slot> SlotMap;
+
+       Poller poller;
+       SlotMap objects;
+
+public:
+       EventDispatcher();
+
+       void add(Base &);
+       void remove(Base &);
+
+       /** Checks for and dispatches events.  If there are no events available,
+       blocks until there are. */
+       void tick();
+
+       /** Checks for and dispatches events.  If there are no events available,
+       waits at most the specified time before returning. */
+       void tick(const Time::TimeDelta &);
+
+private:
+       void object_events_changed(PollEvent, Base *);
+       void object_deleted(Base *);
+       void dispatch();
+};
+
+} // namespace IO
+} // namespace Msp
+
+#endif