DART  6.10.1
Signal.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2021, The DART development contributors
3  * All rights reserved.
4  *
5  * The list of contributors can be found at:
6  * https://github.com/dartsim/dart/blob/master/LICENSE
7  *
8  * This file is provided under the following "BSD-style" License:
9  * Redistribution and use in source and binary forms, with or
10  * without modification, are permitted provided that the following
11  * conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef DART_COMMON_SIGNAL_HPP_
34 #define DART_COMMON_SIGNAL_HPP_
35 
36 #include <functional>
37 #include <memory>
38 #include <set>
39 
42 
43 namespace dart {
44 namespace common {
45 
48 {
49 public:
51  Connection();
52 
54  Connection(const Connection& _other);
55 
57  Connection(Connection&& _other);
58 
60  Connection& operator=(const Connection& _other);
61 
63  Connection& operator=(Connection&& _other);
64 
66  virtual ~Connection();
67 
69  bool isConnected() const;
70 
72  void disconnect() const;
73  // TODO(JS): Make this non-const in the next major release
74 
75  template <typename _Signature, template <class> class Combiner>
76  friend class Signal;
77 
78 protected:
80  Connection(
81  const std::weak_ptr<signal::detail::ConnectionBodyBase>& _connectionBody);
82 
84  Connection(
85  std::weak_ptr<signal::detail::ConnectionBodyBase>&& _connectionBody);
86 
87 private:
89  std::weak_ptr<signal::detail::ConnectionBodyBase> mWeakConnectionBody;
90 };
91 
94 {
95 public:
97  ScopedConnection(const Connection& _other);
98 
100  ScopedConnection(Connection&& _other);
101 
103  virtual ~ScopedConnection();
104 };
105 
106 template <
107  typename _Signature,
108  template <class> class Combiner = signal::detail::DefaultCombiner>
109 class Signal;
110 
112 template <typename _Res, typename... _ArgTypes, template <class> class Combiner>
113 class Signal<_Res(_ArgTypes...), Combiner>
114 {
115 public:
116  using ResultType = _Res;
117  using SlotType = std::function<ResultType(_ArgTypes...)>;
118  using SignalType = Signal<_Res(_ArgTypes...), Combiner>;
119 
121  using ConnectionSetType = std::set<
122  std::shared_ptr<ConnectionBodyType>,
123  std::owner_less<std::shared_ptr<ConnectionBodyType>>>;
124 
126  Signal();
127 
129  virtual ~Signal();
130 
132  Connection connect(const SlotType& _slot);
133 
135  Connection connect(SlotType&& _slot);
136 
138  void disconnect(const Connection& _connection) const;
139 
141  void disconnect(const std::shared_ptr<ConnectionBodyType>& connectionBody);
142 
144  void disconnectAll();
145 
147  DART_DEPRECATED(6.10)
148  void cleanupConnections();
149  // This explicit connection cleaning is no longer necessary because now a
150  // connection gets removed when it's disconnected.
151 
153  std::size_t getNumConnections() const;
154 
156  template <typename... ArgTypes>
157  ResultType raise(ArgTypes&&... _args);
158 
160  template <typename... ArgTypes>
161  ResultType operator()(ArgTypes&&... _args);
162 
163 private:
165  ConnectionSetType mConnectionBodies;
166 };
167 
169 template <typename... _ArgTypes>
170 class Signal<void(_ArgTypes...)>
171 {
172 public:
173  using SlotType = std::function<void(_ArgTypes...)>;
174  using SignalType = Signal<void(_ArgTypes...)>;
175 
177  using ConnectionSetType = std::set<
178  std::shared_ptr<ConnectionBodyType>,
179  std::owner_less<std::shared_ptr<ConnectionBodyType>>>;
180 
182  Signal();
183 
185  virtual ~Signal();
186 
188  Connection connect(const SlotType& _slot);
189 
191  Connection connect(SlotType&& _slot);
192 
194  void disconnect(const Connection& _connection) const;
195 
197  void disconnect(const std::shared_ptr<ConnectionBodyType>& connectionBody);
198 
200  void disconnectAll();
201 
203  DART_DEPRECATED(6.10)
204  void cleanupConnections();
205  // This explicit connection cleaning is no longer necessary because now a
206  // connection gets removed when it's disconnected.
207 
209  std::size_t getNumConnections() const;
210 
212  template <typename... ArgTypes>
213  void raise(ArgTypes&&... _args);
214 
216  template <typename... ArgTypes>
217  void operator()(ArgTypes&&... _args);
218 
219 private:
221  ConnectionSetType mConnectionBodies;
222 };
223 
227 template <typename T>
229 {
230 public:
231  using SlotType = typename T::SlotType;
232  using SignalType = typename T::SignalType;
233 
235  SlotRegister(typename T::SignalType& _signal);
236 
238  Connection connect(const SlotType& _slot);
239 
240 private:
242  typename T::SignalType& mSignal;
243 };
244 
245 } // namespace common
246 } // namespace dart
247 
249 
250 #endif // DART_COMMON_SIGNAL_HPP_
#define DART_DEPRECATED(version)
Definition: Deprecated.hpp:51
class Connection
Definition: Signal.hpp:48
void disconnect() const
Disconnect the connection.
Definition: Signal.cpp:101
virtual ~Connection()
Destructor.
Definition: Signal.cpp:89
Connection & operator=(const Connection &_other)
Assignment operator.
Definition: Signal.cpp:59
std::weak_ptr< signal::detail::ConnectionBodyBase > mWeakConnectionBody
Weak pointer to connection body in the signal.
Definition: Signal.hpp:89
Connection()
Default constructor.
Definition: Signal.cpp:39
bool isConnected() const
Get true if the slot is connected.
Definition: Signal.cpp:95
class ScopedConnection
Definition: Signal.hpp:94
virtual ~ScopedConnection()
Destructor.
Definition: Signal.cpp:122
ScopedConnection(const Connection &_other)
Default constructor.
Definition: Signal.cpp:108
Signal implements a signal/slot mechanism.
Definition: Signal.hpp:114
std::set< std::shared_ptr< ConnectionBodyType >, std::owner_less< std::shared_ptr< ConnectionBodyType > >> ConnectionSetType
Definition: Signal.hpp:123
void disconnect(const std::shared_ptr< ConnectionBodyType > &connectionBody)
Disconnect a connection.
std::function< ResultType(_ArgTypes...)> SlotType
Definition: Signal.hpp:117
Signal implements a signal/slot mechanism for the slots don't return a value.
Definition: Signal.hpp:171
std::set< std::shared_ptr< ConnectionBodyType >, std::owner_less< std::shared_ptr< ConnectionBodyType > >> ConnectionSetType
Definition: Signal.hpp:179
std::function< void(_ArgTypes...)> SlotType
Definition: Signal.hpp:173
void disconnect(const std::shared_ptr< ConnectionBodyType > &connectionBody)
Disconnect given connection.
Definition: Signal.hpp:109
SlotRegister can be used as a public member for connecting slots to a private Signal member.
Definition: Signal.hpp:229
typename T::SignalType SignalType
Definition: Signal.hpp:232
typename T::SlotType SlotType
Definition: Signal.hpp:231
T::SignalType & mSignal
Signal.
Definition: Signal.hpp:242
class ConnectionBody
Definition: ConnectionBody.hpp:63
Definition: BulletCollisionDetector.cpp:65
Definition: SharedLibraryManager.hpp:46
DefaultCombiner – return the last result.
Definition: ConnectionBody.hpp:135