1 /***************************************************************************
2 * Mechanized Assault and Exploration Reloaded Projectfile *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19
20 #ifndef utility_signal_signalconnectionmanagerH
21 #define utility_signal_signalconnectionmanagerH
22
23 #include <vector>
24
25 #include "maxrconfig.h"
26 #include "utility/signal/signalconnection.h"
27
28 /**
29 * A RAII signal connection manager class that can be used to bind
30 * the lifetime of connections to the lifetime of this connection manager.
31 *
32 * All connections that are established through this connection manager
33 * will automatically disconnected when the connection manager will
34 * be destroyed.
35 */
36 class cSignalConnectionManager
37 {
38 public:
39 cSignalConnectionManager();
40 cSignalConnectionManager (cSignalConnectionManager&& other);
41
42 ~cSignalConnectionManager();
43
44 cSignalConnectionManager& operator= (cSignalConnectionManager && other);
45
46 /**
47 * Connects the passed function object to the passed signal and
48 * stores the connection within this connection manager so that
49 * it will be disconnected when the connection manager gets destroyed.
50 *
51 * @tparam SignalType The type of the signal.
52 * @tparam FunctionType The type of the function.
53 * @param signal The signal to connect the function to.
54 * @param function The callable object to connect to the signal.
55 */
56 template<typename SignalType, typename FunctionType>
57 cSignalConnection connect (SignalType& signal, FunctionType&& function);
58
59 /**
60 * Disconnects a single connection and removes it from this manager.
61 *
62 * @param connection The connection to disconnect.
63 * Should be a connection that has been returned by the @ref connect
64 * method of this class.
65 * Any other connections will be ignored.
66 * @return true if the connection could be found in the stored connections and
67 * hence has been disconnected.
68 */
69 bool disconnect (cSignalConnection& connection);
70
71 /**
72 * Disconnects all the stored connections.
73 */
74 void disconnectAll();
75
76 /**
77 * Clears all the stored connections without disconnecting them.
78 */
79 void clear();
80 private:
81 cSignalConnectionManager (const cSignalConnectionManager& other) MAXR_DELETE_FUNCTION;
82 cSignalConnectionManager& operator= (const cSignalConnectionManager& other) MAXR_DELETE_FUNCTION;
83
84 std::vector<cSignalConnection> connections;
85 };
86
87 //------------------------------------------------------------------------------
88 template<typename SignalType, typename FunctionType>
connect(SignalType & signal,FunctionType && function)89 cSignalConnection cSignalConnectionManager::connect (SignalType& signal, FunctionType&& function)
90 {
91 connections.push_back (signal.connect (std::forward<FunctionType> (function)));
92 return connections.back();
93 }
94
95 #endif // utility_signal_signalconnectionmanagerH
96