-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsignals.hpp
1868 lines (1555 loc) · 60.4 KB
/
signals.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
// Copyright Jeremiah van Oosten 2020.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
//
/**
* @file signals.hpp
* @date March 24, 2020
* @author Jeremiah van Oosten
*
* @brief The signals header file.
* @see https://www.boost.org/doc/libs/1_72_0/doc/html/signals2.html
* @see https://github.com/palacaze/sigslot
*/
#include "optional.hpp" // for opt::optional
#include <atomic> // for std::atomic_bool
#include <cstddef> // for std::size_t and std::nullptr_t
#include <exception> // for std::exception
#include <functional> // for std::reference_wrapper
#include <memory> // for std::unique_ptr
#include <mutex> // for std::mutex, and std::lock_guard
#include <tuple> // for std::tuple, and std::make_tuple
#include <type_traits> // for std::decay, and std::enable_if
#include <utility> // for std::declval.
#include <vector> // for std::vector
namespace sig
{
// An exception of type not_comparable_exception is thrown
// if one tries to compare non comparable function types.
class not_comparable_exception : public std::exception
{};
// Pointers that can be converted to a weak pointer concept for
// tracking purposes must implement the to_weak() function in order
// to make use of Argument-dependent lookup (ADL) and to convert
// it to a type whose lifetime can be tracked by the slot.
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;
}
// Forward declare signal class so that it can be
// a friend of a class in a nested namespace.
template<typename, typename>
class signal;
namespace detail
{
namespace traits
{
// Since C++17
// @see https://en.cppreference.com/w/cpp/types/void_t
template<typename... Ts>
struct make_void
{
typedef void type;
};
template<typename... Ts>
using void_t = typename make_void<Ts...>::type;
// Since C++14
template<typename T>
using decay_t = typename std::decay<T>::type;
// Since C++14
template< bool B, typename T = void >
using enable_if_t = typename std::enable_if<B, T>::type;
// Since C++14
template<bool B, typename T, typename F>
using conditional_t = typename std::conditional<B, T, F>::type;
// Since C++14
template<typename T>
using remove_cv_t = typename std::remove_cv<T>::type;
// Since C++14
template<typename T>
using remove_reference_t = typename std::remove_reference<T>::type;
// Since C++20
template<typename T>
struct remove_cvref {
using type = remove_cv_t<remove_reference_t<T>>;
};
template<typename T>
using remove_cvref_t = typename remove_cvref<T>::type;
// Type used to indicate SFINAE success.
template<typename T>
struct success_type
{
using type = T;
};
// Type used to indicate SFINAE failure.
struct failure_type
{};
// Detect reference wrapper
// @see https://stackoverflow.com/questions/40430692/how-to-detect-stdreference-wrapper-in-c-at-compile-time
template <typename T>
struct is_reference_wrapper : std::false_type {};
template <typename U>
struct is_reference_wrapper<std::reference_wrapper<U>> : std::true_type {};
// Test for equality comparable
// @see C++ Templates: The Complete Guide (David Vandevoorde, et. al., 2018)
template<typename T>
struct is_equality_comparable
{
private:
// Test convertibility of == and !(==) to bool:
static void* conv(bool);
template<typename U>
static std::true_type test(
decltype(conv(std::declval<const U&>() == std::declval<const U&>())),
decltype(conv(!(std::declval<const U&>() == std::declval<const U&>())))
);
// Fallback
template<typename U>
static std::false_type test(...);
public:
static constexpr bool value = decltype(test<T>(nullptr, nullptr))::value;
};
// Detect if type supports weak_ptr semantics.
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
{};
// Test to see if a type can be converted to a weak_ptr type.
template <typename T, typename = void>
struct is_weak_ptr_convertable : std::false_type
{};
template <typename T>
struct is_weak_ptr_convertable<T, void_t<decltype(to_weak(std::declval<T>()))>>
: is_weak_ptr<decltype(to_weak(std::declval<T>()))>
{};
// Primary template for extracting function traits.
template<typename Func>
struct function_traits;
template<typename R, typename... Args>
struct function_traits<R(Args...)>
{
using result_type = R;
};
// Used by result_of, invoke etc. to unwrap a reference_wrapper.
template<typename T, typename U = remove_cvref_t<T>>
struct inv_unwrap
{
using type = T;
};
template<typename T, typename U>
struct inv_unwrap<T, std::reference_wrapper<U>>
{
using type = U&;
};
template<typename T>
using inv_unwrap_t = typename inv_unwrap<T>::type;
// Primary template for result_of.
//
template<typename Func>
struct result_of;
// Invoke tags.
struct invoke_func{};
struct invoke_memfun_ref{};
struct invoke_memfun_deref{};
struct invoke_memobj_ref{};
struct invoke_memobj_deref{};
// Associate a tag with a specialization of success_type
template<typename T, typename Tag>
struct result_of_success : success_type<T>
{
using invoke_type = Tag;
};
// Determine result of calling a function object.
struct result_of_func_impl
{
template<typename Func, typename... Args>
static result_of_success<decltype(std::declval<Func>()(std::declval<Args>()...)), invoke_func>
test(int);
// Fallback on failure.
template<typename...>
static failure_type test(...);
};
// Determine the result of calling a pointer to member function
// for reference types.
struct result_of_memfun_ref_impl
{
template<typename Func, typename T, typename... Args>
static result_of_success<decltype((std::declval<T>().*std::declval<Func>())(std::declval<Args>()...)), invoke_memfun_ref>
test(int);
// Fallback on failure.
template<typename...>
static failure_type test(...);
};
template<typename Func, typename Arg, typename... Args>
struct result_of_memfun_ref : private result_of_memfun_ref_impl
{
using type = decltype(test<Func, Arg, Args...>(0));
};
template<typename Func, typename Arg, typename... Args>
using result_of_memfun_ref_t = typename result_of_memfun_ref<Func, Arg, Args...>::type;
// Determine the result of calling a pointer to member function
// for pointer types.
struct result_of_memfun_deref_impl
{
template<typename Func, typename T, typename... Args>
static result_of_success<decltype(((*std::declval<T>()).*std::declval<Func>())(std::declval<Args>()...)), invoke_memfun_deref>
test(int);
// Fallback on failure.
template<typename...>
static failure_type test(...);
};
template<typename Func, typename Arg, typename... Args>
struct result_of_memfun_deref : private result_of_memfun_deref_impl
{
using type = decltype(test<Func, Arg, Args...>(0));
};
template<typename Func, typename Arg, typename... Args>
using result_of_memfun_deref_t = typename result_of_memfun_deref<Func, Arg, Args...>::type;
// Determine the result of a pointer to member data.
// for reference types.
struct result_of_memobj_ref_impl
{
template<typename F, typename T>
static result_of_success<decltype(std::declval<T>().*std::declval<F>()), invoke_memobj_ref>
test(int);
template<typename, typename>
static failure_type test(...);
};
template<typename MemPtr, typename Arg>
struct result_of_memobj_ref : private result_of_memobj_ref_impl
{
using type = decltype(test<MemPtr, Arg>(0));
};
template<typename MemPtr, typename Arg>
using result_of_memobj_ref_t = typename result_of_memobj_ref<MemPtr, Arg>::type;
// Determine the result of a pointer to member data for
// pointer types.
struct result_of_memobj_deref_impl
{
template<typename F, typename T>
static result_of_success<decltype((*std::declval<T>()).*std::declval<F>()), invoke_memobj_deref>
test(int);
template<typename, typename>
static failure_type test(...);
};
template<typename MemPtr, typename Arg>
struct result_of_memobj_deref : private result_of_memobj_deref_impl
{
using type = decltype(test<MemPtr, Arg>(0));
};
template<typename MemPtr, typename Arg>
using result_of_memobj_deref_t = typename result_of_memobj_deref<MemPtr, Arg>::type;
template<typename MemPtr, typename Arg>
struct result_of_memobj;
template<typename Type, typename Class, typename Arg>
struct result_of_memobj<Type Class::*, Arg>
{
using ArgVal = remove_cvref_t<Arg>;
using MemPtr = Type Class::*;
using type = conditional_t<std::is_same<ArgVal, Class>::value || std::is_base_of<Class, ArgVal>::value,
result_of_memobj_ref_t<MemPtr, Arg>,
result_of_memobj_deref_t<MemPtr, Arg>>;
};
template<typename MemPtr, typename Arg>
using result_of_memobj_t = typename result_of_memobj<MemPtr, Arg>::type;
template<typename MemPtr, typename Class, typename... Args>
struct result_of_memfun;
template<typename Func, typename Class, typename Arg, typename... Args>
struct result_of_memfun<Func Class::*, Arg, Args...>
{
using ArgVal = remove_reference_t<Arg>;
using MemPtr = Func Class::*;
using type = conditional_t<std::is_base_of<Class, ArgVal>::value,
result_of_memfun_ref_t<MemPtr, Arg, Args...>,
result_of_memfun_deref_t<MemPtr, Arg, Args...>>;
};
template<typename Func, typename Class, typename Arg, typename... Args>
using result_of_memfun_t = typename result_of_memfun<Func, Class, Arg, Args...>::type;
template<bool IsMemberObjectPointer, bool IsMemberFunctionPointer,
typename Func, typename... Args>
struct result_of_impl
{
using type = failure_type;
};
template<typename MemPtr, typename Arg>
struct result_of_impl<true, false, MemPtr, Arg> : result_of_memobj<decay_t<MemPtr>, inv_unwrap_t<Arg>>
{};
template<typename MemPtr, typename Arg, typename... Args>
struct result_of_impl<false, true, MemPtr, Arg, Args...> : result_of_memfun<decay_t<MemPtr>, inv_unwrap_t<Arg>, Args...>
{};
template<typename Func, typename... Args>
struct result_of_impl<false, false, Func, Args...> : private result_of_func_impl
{
using type = decltype(test<Func, Args...>(0));
};
template<typename Func, typename... Args>
struct invoke_result : public result_of_impl<
std::is_member_object_pointer<remove_reference_t<Func>>::value,
std::is_member_function_pointer<remove_reference_t<Func>>::value,
Func, Args...>::type
{};
template<typename Func, typename... Args>
using invoke_result_t = typename invoke_result<Func, Args...>::type;
// Detect if a function type is invocable given a set of arguments.
// Primary template for invalid INVOKE expressions.
template<typename Result, typename R, bool = std::is_void<R>::value, typename = void>
struct is_invocable_impl : std::false_type
{};
// Valid INVOKE and INVOKE<void> expressions
template<typename Result, typename R>
struct is_invocable_impl<Result, R, true, void_t<typename Result::type>> : std::true_type
{};
// Valid INVOKE<R> expressions.
template<typename Result, typename R>
struct is_invocable_impl<Result, R, false, void_t<typename Result::type>>
{
private:
// The type of the INVOKE expression.
static typename Result::type get();
template<typename T>
static void conv(T);
// This overload is viable if INVOKE(f, args...) can convert to T
template<typename T, typename = decltype(conv<T>(get()))>
static std::true_type test(int);
// Fallback if failure
template<typename T>
static std::false_type test(...);
public:
using type = decltype(test<R>(0));
};
template<typename Func, typename... Args>
struct is_invocable : is_invocable_impl<invoke_result<Func, Args...>, void>::type
{};
template<typename R, typename Func, typename... Args>
struct is_invocable_r : is_invocable_impl<invoke_result<Func, Args...>, R>::type
{};
} // namespace traits
// Use is_equality_compareable to try to perform the equality check
// (if it is valid for the given type).
template<typename T, bool = traits::is_equality_comparable<T>::value>
struct try_equals
{
static bool equals(const T& t1, const T& t2)
{
return t1 == t2;
}
};
// Partial specialization if type is not equality comparable.
// In this case, throw an exception to indicate that the type is not
// equality comparable.
template<typename T>
struct try_equals<T, false>
{
static bool equals(const T&, const T&)
{
throw sig::not_comparable_exception();
}
};
// Primary template
// Invokes a function object.
// @see https://en.cppreference.com/w/cpp/types/result_of
template<typename>
struct invoke_helper
{
// Call a function object.
template<typename Func, typename... Args>
static auto call(Func&& f, Args&&... args) -> decltype(std::forward<Func>(f)(std::forward<Args>(args)...))
{
return std::forward<Func>(f)(std::forward<Args>(args)...);
}
};
// Invoke a pointer to member function or pointer to member data.
// @see https://en.cppreference.com/w/cpp/types/result_of
template<typename Type, typename Base>
struct invoke_helper<Type Base::*>
{
// Get a reference type.
template<typename T, typename Td = traits::decay_t<T>,
typename = traits::enable_if_t<std::is_base_of<Base, Td>::value>>
static auto get(T&& t) -> T&&
{
return t;
}
// Get a std::reference_wrapper
template<typename T, typename Td = traits::decay_t<T>,
typename = traits::enable_if_t<traits::is_reference_wrapper<Td>::value>>
static auto get(T&& t) -> decltype(t.get())
{
return t.get();
}
// Get a pointer or pointer-like object (like smart_ptr, or unique_ptr)
template<typename T, typename Td = traits::decay_t<T>,
typename = traits::enable_if_t<!std::is_base_of<Base, Td>::value>,
typename = traits::enable_if_t<!traits::is_reference_wrapper<Td>::value>>
static auto get(T&& t) -> decltype(*std::forward<T>(t))
{
return *std::forward<T>(t);
}
// Call a pointer to a member function.
template<typename T, typename... Args, typename Type1,
typename = traits::enable_if_t<std::is_function<Type1>::value>>
static auto call(Type1 Base::* pmf, T&& t, Args&&... args)
-> decltype((get(std::forward<T>(t)).*pmf)(std::forward<Args>(args)...))
{
return (get(std::forward<T>(t)).*pmf)(std::forward<Args>(args)...);
}
// Call a pointer to member data.
template<typename T>
static auto call(Type Base::* pmd, T&& t)
-> decltype(get(std::forward<T>(t)).*pmd)
{
return get(std::forward<T>(t)).*pmd;
}
};
/**
* A copy-on-write template class to avoid unnecessary copies of
* data unless the data will be modified. This greatly improves
* the performance of the signal class in a multi-threaded environment
* since read-only copies only require a shared pointer copy.
* The copy-on-write pointer has similar semantics to shared pointers.
*
* @see API Design for C++, Martin Reddy (Elsevier, 2011).
*/
template<typename T>
class cow_ptr
{
public:
using value_type = T;
using pointer_type = std::shared_ptr<T>;
constexpr cow_ptr() noexcept = default;
~cow_ptr() = default;
constexpr explicit cow_ptr(T* other) noexcept
: m_Ptr(other)
{}
constexpr cow_ptr(const cow_ptr& other) noexcept
: m_Ptr(other.m_Ptr)
{}
constexpr cow_ptr(cow_ptr&& other) noexcept
: m_Ptr(std::move(other.m_Ptr))
{}
// Copy assignment operator.
cow_ptr& operator=(const cow_ptr& other) noexcept
{
if (this != &other)
{
*this = cow_ptr(other);
}
return *this;
}
// Move assignment operator.
cow_ptr& operator=(cow_ptr&& other) noexcept
{
m_Ptr = std::move(other.m_Ptr);
return *this;
}
// Assign from other value.
cow_ptr& operator=(T* other) noexcept
{
m_Ptr = pointer_type(other);
return *this;
}
// Non-const dereference operator.
// Will create a copy of the underlying object.
T& operator*()
{
detach();
return *m_Ptr;
}
// Const dereference operator.
// No copy is made of the underlying object
const T& operator*() const noexcept
{
return *m_Ptr;
}
// Non-const pointer dereference operator.
// Will create a copy of the underlying object.
T* operator->()
{
detach();
return m_Ptr.get();
}
// Const pointer dereference operator.
// No copy is made of the underlying object.
const T* operator->() const
{
return m_Ptr.get();
}
// Non-const implicit conversion of underlying type.
// A copy will be created of the underlying object.
operator T* ()
{
detach();
return m_Ptr.get();
}
// Const implicit conversion of underlying type.
// No copy is made of the underlying object.
operator const T* () const
{
return m_Ptr.get();
}
T* data()
{
detach();
return m_Ptr.get();
}
const T* data() const
{
return m_Ptr.get();
}
// Get read-only reference to internal value.
const T& read() const
{
return *m_Ptr;
}
// Get writable reference to internal value.
T& write()
{
detach();
return *m_Ptr;
}
template<typename U>
bool operator==(const cow_ptr<U>& rhs) const noexcept
{
return m_Ptr == rhs.m_Ptr;
}
template<typename U>
bool operator!=(const cow_ptr<U>& rhs) const noexcept
{
return m_Ptr != rhs.m_Ptr;
}
// Explicit bool conversion to check for a valid
// internal value.
explicit operator bool() const noexcept
{
return bool(m_Ptr);
}
private:
void detach()
{
if (m_Ptr && m_Ptr.use_count() > 1)
{
// Detach from the shared pointer
// creating a new instance of the stored object.
*this = cow_ptr(new T(*m_Ptr));
}
}
pointer_type m_Ptr;
};
// Utility function to create a cow_ptr.
template<typename T, typename... Args>
cow_ptr<T> make_cow(Args&&... args)
{
return cow_ptr<T>(new T(std::forward<Args>(args)...));
}
template<typename T, typename U, typename... Args>
cow_ptr<T> make_cow(std::initializer_list<U> il, Args&&... args)
{
return cow_ptr<T>(new T(il, std::forward<Args>(args)...));
}
/**
* Slot state is used as both a non-template base class for slot_impl
* as well as storing connection information about the slot.
*/
class slot_state
{
public:
constexpr slot_state() noexcept
: m_Index(0)
, m_Connected(true)
, m_Blocked(false)
{}
// Atomic variables are not CopyConstructible.
// @see https://en.cppreference.com/w/cpp/atomic/atomic/atomic
slot_state(const slot_state& s) noexcept
: m_Index(s.m_Index)
, m_Connected(s.m_Connected.load())
, m_Blocked(s.m_Blocked.load())
{}
slot_state(slot_state&& s) noexcept
: m_Index(s.m_Index)
, m_Connected(s.m_Connected.load())
, m_Blocked(s.m_Blocked.load())
{}
virtual ~slot_state() = default;
slot_state& operator=(const slot_state& s) noexcept
{
m_Index = s.m_Index;
m_Connected = s.m_Connected.load();
m_Blocked = s.m_Blocked.load();
return *this;
}
slot_state& operator=(slot_state&& s) noexcept
{
m_Index = s.m_Index;
m_Connected.store(s.m_Connected.load());
m_Blocked.store(s.m_Blocked.load());
return *this;
}
virtual bool connected() const noexcept
{
return m_Connected;
}
bool disconnect() noexcept
{
return m_Connected.exchange(false);
}
bool blocked() const noexcept
{
return m_Blocked;
}
void block() noexcept
{
m_Blocked = true;
}
void unblock() noexcept
{
m_Blocked = false;
}
std::size_t& index()
{
return m_Index;
}
private:
std::size_t m_Index;
std::atomic_bool m_Connected;
std::atomic_bool m_Blocked;
};
// Base class for slot implementations.
template<typename R, typename... Args>
class slot_impl : public slot_state
{
public:
virtual ~slot_impl() = default;
virtual slot_impl* clone() const = 0;
virtual bool equals(const slot_impl* s) const = 0;
virtual opt::optional<R> operator()(Args&&... args) = 0;
};
// Slot implementation for callable function objects (Functors)
template<typename R, typename Func, typename... Args>
class slot_func : public slot_impl<R, Args...>
{
public:
using fuction_type = traits::decay_t<Func>;
slot_func(const slot_func&) = default; // Copy constructor.
slot_func(Func&& func)
: m_Func{ std::forward<Func>(func) }
{}
virtual slot_impl<R, Args...>* clone() const override
{
return new slot_func(*this);
}
virtual bool equals(const slot_impl<R, Args...>* s) const override
{
if (auto sfunc = dynamic_cast<const slot_func*>(s))
{
return try_equals<fuction_type>::equals(m_Func, sfunc->m_Func);
}
return false;
}
virtual opt::optional<R> operator()(Args&&... args) override
{
return invoke_helper<fuction_type>::call(m_Func, std::forward<Args>(args)...);
}
private:
fuction_type m_Func;
};
// Specialization for void return types.
template<typename Func, typename... Args>
class slot_func<void, Func, Args...> : public slot_impl<void, Args...>
{
public:
using fuction_type = traits::decay_t<Func>;
slot_func(const slot_func&) = default; // Copy constructor.
slot_func(Func&& func)
: m_Func{ std::forward<Func>(func) }
{}
virtual slot_impl<void, Args...>* clone() const override
{
return new slot_func(*this);
}
virtual bool equals(const slot_impl<void, Args...>* s) const override
{
if (auto sfunc = dynamic_cast<const slot_func*>(s))
{
return try_equals<fuction_type>::equals(m_Func, sfunc->m_Func);
}
return false;
}
virtual opt::optional<void> operator()(Args&&... args) override
{
return (invoke_helper<fuction_type>::call(m_Func, std::forward<Args>(args)...), opt::nullopt);
}
private:
fuction_type m_Func;
};
// Slot implementation for pointer to member function and
// pointer to member data.
template<typename R, typename Func, typename Ptr, typename... Args>
class slot_pmf : public slot_impl<R, Args...>
{
public:
using function_type = traits::decay_t<Func>;
using pointer_type = traits::decay_t<Ptr>;
slot_pmf(const slot_pmf&) = default;
slot_pmf(Func&& func, Ptr&& ptr)
: m_Ptr{ std::forward<Ptr>(ptr) }
, m_Func{ std::forward<Func>(func) }
{}
virtual slot_impl<R, Args...>* clone() const override
{
return new slot_pmf(*this);
}
virtual bool equals(const slot_impl<R, Args...>* s) const override
{
if (auto spmf = dynamic_cast<const slot_pmf*>(s))
{
return try_equals<pointer_type>::equals(m_Ptr, spmf->m_Ptr) &&
try_equals<function_type>::equals(m_Func, spmf->m_Func);
}
return false;
}
virtual opt::optional<R> operator()(Args&&... args) override
{
return invoke_helper<function_type>::call(m_Func, m_Ptr, std::forward<Args>(args)...);
}
private:
pointer_type m_Ptr;
function_type m_Func;
};
// Slot implementation for pointer to member function and
// pointer to member data.
// Specialized on void return value.
template<typename Func, typename Ptr, typename... Args>
class slot_pmf<void, Func, Ptr, Args...> : public slot_impl<void, Args...>
{
public:
using function_type = traits::decay_t<Func>;
using pointer_type = traits::decay_t<Ptr>;
slot_pmf(const slot_pmf&) = default;
slot_pmf(Func&& func, Ptr&& ptr)
: m_Ptr{ std::forward<Ptr>(ptr) }
, m_Func{ std::forward<Func>(func) }
{}
virtual slot_impl<void, Args...>* clone() const override
{
return new slot_pmf(*this);
}
virtual bool equals(const slot_impl<void, Args...>* s) const override
{
if (auto spmf = dynamic_cast<const slot_pmf*>(s))
{
return try_equals<pointer_type>::equals(m_Ptr, spmf->m_Ptr) &&
try_equals<function_type>::equals(m_Func, spmf->m_Func);
}
return false;
}
virtual opt::optional<void> operator()(Args&&... args) override
{
invoke_helper<function_type>::call(m_Func, m_Ptr, std::forward<Args>(args)...);
return {};
}
private:
pointer_type m_Ptr;
function_type m_Func;
};
// Slot implementation for pointer to member function that automatically
// tracks the lifetime of a supplied object through a weak pointer in
// order to disconnect the slot on object destruction.
template<typename R, typename Func, typename WeakPtr, typename... Args>
class slot_pmf_tracked : public slot_impl<R, Args...>
{
public:
using function_type = traits::decay_t<Func>;
using pointer_type = traits::decay_t<WeakPtr>;
slot_pmf_tracked(const slot_pmf_tracked&) = default;
slot_pmf_tracked(Func&& func, WeakPtr&& ptr)
: m_Ptr{ std::forward<WeakPtr>(ptr) }
, m_Func{ std::forward<Func>(func) }
{}
virtual bool connected() const noexcept override
{
return !m_Ptr.expired() && slot_state::connected();
}
virtual slot_impl<R, Args...>* clone() const override
{
return new slot_pmf_tracked(*this);
}
virtual bool equals(const slot_impl<R, Args...>* s) const override
{
if (auto spmft = dynamic_cast<const slot_pmf_tracked*>(s))
{
return try_equals<pointer_type>::equals(m_Ptr, spmft->m_Ptr) &&
try_equals<function_type>::equals(m_Func, spmft->m_Func);
}
return false;
}
virtual opt::optional<R> operator()(Args&&... args) override
{
auto sp = m_Ptr.lock();
if (!sp)
{
this->disconnect();
return {};
}
if (this->connected())
{
return invoke_helper<function_type>::call(m_Func, sp, std::forward<Args>(args)...);
}
return {};
}
private:
pointer_type m_Ptr;
function_type m_Func;
};
// Slot implementation for pointer to member function that automatically
// tracks the lifetime of a supplied object through a weak pointer in
// order to disconnect the slot on object destruction.
// Partial specialization for void return types.
template<typename Func, typename WeakPtr, typename... Args>
class slot_pmf_tracked<void, Func, WeakPtr, Args...> : public slot_impl<void, Args...>
{
public:
using function_type = traits::decay_t<Func>;
using pointer_type = traits::decay_t<WeakPtr>;
slot_pmf_tracked(const slot_pmf_tracked&) = default;
slot_pmf_tracked(Func&& func, WeakPtr&& ptr)