33 #ifndef DART_COMMON_DETAIL_SIGNAL_HPP_
34 #define DART_COMMON_DETAIL_SIGNAL_HPP_
42 template <
typename _Res,
typename... _ArgTypes,
template<
class>
class Combiner>
49 template <
typename _Res,
typename... _ArgTypes,
template<
class>
class Combiner>
50 Signal<_Res (_ArgTypes...), Combiner>::~Signal()
56 template <
typename _Res,
typename... _ArgTypes,
template<
class>
class Combiner>
59 auto newConnectionBody = std::make_shared<ConnectionBodyType>(_slot);
60 mConnectionBodies.insert(newConnectionBody);
62 return Connection(std::move(newConnectionBody));
66 template <
typename _Res,
typename... _ArgTypes,
template<
class>
class Combiner>
69 auto newConnectionBody
70 = std::make_shared<ConnectionBodyType>(std::forward<SlotType>(_slot));
71 mConnectionBodies.insert(newConnectionBody);
73 return Connection(std::move(newConnectionBody));
77 template <
typename _Res,
typename... _ArgTypes,
template<
class>
class Combiner>
78 void Signal<_Res (_ArgTypes...), Combiner>::disconnect(
85 template <
typename _Res,
typename... _ArgTypes,
template<
class>
class Combiner>
86 void Signal<_Res (_ArgTypes...), Combiner>::disconnectAll()
88 mConnectionBodies.clear();
92 template <
typename _Res,
typename... _ArgTypes,
template<
class>
class Combiner>
93 void Signal<_Res (_ArgTypes...), Combiner>::cleanupConnections()
96 for (
const auto& connectionBody : mConnectionBodies)
98 if (!connectionBody->isConnected())
99 mConnectionBodies.erase(connectionBody);
104 template <
typename _Res,
typename... _ArgTypes,
template<
class>
class Combiner>
105 std::size_t
Signal<_Res (_ArgTypes...), Combiner>::getNumConnections()
const
107 std::size_t numConnections = 0;
110 for (
const auto& connectionBody : mConnectionBodies)
112 if (connectionBody->isConnected())
116 return numConnections;
120 template <
typename _Res,
typename... _ArgTypes,
template<
class>
class Combiner>
121 template <
typename... ArgTypes>
122 _Res
Signal<_Res (_ArgTypes...), Combiner>::raise(ArgTypes&&... _args)
124 std::vector<ResultType> res(mConnectionBodies.size());
125 auto resIt = res.begin();
127 for (
auto itr = mConnectionBodies.begin(); itr != mConnectionBodies.end(); )
129 if ((*itr)->isConnected())
131 *(resIt++) = (*itr)->getSlot()(std::forward<ArgTypes>(_args)...);
136 mConnectionBodies.erase(itr++);
140 return Combiner<ResultType>::process(res.begin(), resIt);
144 template <
typename _Res,
typename... _ArgTypes,
template<
class>
class Combiner>
145 template <
typename... ArgTypes>
146 _Res
Signal<_Res (_ArgTypes...), Combiner>::operator()(ArgTypes&&... _args)
148 return raise(std::forward<ArgTypes>(_args)...);
152 template <
typename... _ArgTypes>
159 template <
typename... _ArgTypes>
166 template <
typename... _ArgTypes>
169 auto newConnectionBody = std::make_shared<ConnectionBodyType>(_slot);
170 mConnectionBodies.insert(newConnectionBody);
172 return Connection(std::move(newConnectionBody));
176 template <
typename... _ArgTypes>
179 auto newConnectionBody
180 = std::make_shared<ConnectionBodyType>(std::forward<SlotType>(_slot));
181 mConnectionBodies.insert(newConnectionBody);
183 return Connection(std::move(newConnectionBody));
187 template <
typename... _ArgTypes>
188 void Signal<void (_ArgTypes...)>::disconnect(
195 template <
typename... _ArgTypes>
196 void Signal<void (_ArgTypes...)>::disconnectAll()
198 mConnectionBodies.clear();
202 template <
typename... _ArgTypes>
203 void Signal<void (_ArgTypes...)>::cleanupConnections()
206 for (
const auto& connectionBody : mConnectionBodies)
208 if (!connectionBody->isConnected())
209 mConnectionBodies.erase(connectionBody);
214 template <
typename... _ArgTypes>
215 std::size_t
Signal<void (_ArgTypes...)>::getNumConnections()
const
217 std::size_t numConnections = 0;
220 for (
const auto& connectionBody : mConnectionBodies)
222 if (connectionBody->isConnected())
226 return numConnections;
230 template <
typename... _ArgTypes>
231 template <
typename... ArgTypes>
232 void Signal<void (_ArgTypes...)>::raise(ArgTypes&&... _args)
234 for (
auto itr = mConnectionBodies.begin(); itr != mConnectionBodies.end(); )
236 if ((*itr)->isConnected())
238 (*itr)->getSlot()(std::forward<ArgTypes>(_args)...);
243 mConnectionBodies.erase(itr++);
249 template <
typename... _ArgTypes>
250 template <
typename... ArgTypes>
251 void Signal<void (_ArgTypes...)>::operator()(ArgTypes&&... _args)
253 raise(std::forward<ArgTypes>(_args)...);
257 template <
typename T>
265 template <
typename T>
268 return mSignal.connect(_slot);
class Connection
Definition: Signal.hpp:47
void disconnect() const
Disconnect the connection.
Definition: Signal.cpp:107
std::function< ResultType(_ArgTypes...)> SlotType
Definition: Signal.hpp:114
std::function< void(_ArgTypes...)> SlotType
Definition: Signal.hpp:164
Definition: Signal.hpp:106
SlotRegister(typename T::SignalType &_signal)
Constructor given signal.
Definition: Signal.hpp:258
typename T::SlotType SlotType
Definition: Signal.hpp:216
Connection connect(const SlotType &_slot)
Connect a slot to the signal.
Definition: Signal.hpp:266
Definition: BulletCollisionDetector.cpp:63