summaryrefslogtreecommitdiffstats
path: root/src/Core/Signals
diff options
context:
space:
mode:
Diffstat (limited to 'src/Core/Signals')
-rw-r--r--src/Core/Signals/Connection.h53
-rw-r--r--src/Core/Signals/GenericSignal.h61
-rw-r--r--src/Core/Signals/Signal0.h46
-rw-r--r--src/Core/Signals/Signal1.h47
-rw-r--r--src/Core/Signals/Signal2.h47
-rw-r--r--src/Core/Signals/SignalBase.h53
6 files changed, 307 insertions, 0 deletions
diff --git a/src/Core/Signals/Connection.h b/src/Core/Signals/Connection.h
new file mode 100644
index 0000000..5ce7b4a
--- /dev/null
+++ b/src/Core/Signals/Connection.h
@@ -0,0 +1,53 @@
+/*
+ * Connection.h
+ *
+ * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef MAD_CORE_SIGNALS_CONNECTION_H_
+#define MAD_CORE_SIGNALS_CONNECTION_H_
+
+namespace Mad {
+namespace Core {
+namespace Signals {
+
+class SignalBase;
+
+class Connection {
+ private:
+ friend class SignalBase;
+
+ SignalBase *signal;
+ unsigned long id;
+
+ Connection(SignalBase *signal0, unsigned long id0)
+ : signal(signal0), id(id0) {}
+
+ public:
+ bool operator==(const Connection &o) const {
+ return (signal == o.signal && id == o.id);
+ }
+
+ bool operator<(const Connection &o) const {
+ return (signal != o.signal) ? (signal < o.signal) : (id < o.id);
+ }
+};
+
+}
+}
+}
+
+#endif /* MAD_CORE_SIGNALS_CONNECTION_H_ */
diff --git a/src/Core/Signals/GenericSignal.h b/src/Core/Signals/GenericSignal.h
new file mode 100644
index 0000000..641f575
--- /dev/null
+++ b/src/Core/Signals/GenericSignal.h
@@ -0,0 +1,61 @@
+/*
+ * GenericSignal.h
+ *
+ * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef MAD_CORE_SIGNALS_GENERICSIGNAL_H_
+#define MAD_CORE_SIGNALS_GENERICSIGNAL_H_
+
+#include "SignalBase.h"
+
+#include <map>
+#include <boost/thread/locks.hpp>
+
+namespace Mad {
+namespace Core {
+namespace Signals {
+
+template <typename FunctionType>
+class GenericSignal : protected SignalBase {
+ public:
+ typedef FunctionType slot_type;
+
+ protected:
+ std::map<Connection, slot_type> handlers;
+
+ public:
+ Connection connect(const slot_type &slot) {
+ boost::lock_guard<boost::mutex> lock(mutex);
+
+ Connection con = getNewConnection();
+ handlers.insert(std::make_pair(con, slot));
+
+ return con;
+ }
+
+ bool disconnect(const Connection &connection) {
+ boost::lock_guard<boost::mutex> lock(mutex);
+
+ return handlers.erase(connection);
+ }
+};
+
+}
+}
+}
+
+#endif /* MAD_CORE_SIGNALS_GENERICSIGNAL_H_ */
diff --git a/src/Core/Signals/Signal0.h b/src/Core/Signals/Signal0.h
new file mode 100644
index 0000000..ccfb548
--- /dev/null
+++ b/src/Core/Signals/Signal0.h
@@ -0,0 +1,46 @@
+/*
+ * Signal0.h
+ *
+ * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef MAD_CORE_SIGNALS_SIGNAL0_H_
+#define MAD_CORE_SIGNALS_SIGNAL0_H_
+
+#include "GenericSignal.h"
+#include "../ThreadManager.h"
+
+#include <boost/function.hpp>
+
+namespace Mad {
+namespace Core {
+namespace Signals {
+
+class Signal0 : public GenericSignal<boost::function0<void> > {
+ public:
+ void emit() {
+ boost::lock_guard<boost::mutex> lock(mutex);
+
+ for(std::map<Connection, slot_type>::iterator handler = handlers.begin(); handler != handlers.end(); ++handler)
+ ThreadManager::get()->pushWork(handler->second);
+ }
+};
+
+}
+}
+}
+
+#endif /* MAD_CORE_SIGNALS_SIGNAL0_H_ */
diff --git a/src/Core/Signals/Signal1.h b/src/Core/Signals/Signal1.h
new file mode 100644
index 0000000..e4a946c
--- /dev/null
+++ b/src/Core/Signals/Signal1.h
@@ -0,0 +1,47 @@
+/*
+ * Signal1.h
+ *
+ * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef MAD_CORE_SIGNALS_SIGNAL1_H_
+#define MAD_CORE_SIGNALS_SIGNAL1_H_
+
+#include "GenericSignal.h"
+#include "../ThreadManager.h"
+
+#include <boost/function.hpp>
+
+namespace Mad {
+namespace Core {
+namespace Signals {
+
+template <typename T1>
+class Signal1 : public GenericSignal<boost::function1<void, T1> > {
+ public:
+ void emit(T1 arg1) {
+ boost::lock_guard<boost::mutex> lock(this->mutex);
+
+ for(typename std::map<Connection, typename GenericSignal<boost::function1<void, T1> >::slot_type>::iterator handler = this->handlers.begin(); handler != this->handlers.end(); ++handler)
+ ThreadManager::get()->pushWork(boost::bind(handler->second, arg1));
+ }
+};
+
+}
+}
+}
+
+#endif /* MAD_CORE_SIGNALS_SIGNAL1_H_ */
diff --git a/src/Core/Signals/Signal2.h b/src/Core/Signals/Signal2.h
new file mode 100644
index 0000000..41045d7
--- /dev/null
+++ b/src/Core/Signals/Signal2.h
@@ -0,0 +1,47 @@
+/*
+ * Signal2.h
+ *
+ * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef MAD_CORE_SIGNALS_SIGNAL2_H_
+#define MAD_CORE_SIGNALS_SIGNAL2_H_
+
+#include "GenericSignal.h"
+#include "../ThreadManager.h"
+
+#include <boost/function.hpp>
+
+namespace Mad {
+namespace Core {
+namespace Signals {
+
+template <typename T1, typename T2>
+class Signal2 : public GenericSignal<boost::function2<void, T1, T2> > {
+ public:
+ void emit(T1 arg1, T2 arg2) {
+ boost::lock_guard<boost::mutex> lock(this->mutex);
+
+ for(typename std::map<Connection, typename GenericSignal<boost::function2<void, T1, T2> >::slot_type>::iterator handler = this->handlers.begin(); handler != this->handlers.end(); ++handler)
+ ThreadManager::get()->pushWork(boost::bind(handler->second, arg1, arg2));
+ }
+};
+
+}
+}
+}
+
+#endif /* MAD_CORE_SIGNALS_SIGNAL2_H_ */
diff --git a/src/Core/Signals/SignalBase.h b/src/Core/Signals/SignalBase.h
new file mode 100644
index 0000000..1a5d5a3
--- /dev/null
+++ b/src/Core/Signals/SignalBase.h
@@ -0,0 +1,53 @@
+/*
+ * SignalBase.h
+ *
+ * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef MAD_CORE_SIGNALS_SIGNALBASE_H_
+#define MAD_CORE_SIGNALS_SIGNALBASE_H_
+
+#include "Connection.h"
+
+#include <set>
+#include <boost/thread/mutex.hpp>
+
+namespace Mad {
+namespace Core {
+namespace Signals {
+
+class SignalBase : private boost::noncopyable {
+ private:
+ friend class Connection;
+
+ unsigned long connectionId;
+
+ protected:
+ boost::mutex mutex;
+
+ Connection getNewConnection() {
+ return Connection(this, connectionId++);
+ }
+
+ SignalBase() : connectionId(0) {}
+ virtual ~SignalBase() {}
+};
+
+}
+}
+}
+
+#endif /* MAD_CORE_SIGNALS_SIGNALBASE_H_ */