-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal.hpp
2138 lines (1854 loc) · 79.3 KB
/
signal.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include <atomic>
#include <cstring>
#include <future>
#include <memory>
#include <mutex>
#include <type_traits>
#include <utility>
#include <thread>
#include <vector>
#if defined(__GXX_RTTI) || defined(__cpp_rtti) || defined(_CPPRTTI)
#define SIGSLOT_RTTI_ENABLED 1
#include <typeinfo>
#endif
#include <iostream>
#include <assert.h>
namespace core {
class TaskQueue;
}
namespace sigslot {
//class i_executor;
template <typename, typename...>
class signal_base;
namespace detail {
// Used to detect an object of observer type
struct observer_type {};
} // namespace detail
namespace trait {
/// represent a list of types
template <typename...> struct typelist {};
/**
* Pointers that can be converted to a weak pointer concept for tracking
* purpose must implement the to_weak() function in order to make use of
* ADL to convert that type and make it usable
*/
template <typename T>
std::weak_ptr<T> to_weak(std::weak_ptr<T> w) {
return w;
}
template <typename T>
std::weak_ptr<T> to_weak(std::shared_ptr<T> s) {
return s;
}
// tools
namespace detail {
template <typename...>
struct voider { using type = void; };
// void_t from c++17
template <typename...T>
using void_t = typename detail::voider<T...>::type;
template <typename, typename = void>
struct has_call_operator : std::false_type {};
template <typename F>
struct has_call_operator<F, void_t<decltype(&std::remove_reference<F>::type::operator())>>
: std::true_type {};
template <typename, typename, typename = void, typename = void>
struct is_callable : std::false_type {};
template <typename P, typename F, typename... T>
struct is_callable<P, F, typelist<T...>,
void_t<decltype(((*std::declval<P>()).*std::declval<F>())(std::declval<T>()...))>>
: std::true_type {};
template <typename F, typename... T>
struct is_callable<F, typelist<T...>,
void_t<decltype(std::declval<F>()(std::declval<T>()...))>>
: std::true_type {};
template <typename T, typename = void>
struct is_weak_ptr : std::false_type {};
template <typename T>
struct is_weak_ptr<T, void_t<decltype(std::declval<T>().expired()),
decltype(std::declval<T>().lock()),
decltype(std::declval<T>().reset())>>
: std::true_type {};
template <typename T, typename = void>
struct is_weak_ptr_compatible : std::false_type {};
template <typename T>
struct is_weak_ptr_compatible<T, void_t<decltype(to_weak(std::declval<T>()))>>
: is_weak_ptr<decltype(to_weak(std::declval<T>()))> {};
template <typename...>
struct is_signal : std::false_type {};
template <typename L, typename... T>
struct is_signal<signal_base<L, T...>>
: std::true_type {};
} // namespace detail
static constexpr bool with_rtti =
#ifdef SIGSLOT_RTTI_ENABLED
true;
#else
false;
#endif
/// determine if a pointer is convertible into a "weak" pointer
template <typename P>
constexpr bool is_weak_ptr_compatible_v = detail::is_weak_ptr_compatible<std::decay_t<P>>::value;
/// determine if a type T (Callable or Pmf) is callable with supplied arguments
template <typename L, typename... T>
constexpr bool is_callable_v = detail::is_callable<T..., L>::value;
template <typename T>
constexpr bool is_weak_ptr_v = detail::is_weak_ptr<T>::value;
template <typename T>
constexpr bool has_call_operator_v = detail::has_call_operator<T>::value;
template <typename T>
constexpr bool is_pointer_v = std::is_pointer<T>::value;
template <typename T>
constexpr bool is_func_v = std::is_function<T>::value;
template <typename T>
constexpr bool is_pmf_v = std::is_member_function_pointer<T>::value;
template <typename T>
constexpr bool is_observer_v = std::is_base_of<::sigslot::detail::observer_type,
std::remove_pointer_t<std::remove_reference_t<T>>>::value;
template <typename S>
constexpr bool is_signal_v = detail::is_signal<S>::value;
} // namespace trait
enum connection_type {
auto_connection = 0,
direct_connection = 1,
queued_connection = 2,
blocking_queued_connection = 3,
unique_connection = 0x80,
singleshot_connection = 0x100
};
/**
* A group_id is used to identify a group of slots
*/
using group_id = std::int32_t;
namespace detail {
/**
* The following function_traits and object_pointer series of templates are
* used to circumvent the type-erasing that takes place in the slot_base
* implementations. They are used to compare the stored functions and objects
* with another one for disconnection purpose.
*/
/*
* Function pointers and member function pointers size differ from compiler to
* compiler, and for virtual members compared to non virtual members. On some
* compilers, multiple inheritance has an impact too. Hence, we form an union
* big enough to store any kind of function pointer.
*/
namespace mock {
struct a { virtual ~a() = default; void f(); virtual void g(); static void h(); };
struct b { virtual ~b() = default; void f(); virtual void g(); };
struct c : a, b { void f(); void g() override; };
struct d : virtual a { void g() override; };
union fun_types {
decltype(&d::g) dm;
decltype(&c::g) mm;
decltype(&c::g) mvm;
decltype(&a::f) m;
decltype(&a::g) vm;
decltype(&a::h) s;
void (*f)();
void *o;
};
} // namespace mock
/*
* This struct is used to store function pointers.
* This is needed for slot disconnection by function pointer.
* It assumes the underlying implementation to be trivially copiable.
*/
struct func_ptr {
func_ptr()
: sz{0}
{
std::uninitialized_fill(std::begin(data), std::end(data), '\0');
}
template <typename T>
void store(const T& t) {
const auto *b = reinterpret_cast<const char*>(&t);
sz = sizeof(T);
std::memcpy(data, b, sz);
}
template <typename T>
const T* as() const {
if (sizeof(T) != sz) {
return nullptr;
}
return reinterpret_cast<const T*>(data);
}
private:
alignas(sizeof(mock::fun_types)) char data[sizeof(mock::fun_types)];
size_t sz;
};
template <typename T, typename = void>
struct function_traits {
static void ptr(const T& /*t*/, func_ptr& /*d*/) {
}
static bool eq(const T& /*t*/, const func_ptr& /*d*/) {
return false;
}
static constexpr bool is_disconnectable = false;
static constexpr bool must_check_object = true;
};
template <typename T>
struct function_traits<T, std::enable_if_t<trait::is_func_v<T>>> {
static void ptr(T& t, func_ptr& d) {
d.store(&t);
}
static bool eq(T& t, const func_ptr& d) {
const auto *r = d.as<const T*>();
return r && *r == &t;
}
static constexpr bool is_disconnectable = true;
static constexpr bool must_check_object = false;
};
template <typename T>
struct function_traits<T*, std::enable_if_t<trait::is_func_v<T>>> {
static void ptr(T *t, func_ptr& d) {
function_traits<T>::ptr(*t, d);
}
static bool eq(T *t, const func_ptr& d) {
return function_traits<T>::eq(*t, d);
}
static constexpr bool is_disconnectable = true;
static constexpr bool must_check_object = false;
};
template <typename T>
struct function_traits<T, std::enable_if_t<trait::is_pmf_v<T>>> {
static void ptr(T t, func_ptr& d) {
d.store(t);
}
static bool eq(T t, const func_ptr& d) {
const auto *r = d.as<const T>();
return r && *r == t;
}
static constexpr bool is_disconnectable = trait::with_rtti;
static constexpr bool must_check_object = true;
};
// for function objects, the assumption is that we are looking for the call operator
template <typename T>
struct function_traits<T, std::enable_if_t<trait::has_call_operator_v<T>>> {
using call_type = decltype(&std::remove_reference<T>::type::operator());
static void ptr(const T& /*t*/, func_ptr& d) {
function_traits<call_type>::ptr(&T::operator(), d);
}
static bool eq(const T& /*t*/, const func_ptr& d) {
return function_traits<call_type>::eq(&T::operator(), d);
}
static constexpr bool is_disconnectable = function_traits<call_type>::is_disconnectable;
static constexpr bool must_check_object = function_traits<call_type>::must_check_object;
};
template <typename T>
func_ptr get_function_ptr(const T& t) {
func_ptr d;
function_traits<std::decay_t<T>>::ptr(t, d);
return d;
}
template <typename T>
bool eq_function_ptr(const T& t, const func_ptr& d) {
return function_traits<std::decay_t<T>>::eq(t, d);
}
/*
* obj_ptr is used to store a pointer to an object.
* The object_pointer traits are needed to handle trackable objects correctly,
* as they are likely to not be pointers.
*/
using obj_ptr = const void*;
template <typename T>
obj_ptr get_object_ptr(const T& t);
template <typename T, typename = void>
struct object_pointer {
static obj_ptr get(const T&) {
return nullptr;
}
};
template <typename T>
struct object_pointer<T*, std::enable_if_t<trait::is_pointer_v<T*>>> {
static obj_ptr get(const T *t) {
return reinterpret_cast<obj_ptr>(t);
}
};
template <typename T>
struct object_pointer<T, std::enable_if_t<trait::is_weak_ptr_v<T>>> {
static obj_ptr get(const T& t) {
auto p = t.lock();
return get_object_ptr(p);
}
};
template <typename T>
struct object_pointer<T, std::enable_if_t<!trait::is_pointer_v<T> &&
!trait::is_weak_ptr_v<T> &&
trait::is_weak_ptr_compatible_v<T>>>
{
static obj_ptr get(const T& t) {
return t ? reinterpret_cast<obj_ptr>(t.get()) : nullptr;
}
};
template <typename T>
obj_ptr get_object_ptr(const T& t) {
return object_pointer<T>::get(t);
}
// noop mutex for thread-unsafe use
struct null_mutex {
null_mutex() noexcept = default;
~null_mutex() noexcept = default;
null_mutex(const null_mutex&) = delete;
null_mutex& operator=(const null_mutex&) = delete;
null_mutex(null_mutex&&) = delete;
null_mutex& operator=(null_mutex&&) = delete;
inline bool try_lock() noexcept { return true; }
inline void lock() noexcept {}
inline void unlock() noexcept {}
};
/**
* A spin mutex that yields, mostly for use in benchmarks and scenarii that invoke
* slots at a very high pace.
* One should almost always prefer a standard mutex over this.
*/
struct spin_mutex {
spin_mutex() noexcept = default;
~spin_mutex() noexcept = default;
spin_mutex(spin_mutex const&) = delete;
spin_mutex& operator=(const spin_mutex&) = delete;
spin_mutex(spin_mutex&&) = delete;
spin_mutex& operator=(spin_mutex&&) = delete;
void lock() noexcept {
while (true) {
while (!state.load(std::memory_order_relaxed)) {
std::this_thread::yield();
}
if (try_lock()) {
break;
}
}
}
bool try_lock() noexcept {
return state.exchange(false, std::memory_order_acquire);
}
void unlock() noexcept {
state.store(true, std::memory_order_release);
}
private:
std::atomic<bool> state {true};
};
/**
* A simple copy on write container that will be used to improve slot lists
* access efficiency in a multithreaded context.
*/
template <typename T>
class copy_on_write {
struct payload {
payload() = default;
template <typename... Args>
explicit payload(Args&& ...args)
: value(std::forward<Args>(args)...)
{}
std::atomic<std::size_t> count{1};
T value;
};
public:
using element_type = T;
copy_on_write()
: m_data(new payload)
{}
template <typename U>
explicit copy_on_write(U&& x, std::enable_if_t<!std::is_same<std::decay_t<U>,
copy_on_write>::value>* = nullptr)
: m_data(new payload(std::forward<U>(x)))
{}
copy_on_write(const copy_on_write& x) noexcept
: m_data(x.m_data)
{
++m_data->count;
}
copy_on_write(copy_on_write&& x) noexcept
: m_data(x.m_data)
{
x.m_data = nullptr;
}
~copy_on_write() {
if (m_data && (--m_data->count == 0)) {
delete m_data;
}
}
copy_on_write& operator=(const copy_on_write& x) noexcept {
if (&x != this) {
*this = copy_on_write(x);
}
return *this;
}
copy_on_write& operator=(copy_on_write&& x) noexcept {
auto tmp = std::move(x);
swap(*this, tmp);
return *this;
}
element_type& write() {
if (!unique()) {
*this = copy_on_write(read());
}
return m_data->value;
}
const element_type& read() const noexcept {
return m_data->value;
}
friend inline void swap(copy_on_write& x, copy_on_write& y) noexcept {
using std::swap;
swap(x.m_data, y.m_data);
}
private:
bool unique() const noexcept {
return m_data->count == 1;
}
private:
payload *m_data;
};
/**
* Specializations for thread-safe code path
*/
template <typename T>
const T& cow_read(const T& v) {
return v;
}
template <typename T>
const T& cow_read(copy_on_write<T>& v) {
return v.read();
}
template <typename T>
T& cow_write(T& v) {
return v;
}
template <typename T>
T& cow_write(copy_on_write<T>& v) {
return v.write();
}
/**
* std::make_shared instantiates a lot a templates, and makes both compilation time
* and executable size far bigger than they need to be. We offer a make_shared
* equivalent that will avoid most instantiations with the following tradeoffs:
* - Not exception safe,
* - Allocates a separate control block, and will thus make the code slower.
*/
#ifdef SIGSLOT_REDUCE_COMPILE_TIME
template <typename B, typename D, typename ...Arg>
inline std::shared_ptr<B> make_shared(Arg&& ... arg) {
return std::shared_ptr<B>(static_cast<B*>(new D(std::forward<Arg>(arg)...)));
}
#else
template <typename B, typename D, typename ...Arg>
inline std::shared_ptr<B> make_shared(Arg&& ... arg) {
return std::static_pointer_cast<B>(std::make_shared<D>(std::forward<Arg>(arg)...));
}
#endif
// Adapt a signal into a cheap function object, for easy signal chaining
template <typename SigT>
struct signal_wrapper {
template <typename... U>
void operator()(U&& ...u) {
(*m_sig)(std::forward<U>(u)...);
}
SigT *m_sig{};
};
/* slot_state holds slot type independent state, to be used to interact with
* slots indirectly through connection and scoped_connection objects.
*/
class slot_state {
public:
constexpr slot_state(group_id gid) noexcept
: m_index(0)
, m_group(gid)
, m_connected(true)
, m_blocked(false)
{}
virtual ~slot_state() = default;
virtual bool connected() const noexcept { return m_connected; }
bool disconnect() noexcept {
bool ret = m_connected.exchange(false);
if (ret) {
do_disconnect();
}
return ret;
}
bool blocked() const noexcept { return m_blocked.load(); }
void block() noexcept { m_blocked.store(true); }
void unblock() noexcept { m_blocked.store(false); }
protected:
virtual void do_disconnect() {}
auto index() const {
return m_index;
}
auto& index() {
return m_index;
}
group_id group() const {
return m_group;
}
private:
template <typename, typename...>
friend class ::sigslot::signal_base;
std::size_t m_index; // index into the array of slot pointers inside the signal
const group_id m_group; // slot group this slot belongs to
std::atomic<bool> m_connected;
std::atomic<bool> m_blocked;
};
} // namespace detail
/**
* connection_blocker is a RAII object that blocks a connection until destruction
*/
class connection_blocker {
public:
connection_blocker() = default;
~connection_blocker() noexcept { release(); }
connection_blocker(const connection_blocker&) = delete;
connection_blocker& operator=(const connection_blocker&) = delete;
connection_blocker(connection_blocker&& o) noexcept
: m_state{std::move(o.m_state)}
{}
connection_blocker& operator=(connection_blocker&& o) noexcept {
release();
m_state.swap(o.m_state);
return *this;
}
private:
friend class connection;
explicit connection_blocker(std::weak_ptr<detail::slot_state> s) noexcept
: m_state{std::move(s)}
{
if (auto d = m_state.lock()) {
d->block();
}
}
void release() noexcept {
if (auto d = m_state.lock()) {
d->unblock();
}
}
private:
std::weak_ptr<detail::slot_state> m_state;
};
/**
* A connection object allows interaction with an ongoing slot connection
* * It allows common actions such as connection blocking and disconnection.
* Note that connection is not a RAII object, one does not need to hold one
* such object to keep the signal-slot connection alive.
*/
class connection {
public:
connection() = default;
virtual ~connection() = default;
connection(const connection&) noexcept = default;
connection& operator=(const connection&) noexcept = default;
connection(connection&&) noexcept = default;
connection& operator=(connection&&) noexcept = default;
bool valid() const noexcept {
return !m_state.expired();
}
bool connected() const noexcept {
const auto d = m_state.lock();
return d && d->connected();
}
bool disconnect() noexcept {
auto d = m_state.lock();
return d && d->disconnect();
}
bool blocked() const noexcept {
const auto d = m_state.lock();
return d && d->blocked();
}
void block() noexcept {
if (auto d = m_state.lock()) {
d->block();
}
}
void unblock() noexcept {
if (auto d = m_state.lock()) {
d->unblock();
}
}
connection_blocker blocker() const noexcept {
return connection_blocker{m_state};
}
protected:
template <typename, typename...> friend class signal_base;
explicit connection(std::weak_ptr<detail::slot_state> s) noexcept
: m_state{std::move(s)}
{}
protected:
std::weak_ptr<detail::slot_state> m_state;
};
/**
* scoped_connection is a RAII version of connection
* It disconnects the slot from the signal upon destruction.
*/
class scoped_connection final : public connection {
public:
scoped_connection() = default;
~scoped_connection() override {
disconnect();
}
/*implicit*/ scoped_connection(const connection& c) noexcept : connection(c) {}
/*implicit*/ scoped_connection(connection&& c) noexcept : connection(std::move(c)) {}
scoped_connection(const scoped_connection&) noexcept = delete;
scoped_connection& operator=(const scoped_connection&) noexcept = delete;
scoped_connection(scoped_connection&& o) noexcept
: connection{std::move(o.m_state)}
{}
scoped_connection& operator=(scoped_connection&& o) noexcept {
disconnect();
m_state.swap(o.m_state);
return *this;
}
private:
template <typename, typename...> friend class signal_base;
explicit scoped_connection(std::weak_ptr<detail::slot_state> s) noexcept
: connection{std::move(s)}
{}
};
/**
* Observer is a base class for intrusive lifetime tracking of objects.
* * This is an alternative to trackable pointers, such as std::shared_ptr,
* and manual connection management by keeping connection objects in scope.
* Deriving from this class allows automatic disconnection of all the slots
* connected to any signal when an instance is destroyed.
*/
template <typename Lockable>
struct observer_base : private detail::observer_type {
virtual ~observer_base() = default;
protected:
/**
* Disconnect all signals connected to this object.
* * To avoid invocation of slots on a semi-destructed instance, which may happen
* in multi-threaded contexts, derived classes should call this method in their
* destructor. This will ensure proper disconnection prior to the destruction.
*/
void disconnect_all() {
std::unique_lock<Lockable> _{m_mutex};
m_connections.clear();
}
private:
template <typename, typename ...>
friend class signal_base;
void add_connection(connection conn) {
std::unique_lock<Lockable> _{m_mutex};
m_connections.emplace_back(std::move(conn));
}
Lockable m_mutex;
std::vector<scoped_connection> m_connections;
};
/**
* Specialization of observer_base to be used in single threaded contexts.
*/
using observer_st = observer_base<detail::null_mutex>;
/**
* Specialization of observer_base to be used in multi-threaded contexts.
*/
using observer = observer_base<std::mutex>;
namespace detail {
// interface for cleanable objects, used to cleanup disconnected slots
struct cleanable {
virtual ~cleanable() = default;
virtual void clean(slot_state *) = 0;
};
template <typename...>
class slot_base;
template <typename... T>
using slot_ptr = std::shared_ptr<slot_base<T...>>;
/* A base class for slot objects. This base type only depends on slot argument
* types, it will be used as an element in an intrusive singly-linked list of
* slots, hence the public next member.
*/
template <typename... Args>
class slot_base : public slot_state {
public:
using base_types = trait::typelist<Args...>;
explicit slot_base(cleanable& c, uint32_t type, core::TaskQueue* queue, group_id gid)
: slot_state(gid)
, m_cleaner(c)
, m_queue(queue) {
m_singleshot = type & connection_type::singleshot_connection;
uint32_t t = type;
t &= ~connection_type::unique_connection;
t &= ~connection_type::singleshot_connection;
m_type = t;
}
~slot_base() override = default;
// method effectively responsible for calling the "slot" function with
// supplied arguments whenever emission happens.
virtual void call_slot(Args...) = 0;
template <typename... U>
void operator()(U&& ...u) {
if (slot_state::connected() && !slot_state::blocked()) {
call_slot(std::forward<U>(u)...);
}
}
// check if we are storing callable c
template <typename C>
bool has_callable(const C& c) const {
auto p = get_callable();
return eq_function_ptr(c, p);
}
template <typename C>
std::enable_if_t<function_traits<C>::must_check_object, bool>
has_full_callable(const C& c) const {
return has_callable(c) && check_class_type<std::decay_t<C>>();
}
template <typename C>
std::enable_if_t<!function_traits<C>::must_check_object, bool>
has_full_callable(const C& c) const {
return has_callable(c);
}
// check if we are storing object o
template <typename O>
bool has_object(const O& o) const {
return get_object() == get_object_ptr(o);
}
void set_unique(bool unique) {
this->m_unique = unique;
}
bool is_unique() {
return this->m_unique;
}
protected:
void do_disconnect() final {
m_cleaner.clean(this);
}
// retieve a pointer to the object embedded in the slot
virtual obj_ptr get_object() const noexcept {
return nullptr;
}
// retieve a pointer to the callable embedded in the slot
virtual func_ptr get_callable() const noexcept {
return get_function_ptr(nullptr);
}
inline bool can_emit() {
if (this->m_singleshot && this->m_emitted) {
return false;
}
return true;
}
inline void set_emitted() {
if (this->m_singleshot && !this->m_emitted) {
this->m_emitted = true;
}
}
inline bool is_current() {
assert(this->m_queue);
auto is_current = false;
if (this->m_queue->IsCurrent()) {
is_current = true;
}
return is_current;
}
uint32_t type() {
uint32_t type = this->m_type;
if (type == connection_type::auto_connection) {
if (is_current()) {
type = connection_type::direct_connection;
} else {
type = connection_type::queued_connection;
}
}
return type;
}
#ifdef SIGSLOT_RTTI_ENABLED
// retieve a pointer to the callable embedded in the slot
virtual const std::type_info& get_callable_type() const noexcept {
return typeid(nullptr);
}
private:
template <typename U>
bool check_class_type() const {
return typeid(U) == get_callable_type();
}
#else
template <typename U>
bool check_class_type() const {
return false;
}
#endif
protected:
std::atomic<uint32_t> m_type = {0};
std::atomic_bool m_unique = {false};
core::TaskQueue* m_queue = nullptr;
std::atomic_bool m_singleshot = {false};
std::atomic_bool m_emitted = {false};
private:
cleanable& m_cleaner;
};
/*
* A slot object holds state information, and a callable to to be called
* whenever the function call operator of its slot_base base class is called.
*/
template <typename Func, typename... Args>
class slot final : public slot_base<Args...>, public std::enable_shared_from_this<slot<Func, Args...>> {
public:
using this_type = slot<Func, Args...>;
template <typename F, typename Gid>
constexpr slot(cleanable& c, F&& f, uint32_t type, core::TaskQueue* queue, Gid gid)
: slot_base<Args...>(c, type, queue, gid)
, func{std::forward<F>(f)} {}
protected:
void call_slot(Args ...args) override {
if (!this->can_emit()) {
return;
}
this->set_emitted();
uint32_t type = this->type();
if (type == connection_type::direct_connection) {
if (this->slot_state::connected()) {
func(args...);
if (this->m_singleshot && this->m_emitted) {
this->slot_state::disconnect();
}
} else {
std::cerr << "canceling slot execution due to connection being disconnected" << std::endl;
}
} else if (type == connection_type::queued_connection) {
assert(this->m_queue);
if (this->m_queue) {
this->m_queue->PostTask([wself = std::weak_ptr<this_type>(this_type::shared_from_this()), args...]() mutable {
auto self = wself.lock();
if (!self) {
return;
}
if (self->slot_state::connected()) {
self->func(args...);
if (self->m_singleshot && self->m_emitted) {