henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | // sigslot.h: Signal/Slot classes |
| 2 | // |
| 3 | // Written by Sarah Thompson (sarah@telergy.com) 2002. |
| 4 | // |
deadbeef | d0ffa86 | 2017-05-12 22:52:51 -0700 | [diff] [blame] | 5 | // License: Public domain. You are free to use this code however you like, with |
| 6 | // the proviso that the author takes on no responsibility or liability for any |
| 7 | // use. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 8 | // |
| 9 | // QUICK DOCUMENTATION |
| 10 | // |
deadbeef | d0ffa86 | 2017-05-12 22:52:51 -0700 | [diff] [blame] | 11 | // (see also the full documentation at http://sigslot.sourceforge.net/) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | // |
deadbeef | d0ffa86 | 2017-05-12 22:52:51 -0700 | [diff] [blame] | 13 | // #define switches |
| 14 | // SIGSLOT_PURE_ISO: |
| 15 | // Define this to force ISO C++ compliance. This also disables all of |
| 16 | // the thread safety support on platforms where it is available. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | // |
deadbeef | d0ffa86 | 2017-05-12 22:52:51 -0700 | [diff] [blame] | 18 | // SIGSLOT_USE_POSIX_THREADS: |
| 19 | // Force use of Posix threads when using a C++ compiler other than gcc |
| 20 | // on a platform that supports Posix threads. (When using gcc, this is |
| 21 | // the default - use SIGSLOT_PURE_ISO to disable this if necessary) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | // |
deadbeef | d0ffa86 | 2017-05-12 22:52:51 -0700 | [diff] [blame] | 23 | // SIGSLOT_DEFAULT_MT_POLICY: |
| 24 | // Where thread support is enabled, this defaults to |
| 25 | // multi_threaded_global. Otherwise, the default is single_threaded. |
| 26 | // #define this yourself to override the default. In pure ISO mode, |
| 27 | // anything other than single_threaded will cause a compiler error. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 28 | // |
deadbeef | d0ffa86 | 2017-05-12 22:52:51 -0700 | [diff] [blame] | 29 | // PLATFORM NOTES |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 30 | // |
deadbeef | d0ffa86 | 2017-05-12 22:52:51 -0700 | [diff] [blame] | 31 | // Win32: |
| 32 | // On Win32, the WEBRTC_WIN symbol must be #defined. Most mainstream |
| 33 | // compilers do this by default, but you may need to define it yourself |
| 34 | // if your build environment is less standard. This causes the Win32 |
| 35 | // thread support to be compiled in and used automatically. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | // |
deadbeef | d0ffa86 | 2017-05-12 22:52:51 -0700 | [diff] [blame] | 37 | // Unix/Linux/BSD, etc.: |
| 38 | // If you're using gcc, it is assumed that you have Posix threads |
| 39 | // available, so they are used automatically. You can override this (as |
| 40 | // under Windows) with the SIGSLOT_PURE_ISO switch. If you're using |
| 41 | // something other than gcc but still want to use Posix threads, you |
| 42 | // need to #define SIGSLOT_USE_POSIX_THREADS. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 43 | // |
deadbeef | d0ffa86 | 2017-05-12 22:52:51 -0700 | [diff] [blame] | 44 | // ISO C++: |
| 45 | // If none of the supported platforms are detected, or if |
| 46 | // SIGSLOT_PURE_ISO is defined, all multithreading support is turned |
| 47 | // off, along with any code that might cause a pure ISO C++ environment |
| 48 | // to complain. Before you ask, gcc -ansi -pedantic won't compile this |
| 49 | // library, but gcc -ansi is fine. Pedantic mode seems to throw a lot of |
| 50 | // errors that aren't really there. If you feel like investigating this, |
| 51 | // please contact the author. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 52 | // |
| 53 | // |
deadbeef | d0ffa86 | 2017-05-12 22:52:51 -0700 | [diff] [blame] | 54 | // THREADING MODES |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 55 | // |
deadbeef | d0ffa86 | 2017-05-12 22:52:51 -0700 | [diff] [blame] | 56 | // single_threaded: |
| 57 | // Your program is assumed to be single threaded from the point of view |
| 58 | // of signal/slot usage (i.e. all objects using signals and slots are |
| 59 | // created and destroyed from a single thread). Behaviour if objects are |
| 60 | // destroyed concurrently is undefined (i.e. you'll get the occasional |
| 61 | // segmentation fault/memory exception). |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 62 | // |
deadbeef | d0ffa86 | 2017-05-12 22:52:51 -0700 | [diff] [blame] | 63 | // multi_threaded_global: |
| 64 | // Your program is assumed to be multi threaded. Objects using signals |
| 65 | // and slots can be safely created and destroyed from any thread, even |
| 66 | // when connections exist. In multi_threaded_global mode, this is |
| 67 | // achieved by a single global mutex (actually a critical section on |
| 68 | // Windows because they are faster). This option uses less OS resources, |
| 69 | // but results in more opportunities for contention, possibly resulting |
| 70 | // in more context switches than are strictly necessary. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 71 | // |
deadbeef | d0ffa86 | 2017-05-12 22:52:51 -0700 | [diff] [blame] | 72 | // multi_threaded_local: |
| 73 | // Behaviour in this mode is essentially the same as |
| 74 | // multi_threaded_global, except that each signal, and each object that |
| 75 | // inherits has_slots, all have their own mutex/critical section. In |
| 76 | // practice, this means that mutex collisions (and hence context |
| 77 | // switches) only happen if they are absolutely essential. However, on |
| 78 | // some platforms, creating a lot of mutexes can slow down the whole OS, |
| 79 | // so use this option with care. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 80 | // |
deadbeef | d0ffa86 | 2017-05-12 22:52:51 -0700 | [diff] [blame] | 81 | // USING THE LIBRARY |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 82 | // |
deadbeef | d0ffa86 | 2017-05-12 22:52:51 -0700 | [diff] [blame] | 83 | // See the full documentation at http://sigslot.sourceforge.net/ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 84 | // |
| 85 | // Libjingle specific: |
deadbeef | d0ffa86 | 2017-05-12 22:52:51 -0700 | [diff] [blame] | 86 | // |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 87 | // This file has been modified such that has_slots and signalx do not have to be |
| 88 | // using the same threading requirements. E.g. it is possible to connect a |
| 89 | // has_slots<single_threaded> and signal0<multi_threaded_local> or |
| 90 | // has_slots<multi_threaded_local> and signal0<single_threaded>. |
| 91 | // If has_slots is single threaded the user must ensure that it is not trying |
| 92 | // to connect or disconnect to signalx concurrently or data race may occur. |
| 93 | // If signalx is single threaded the user must ensure that disconnect, connect |
| 94 | // or signal is not happening concurrently or data race may occur. |
| 95 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 96 | #ifndef RTC_BASE_SIGSLOT_H_ |
| 97 | #define RTC_BASE_SIGSLOT_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 98 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 99 | #include <stdlib.h> |
| 100 | #include <cstring> |
| 101 | #include <list> |
| 102 | #include <set> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 103 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 104 | // On our copy of sigslot.h, we set single threading as default. |
| 105 | #define SIGSLOT_DEFAULT_MT_POLICY single_threaded |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 106 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 107 | #if defined(SIGSLOT_PURE_ISO) || \ |
| 108 | (!defined(WEBRTC_WIN) && !defined(__GNUG__) && \ |
| 109 | !defined(SIGSLOT_USE_POSIX_THREADS)) |
| 110 | #define _SIGSLOT_SINGLE_THREADED |
| 111 | #elif defined(WEBRTC_WIN) |
| 112 | #define _SIGSLOT_HAS_WIN32_THREADS |
| 113 | #if !defined(WIN32_LEAN_AND_MEAN) |
| 114 | #define WIN32_LEAN_AND_MEAN |
| 115 | #endif |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 116 | #include "rtc_base/win32.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 117 | #elif defined(__GNUG__) || defined(SIGSLOT_USE_POSIX_THREADS) |
| 118 | #define _SIGSLOT_HAS_POSIX_THREADS |
| 119 | #include <pthread.h> |
| 120 | #else |
| 121 | #define _SIGSLOT_SINGLE_THREADED |
| 122 | #endif |
| 123 | |
| 124 | #ifndef SIGSLOT_DEFAULT_MT_POLICY |
| 125 | #ifdef _SIGSLOT_SINGLE_THREADED |
| 126 | #define SIGSLOT_DEFAULT_MT_POLICY single_threaded |
| 127 | #else |
| 128 | #define SIGSLOT_DEFAULT_MT_POLICY multi_threaded_local |
| 129 | #endif |
| 130 | #endif |
| 131 | |
| 132 | // TODO: change this namespace to rtc? |
| 133 | namespace sigslot { |
| 134 | |
| 135 | class single_threaded { |
| 136 | public: |
| 137 | void lock() {} |
| 138 | void unlock() {} |
| 139 | }; |
| 140 | |
| 141 | #ifdef _SIGSLOT_HAS_WIN32_THREADS |
| 142 | // The multi threading policies only get compiled in if they are enabled. |
| 143 | class multi_threaded_global { |
| 144 | public: |
| 145 | multi_threaded_global() { |
| 146 | static bool isinitialised = false; |
| 147 | |
| 148 | if (!isinitialised) { |
| 149 | InitializeCriticalSection(get_critsec()); |
| 150 | isinitialised = true; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void lock() { EnterCriticalSection(get_critsec()); } |
| 155 | |
| 156 | void unlock() { LeaveCriticalSection(get_critsec()); } |
| 157 | |
| 158 | private: |
| 159 | CRITICAL_SECTION* get_critsec() { |
| 160 | static CRITICAL_SECTION g_critsec; |
| 161 | return &g_critsec; |
| 162 | } |
| 163 | }; |
| 164 | |
| 165 | class multi_threaded_local { |
| 166 | public: |
| 167 | multi_threaded_local() { InitializeCriticalSection(&m_critsec); } |
| 168 | |
| 169 | multi_threaded_local(const multi_threaded_local&) { |
| 170 | InitializeCriticalSection(&m_critsec); |
| 171 | } |
| 172 | |
| 173 | ~multi_threaded_local() { DeleteCriticalSection(&m_critsec); } |
| 174 | |
| 175 | void lock() { EnterCriticalSection(&m_critsec); } |
| 176 | |
| 177 | void unlock() { LeaveCriticalSection(&m_critsec); } |
| 178 | |
| 179 | private: |
| 180 | CRITICAL_SECTION m_critsec; |
| 181 | }; |
| 182 | #endif // _SIGSLOT_HAS_WIN32_THREADS |
| 183 | |
| 184 | #ifdef _SIGSLOT_HAS_POSIX_THREADS |
| 185 | // The multi threading policies only get compiled in if they are enabled. |
| 186 | class multi_threaded_global { |
| 187 | public: |
| 188 | void lock() { pthread_mutex_lock(get_mutex()); } |
| 189 | void unlock() { pthread_mutex_unlock(get_mutex()); } |
| 190 | |
| 191 | private: |
| 192 | static pthread_mutex_t* get_mutex(); |
| 193 | }; |
| 194 | |
| 195 | class multi_threaded_local { |
| 196 | public: |
| 197 | multi_threaded_local() { pthread_mutex_init(&m_mutex, nullptr); } |
| 198 | multi_threaded_local(const multi_threaded_local&) { |
| 199 | pthread_mutex_init(&m_mutex, nullptr); |
| 200 | } |
| 201 | ~multi_threaded_local() { pthread_mutex_destroy(&m_mutex); } |
| 202 | void lock() { pthread_mutex_lock(&m_mutex); } |
| 203 | void unlock() { pthread_mutex_unlock(&m_mutex); } |
| 204 | |
| 205 | private: |
| 206 | pthread_mutex_t m_mutex; |
| 207 | }; |
| 208 | #endif // _SIGSLOT_HAS_POSIX_THREADS |
| 209 | |
| 210 | template <class mt_policy> |
| 211 | class lock_block { |
| 212 | public: |
| 213 | mt_policy* m_mutex; |
| 214 | |
| 215 | lock_block(mt_policy* mtx) : m_mutex(mtx) { m_mutex->lock(); } |
| 216 | |
| 217 | ~lock_block() { m_mutex->unlock(); } |
| 218 | }; |
| 219 | |
| 220 | class _signal_base_interface; |
| 221 | |
| 222 | class has_slots_interface { |
| 223 | private: |
| 224 | typedef void (*signal_connect_t)(has_slots_interface* self, |
| 225 | _signal_base_interface* sender); |
| 226 | typedef void (*signal_disconnect_t)(has_slots_interface* self, |
| 227 | _signal_base_interface* sender); |
| 228 | typedef void (*disconnect_all_t)(has_slots_interface* self); |
| 229 | |
| 230 | const signal_connect_t m_signal_connect; |
| 231 | const signal_disconnect_t m_signal_disconnect; |
| 232 | const disconnect_all_t m_disconnect_all; |
| 233 | |
| 234 | protected: |
| 235 | has_slots_interface(signal_connect_t conn, |
| 236 | signal_disconnect_t disc, |
| 237 | disconnect_all_t disc_all) |
| 238 | : m_signal_connect(conn), |
| 239 | m_signal_disconnect(disc), |
| 240 | m_disconnect_all(disc_all) {} |
| 241 | |
| 242 | // Doesn't really need to be virtual, but is for backwards compatibility |
| 243 | // (it was virtual in a previous version of sigslot). |
| 244 | virtual ~has_slots_interface() {} |
| 245 | |
| 246 | public: |
| 247 | void signal_connect(_signal_base_interface* sender) { |
| 248 | m_signal_connect(this, sender); |
| 249 | } |
| 250 | |
| 251 | void signal_disconnect(_signal_base_interface* sender) { |
| 252 | m_signal_disconnect(this, sender); |
| 253 | } |
| 254 | |
| 255 | void disconnect_all() { m_disconnect_all(this); } |
| 256 | }; |
| 257 | |
| 258 | class _signal_base_interface { |
| 259 | private: |
| 260 | typedef void (*slot_disconnect_t)(_signal_base_interface* self, |
| 261 | has_slots_interface* pslot); |
| 262 | typedef void (*slot_duplicate_t)(_signal_base_interface* self, |
| 263 | const has_slots_interface* poldslot, |
| 264 | has_slots_interface* pnewslot); |
| 265 | |
| 266 | const slot_disconnect_t m_slot_disconnect; |
| 267 | const slot_duplicate_t m_slot_duplicate; |
| 268 | |
| 269 | protected: |
| 270 | _signal_base_interface(slot_disconnect_t disc, slot_duplicate_t dupl) |
| 271 | : m_slot_disconnect(disc), m_slot_duplicate(dupl) {} |
| 272 | |
| 273 | ~_signal_base_interface() {} |
| 274 | |
| 275 | public: |
| 276 | void slot_disconnect(has_slots_interface* pslot) { |
| 277 | m_slot_disconnect(this, pslot); |
| 278 | } |
| 279 | |
| 280 | void slot_duplicate(const has_slots_interface* poldslot, |
| 281 | has_slots_interface* pnewslot) { |
| 282 | m_slot_duplicate(this, poldslot, pnewslot); |
| 283 | } |
| 284 | }; |
| 285 | |
| 286 | class _opaque_connection { |
| 287 | private: |
| 288 | typedef void (*emit_t)(const _opaque_connection*); |
| 289 | template <typename FromT, typename ToT> |
| 290 | union union_caster { |
| 291 | FromT from; |
| 292 | ToT to; |
| 293 | }; |
| 294 | |
| 295 | emit_t pemit; |
| 296 | has_slots_interface* pdest; |
| 297 | // Pointers to member functions may be up to 16 bytes for virtual classes, |
| 298 | // so make sure we have enough space to store it. |
| 299 | unsigned char pmethod[16]; |
| 300 | |
| 301 | public: |
| 302 | template <typename DestT, typename... Args> |
| 303 | _opaque_connection(DestT* pd, void (DestT::*pm)(Args...)) : pdest(pd) { |
| 304 | typedef void (DestT::*pm_t)(Args...); |
| 305 | static_assert(sizeof(pm_t) <= sizeof(pmethod), |
| 306 | "Size of slot function pointer too large."); |
| 307 | |
| 308 | std::memcpy(pmethod, &pm, sizeof(pm_t)); |
| 309 | |
| 310 | typedef void (*em_t)(const _opaque_connection* self, Args...); |
| 311 | union_caster<em_t, emit_t> caster2; |
| 312 | caster2.from = &_opaque_connection::emitter<DestT, Args...>; |
| 313 | pemit = caster2.to; |
| 314 | } |
| 315 | |
| 316 | has_slots_interface* getdest() const { return pdest; } |
| 317 | |
| 318 | _opaque_connection duplicate(has_slots_interface* newtarget) const { |
| 319 | _opaque_connection res = *this; |
| 320 | res.pdest = newtarget; |
| 321 | return res; |
| 322 | } |
| 323 | |
| 324 | // Just calls the stored "emitter" function pointer stored at construction |
| 325 | // time. |
| 326 | template <typename... Args> |
| 327 | void emit(Args... args) const { |
| 328 | typedef void (*em_t)(const _opaque_connection*, Args...); |
| 329 | union_caster<emit_t, em_t> caster; |
| 330 | caster.from = pemit; |
| 331 | (caster.to)(this, args...); |
| 332 | } |
| 333 | |
| 334 | private: |
| 335 | template <typename DestT, typename... Args> |
| 336 | static void emitter(const _opaque_connection* self, Args... args) { |
| 337 | typedef void (DestT::*pm_t)(Args...); |
| 338 | pm_t pm; |
| 339 | std::memcpy(&pm, self->pmethod, sizeof(pm_t)); |
| 340 | (static_cast<DestT*>(self->pdest)->*(pm))(args...); |
| 341 | } |
| 342 | }; |
| 343 | |
| 344 | template <class mt_policy> |
| 345 | class _signal_base : public _signal_base_interface, public mt_policy { |
| 346 | protected: |
| 347 | typedef std::list<_opaque_connection> connections_list; |
| 348 | |
| 349 | _signal_base() |
| 350 | : _signal_base_interface(&_signal_base::do_slot_disconnect, |
| 351 | &_signal_base::do_slot_duplicate), |
| 352 | m_current_iterator(m_connected_slots.end()) {} |
| 353 | |
| 354 | ~_signal_base() { disconnect_all(); } |
| 355 | |
| 356 | private: |
| 357 | _signal_base& operator=(_signal_base const& that); |
| 358 | |
| 359 | public: |
| 360 | _signal_base(const _signal_base& o) |
| 361 | : _signal_base_interface(&_signal_base::do_slot_disconnect, |
| 362 | &_signal_base::do_slot_duplicate), |
| 363 | m_current_iterator(m_connected_slots.end()) { |
| 364 | lock_block<mt_policy> lock(this); |
| 365 | for (const auto& connection : o.m_connected_slots) { |
| 366 | connection.getdest()->signal_connect(this); |
| 367 | m_connected_slots.push_back(connection); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | bool is_empty() { |
| 372 | lock_block<mt_policy> lock(this); |
| 373 | return m_connected_slots.empty(); |
| 374 | } |
| 375 | |
| 376 | void disconnect_all() { |
| 377 | lock_block<mt_policy> lock(this); |
| 378 | |
| 379 | while (!m_connected_slots.empty()) { |
| 380 | has_slots_interface* pdest = m_connected_slots.front().getdest(); |
| 381 | m_connected_slots.pop_front(); |
| 382 | pdest->signal_disconnect(static_cast<_signal_base_interface*>(this)); |
| 383 | } |
| 384 | // If disconnect_all is called while the signal is firing, advance the |
| 385 | // current slot iterator to the end to avoid an invalidated iterator from |
| 386 | // being dereferenced. |
| 387 | m_current_iterator = m_connected_slots.end(); |
| 388 | } |
| 389 | |
| 390 | #if !defined(NDEBUG) |
| 391 | bool connected(has_slots_interface* pclass) { |
| 392 | lock_block<mt_policy> lock(this); |
| 393 | connections_list::const_iterator it = m_connected_slots.begin(); |
| 394 | connections_list::const_iterator itEnd = m_connected_slots.end(); |
| 395 | while (it != itEnd) { |
| 396 | if (it->getdest() == pclass) |
| 397 | return true; |
| 398 | ++it; |
| 399 | } |
| 400 | return false; |
| 401 | } |
| 402 | #endif |
| 403 | |
| 404 | void disconnect(has_slots_interface* pclass) { |
| 405 | lock_block<mt_policy> lock(this); |
| 406 | connections_list::iterator it = m_connected_slots.begin(); |
| 407 | connections_list::iterator itEnd = m_connected_slots.end(); |
| 408 | |
| 409 | while (it != itEnd) { |
| 410 | if (it->getdest() == pclass) { |
| 411 | // If we're currently using this iterator because the signal is firing, |
| 412 | // advance it to avoid it being invalidated. |
| 413 | if (m_current_iterator == it) { |
| 414 | m_current_iterator = m_connected_slots.erase(it); |
| 415 | } else { |
| 416 | m_connected_slots.erase(it); |
| 417 | } |
| 418 | pclass->signal_disconnect(static_cast<_signal_base_interface*>(this)); |
| 419 | return; |
| 420 | } |
| 421 | ++it; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | private: |
| 426 | static void do_slot_disconnect(_signal_base_interface* p, |
| 427 | has_slots_interface* pslot) { |
| 428 | _signal_base* const self = static_cast<_signal_base*>(p); |
| 429 | lock_block<mt_policy> lock(self); |
| 430 | connections_list::iterator it = self->m_connected_slots.begin(); |
| 431 | connections_list::iterator itEnd = self->m_connected_slots.end(); |
| 432 | |
| 433 | while (it != itEnd) { |
| 434 | connections_list::iterator itNext = it; |
| 435 | ++itNext; |
| 436 | |
| 437 | if (it->getdest() == pslot) { |
| 438 | // If we're currently using this iterator because the signal is firing, |
| 439 | // advance it to avoid it being invalidated. |
| 440 | if (self->m_current_iterator == it) { |
| 441 | self->m_current_iterator = self->m_connected_slots.erase(it); |
| 442 | } else { |
| 443 | self->m_connected_slots.erase(it); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | it = itNext; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | static void do_slot_duplicate(_signal_base_interface* p, |
| 452 | const has_slots_interface* oldtarget, |
| 453 | has_slots_interface* newtarget) { |
| 454 | _signal_base* const self = static_cast<_signal_base*>(p); |
| 455 | lock_block<mt_policy> lock(self); |
| 456 | connections_list::iterator it = self->m_connected_slots.begin(); |
| 457 | connections_list::iterator itEnd = self->m_connected_slots.end(); |
| 458 | |
| 459 | while (it != itEnd) { |
| 460 | if (it->getdest() == oldtarget) { |
| 461 | self->m_connected_slots.push_back(it->duplicate(newtarget)); |
| 462 | } |
| 463 | |
| 464 | ++it; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | protected: |
| 469 | connections_list m_connected_slots; |
| 470 | |
| 471 | // Used to handle a slot being disconnected while a signal is |
| 472 | // firing (iterating m_connected_slots). |
| 473 | connections_list::iterator m_current_iterator; |
| 474 | bool m_erase_current_iterator = false; |
| 475 | }; |
| 476 | |
| 477 | template <class mt_policy = SIGSLOT_DEFAULT_MT_POLICY> |
| 478 | class has_slots : public has_slots_interface, public mt_policy { |
| 479 | private: |
| 480 | typedef std::set<_signal_base_interface*> sender_set; |
| 481 | typedef sender_set::const_iterator const_iterator; |
| 482 | |
| 483 | public: |
| 484 | has_slots() |
| 485 | : has_slots_interface(&has_slots::do_signal_connect, |
| 486 | &has_slots::do_signal_disconnect, |
| 487 | &has_slots::do_disconnect_all) {} |
| 488 | |
| 489 | has_slots(has_slots const& o) |
| 490 | : has_slots_interface(&has_slots::do_signal_connect, |
| 491 | &has_slots::do_signal_disconnect, |
| 492 | &has_slots::do_disconnect_all) { |
| 493 | lock_block<mt_policy> lock(this); |
| 494 | for (auto* sender : o.m_senders) { |
| 495 | sender->slot_duplicate(&o, this); |
| 496 | m_senders.insert(sender); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | ~has_slots() { this->disconnect_all(); } |
| 501 | |
| 502 | private: |
| 503 | has_slots& operator=(has_slots const&); |
| 504 | |
| 505 | static void do_signal_connect(has_slots_interface* p, |
| 506 | _signal_base_interface* sender) { |
| 507 | has_slots* const self = static_cast<has_slots*>(p); |
| 508 | lock_block<mt_policy> lock(self); |
| 509 | self->m_senders.insert(sender); |
| 510 | } |
| 511 | |
| 512 | static void do_signal_disconnect(has_slots_interface* p, |
| 513 | _signal_base_interface* sender) { |
| 514 | has_slots* const self = static_cast<has_slots*>(p); |
| 515 | lock_block<mt_policy> lock(self); |
| 516 | self->m_senders.erase(sender); |
| 517 | } |
| 518 | |
| 519 | static void do_disconnect_all(has_slots_interface* p) { |
| 520 | has_slots* const self = static_cast<has_slots*>(p); |
| 521 | lock_block<mt_policy> lock(self); |
| 522 | while (!self->m_senders.empty()) { |
| 523 | std::set<_signal_base_interface*> senders; |
| 524 | senders.swap(self->m_senders); |
| 525 | const_iterator it = senders.begin(); |
| 526 | const_iterator itEnd = senders.end(); |
| 527 | |
| 528 | while (it != itEnd) { |
| 529 | _signal_base_interface* s = *it; |
| 530 | ++it; |
| 531 | s->slot_disconnect(p); |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | private: |
| 537 | sender_set m_senders; |
| 538 | }; |
| 539 | |
| 540 | template <class mt_policy, typename... Args> |
| 541 | class signal_with_thread_policy : public _signal_base<mt_policy> { |
| 542 | private: |
| 543 | typedef _signal_base<mt_policy> base; |
| 544 | |
| 545 | protected: |
| 546 | typedef typename base::connections_list connections_list; |
| 547 | |
| 548 | public: |
| 549 | signal_with_thread_policy() {} |
| 550 | |
| 551 | template <class desttype> |
| 552 | void connect(desttype* pclass, void (desttype::*pmemfun)(Args...)) { |
| 553 | lock_block<mt_policy> lock(this); |
| 554 | this->m_connected_slots.push_back(_opaque_connection(pclass, pmemfun)); |
| 555 | pclass->signal_connect(static_cast<_signal_base_interface*>(this)); |
| 556 | } |
| 557 | |
| 558 | void emit(Args... args) { |
| 559 | lock_block<mt_policy> lock(this); |
| 560 | this->m_current_iterator = this->m_connected_slots.begin(); |
| 561 | while (this->m_current_iterator != this->m_connected_slots.end()) { |
| 562 | _opaque_connection const& conn = *this->m_current_iterator; |
| 563 | ++(this->m_current_iterator); |
| 564 | conn.emit<Args...>(args...); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | void operator()(Args... args) { emit(args...); } |
| 569 | }; |
| 570 | |
| 571 | // Alias with default thread policy. Needed because both default arguments |
| 572 | // and variadic template arguments must go at the end of the list, so we |
| 573 | // can't have both at once. |
| 574 | template <typename... Args> |
| 575 | using signal = signal_with_thread_policy<SIGSLOT_DEFAULT_MT_POLICY, Args...>; |
| 576 | |
| 577 | // The previous verion of sigslot didn't use variadic templates, so you would |
| 578 | // need to write "sigslot::signal2<Arg1, Arg2>", for example. |
| 579 | // Now you can just write "sigslot::signal<Arg1, Arg2>", but these aliases |
| 580 | // exist for backwards compatibility. |
| 581 | template <typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY> |
| 582 | using signal0 = signal_with_thread_policy<mt_policy>; |
| 583 | |
| 584 | template <typename A1, typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY> |
| 585 | using signal1 = signal_with_thread_policy<mt_policy, A1>; |
| 586 | |
| 587 | template <typename A1, |
| 588 | typename A2, |
| 589 | typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY> |
| 590 | using signal2 = signal_with_thread_policy<mt_policy, A1, A2>; |
| 591 | |
| 592 | template <typename A1, |
| 593 | typename A2, |
| 594 | typename A3, |
| 595 | typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY> |
| 596 | using signal3 = signal_with_thread_policy<mt_policy, A1, A2, A3>; |
| 597 | |
| 598 | template <typename A1, |
| 599 | typename A2, |
| 600 | typename A3, |
| 601 | typename A4, |
| 602 | typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY> |
| 603 | using signal4 = signal_with_thread_policy<mt_policy, A1, A2, A3, A4>; |
| 604 | |
| 605 | template <typename A1, |
| 606 | typename A2, |
| 607 | typename A3, |
| 608 | typename A4, |
| 609 | typename A5, |
| 610 | typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY> |
| 611 | using signal5 = signal_with_thread_policy<mt_policy, A1, A2, A3, A4, A5>; |
| 612 | |
| 613 | template <typename A1, |
| 614 | typename A2, |
| 615 | typename A3, |
| 616 | typename A4, |
| 617 | typename A5, |
| 618 | typename A6, |
| 619 | typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY> |
| 620 | using signal6 = signal_with_thread_policy<mt_policy, A1, A2, A3, A4, A5, A6>; |
| 621 | |
| 622 | template <typename A1, |
| 623 | typename A2, |
| 624 | typename A3, |
| 625 | typename A4, |
| 626 | typename A5, |
| 627 | typename A6, |
| 628 | typename A7, |
| 629 | typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY> |
| 630 | using signal7 = |
| 631 | signal_with_thread_policy<mt_policy, A1, A2, A3, A4, A5, A6, A7>; |
| 632 | |
| 633 | template <typename A1, |
| 634 | typename A2, |
| 635 | typename A3, |
| 636 | typename A4, |
| 637 | typename A5, |
| 638 | typename A6, |
| 639 | typename A7, |
| 640 | typename A8, |
| 641 | typename mt_policy = SIGSLOT_DEFAULT_MT_POLICY> |
| 642 | using signal8 = |
| 643 | signal_with_thread_policy<mt_policy, A1, A2, A3, A4, A5, A6, A7, A8>; |
| 644 | |
| 645 | } // namespace sigslot |
| 646 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 647 | #endif // RTC_BASE_SIGSLOT_H_ |