DART  6.7.3
Signal.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2019, 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 
41 
42 namespace dart {
43 namespace common {
44 
47 {
48 public:
50  Connection();
51 
53  Connection(const Connection& _other);
54 
56  Connection(Connection&& _other);
57 
59  Connection& operator=(const Connection& _other);
60 
62  Connection& operator=(Connection&& _other);
63 
65  virtual ~Connection();
66 
68  bool isConnected() const;
69 
71  void disconnect() const;
72 
73  template <typename _Signature, template<class> class Combiner>
74  friend class Signal;
75 
76 protected:
78  Connection(
79  const std::weak_ptr<signal::detail::ConnectionBodyBase>& _connectionBody);
80 
82  Connection(
83  std::weak_ptr<signal::detail::ConnectionBodyBase>&& _connectionBody);
84 
85 private:
87  std::weak_ptr<signal::detail::ConnectionBodyBase> mWeakConnectionBody;
88 };
89 
92 {
93 public:
95  ScopedConnection(const Connection& _other);
96 
98  ScopedConnection(Connection&& _other);
99 
101  virtual ~ScopedConnection();
102 };
103 
104 template <typename _Signature,
105  template<class> class Combiner = signal::detail::DefaultCombiner>
106 class Signal;
107 
109 template <typename _Res, typename... _ArgTypes, template<class> class Combiner>
110 class Signal<_Res(_ArgTypes...), Combiner>
111 {
112 public:
113  using ResultType = _Res;
114  using SlotType = std::function<ResultType(_ArgTypes...)>;
115  using SignalType = Signal<_Res(_ArgTypes...), Combiner>;
116 
119  = std::set<std::shared_ptr<ConnectionBodyType>,
120  std::owner_less<std::shared_ptr<ConnectionBodyType>>>;
121 
123  Signal();
124 
126  virtual ~Signal();
127 
129  Connection connect(const SlotType& _slot);
130 
132  Connection connect(SlotType&& _slot);
133 
135  void disconnect(const Connection& _connection) const;
136 
138  void disconnectAll();
139 
141  void cleanupConnections();
142 
144  std::size_t getNumConnections() const;
145 
147  template <typename... ArgTypes>
148  ResultType raise(ArgTypes&&... _args);
149 
151  template <typename... ArgTypes>
152  ResultType operator()(ArgTypes&&... _args);
153 
154 private:
157 };
158 
160 template <typename... _ArgTypes>
161 class Signal<void(_ArgTypes...)>
162 {
163 public:
164  using SlotType = std::function<void(_ArgTypes...)>;
165  using SignalType = Signal<void(_ArgTypes...)>;
166 
169  = std::set<std::shared_ptr<ConnectionBodyType>,
170  std::owner_less<std::shared_ptr<ConnectionBodyType>>>;
171 
173  Signal();
174 
176  virtual ~Signal();
177 
179  Connection connect(const SlotType& _slot);
180 
182  Connection connect(SlotType&& _slot);
183 
185  void disconnect(const Connection& _connection) const;
186 
188  void disconnectAll();
189 
191  void cleanupConnections();
192 
194  std::size_t getNumConnections() const;
195 
197  template <typename... ArgTypes>
198  void raise(ArgTypes&&... _args);
199 
201  template <typename... ArgTypes>
202  void operator()(ArgTypes&&... _args);
203 
204 private:
207 };
208 
212 template <typename T>
214 {
215 public:
216  using SlotType = typename T::SlotType;
217  using SignalType = typename T::SignalType;
218 
220  SlotRegister(typename T::SignalType& _signal);
221 
223  Connection connect(const SlotType& _slot);
224 
225 private:
227  typename T::SignalType& mSignal;
228 };
229 
230 } // namespace common
231 } // namespace dart
232 
234 
235 #endif // DART_COMMON_SIGNAL_HPP_
236 
class Connection
Definition: Signal.hpp:47
void disconnect() const
Disconnect the connection.
Definition: Signal.cpp:107
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:87
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:92
virtual ~ScopedConnection()
Destructor.
Definition: Signal.cpp:133
ScopedConnection(const Connection &_other)
Default constructor.
Definition: Signal.cpp:119
Signal implements a signal/slot mechanism.
Definition: Signal.hpp:111
std::set< std::shared_ptr< ConnectionBodyType >, std::owner_less< std::shared_ptr< ConnectionBodyType > >> ConnectionSetType
Definition: Signal.hpp:120
ConnectionSetType mConnectionBodies
Connection set.
Definition: Signal.hpp:156
ResultType operator()(ArgTypes &&... _args)
Raise the signal.
std::function< ResultType(_ArgTypes...)> SlotType
Definition: Signal.hpp:114
Signal implements a signal/slot mechanism for the slots don't return a value.
Definition: Signal.hpp:162
std::function< void(_ArgTypes...)> SlotType
Definition: Signal.hpp:164
ConnectionSetType mConnectionBodies
Connection set.
Definition: Signal.hpp:206
std::set< std::shared_ptr< ConnectionBodyType >, std::owner_less< std::shared_ptr< ConnectionBodyType > >> ConnectionSetType
Definition: Signal.hpp:170
Definition: Signal.hpp:106
SlotRegister can be used as a public member for connecting slots to a private Signal member.
Definition: Signal.hpp:214
typename T::SignalType SignalType
Definition: Signal.hpp:217
SlotRegister(typename T::SignalType &_signal)
Constructor given signal.
Definition: Signal.hpp:258
typename T::SlotType SlotType
Definition: Signal.hpp:216
T::SignalType & mSignal
Signal.
Definition: Signal.hpp:227
Connection connect(const SlotType &_slot)
Connect a slot to the signal.
Definition: Signal.hpp:266
class ConnectionBody
Definition: ConnectionBody.hpp:68
Definition: BulletCollisionDetector.cpp:63
DefaultCombiner – return the last result.
Definition: ConnectionBody.hpp:120