diff options
Diffstat (limited to 'src/Net/Signals/SignalBase.h')
-rw-r--r-- | src/Net/Signals/SignalBase.h | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/Net/Signals/SignalBase.h b/src/Net/Signals/SignalBase.h new file mode 100644 index 0000000..fc5f2c6 --- /dev/null +++ b/src/Net/Signals/SignalBase.h @@ -0,0 +1,80 @@ +/* + * 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_SIGNALS_SIGNALBASE_H_ +#define MAD_SIGNALS_SIGNALBASE_H_ + +#include "Connection.h" + +#include <set> +#include <boost/signals.hpp> +#include <boost/thread/locks.hpp> +#include <boost/thread/recursive_mutex.hpp> + +namespace Mad { +namespace Net { +namespace Signals { + +template <typename SignalType, typename FunctionType> +class SignalBase : private boost::noncopyable { + public: + typedef boost::slot<FunctionType> slot_type; + + protected: + typedef SignalType signal_type; + + std::set<boost::signals::connection> connections; + boost::recursive_mutex mutex; + + signal_type signal; + + SignalBase() {} + ~SignalBase() { + // Wait for other threads + boost::lock_guard<boost::recursive_mutex> lock(mutex); + } + + public: + Connection connect(const slot_type &slot) { + boost::lock_guard<boost::recursive_mutex> lock(mutex); + + boost::signals::connection con(signal.connect(slot)); + + connections.insert(con); + return con; + } + + void disconnect(const Connection &connection) { + boost::lock_guard<boost::recursive_mutex> lock(mutex); + + std::set<boost::signals::connection>::iterator it = connections.find(connection.connection); + + if(it == connections.end()) + return; + + it->disconnect(); + connections.erase(it); + } +}; + +} +} +} + +#endif /* MAD_SIGNALS_SIGNALBASE_H_ */ |