19
19
#ifndef _FCITX_UTILS_SIGNALS_H_
20
20
#define _FCITX_UTILS_SIGNALS_H_
21
21
22
+ // / \addtogroup FcitxUtils
23
+ // / \{
24
+ // / \file
25
+ // / \brief A signal-slot implemention.
26
+
22
27
#include < fcitx-utils/handlertable.h>
23
28
#include < fcitx-utils/intrusivelist.h>
24
29
#include < fcitx-utils/macros.h>
25
30
#include < fcitx-utils/trackableobject.h>
26
31
#include < fcitx-utils/tuplehelpers.h>
32
+ #include < fcitx-utils/signals_details.h>
27
33
#include < tuple>
28
34
29
35
namespace fcitx {
30
36
37
+ // / \brief Combiner that return the last value.
31
38
template <typename T>
32
39
class LastValue {
33
40
public:
@@ -45,6 +52,7 @@ class LastValue {
45
52
private:
46
53
T initial_;
47
54
};
55
+
48
56
template <>
49
57
class LastValue <void > {
50
58
public:
@@ -57,90 +65,8 @@ class LastValue<void> {
57
65
}
58
66
};
59
67
60
- template <typename Ret, typename ... Args>
61
- class Invoker {
62
- public:
63
- Invoker (Args &... args) : args_(args...) {}
64
-
65
- template <typename Func>
66
- Ret operator ()(Func &func) {
67
- return callWithTuple (func, args_);
68
- }
69
-
70
- private:
71
- std::tuple<Args &...> args_;
72
- };
73
-
74
- template <typename T>
75
- class SlotInvokeIterator ;
76
-
77
- template <typename Ret, typename ... Args>
78
- class SlotInvokeIterator <std::function<Ret(Args...)>> {
79
- public:
80
- typedef std::input_iterator_tag iterator_category;
81
- typedef std::function<Ret(Args...)> function_type;
82
- typedef typename function_type::result_type value_type;
83
- typedef std::ptrdiff_t difference_type;
84
- typedef value_type reference;
85
- typedef typename HandlerTableView<function_type>::iterator super_iterator;
86
- typedef SlotInvokeIterator iterator;
87
- typedef Invoker<Ret, Args...> invoker_type;
88
-
89
- SlotInvokeIterator (invoker_type &invoker, super_iterator iter)
90
- : parentIter_(iter), invoker_(invoker) {}
91
-
92
- SlotInvokeIterator (const iterator &other) = default ;
93
-
94
- iterator &operator =(const iterator &other) = default ;
95
-
96
- bool operator ==(const iterator &other) const noexcept {
97
- return parentIter_ == other.parentIter_ ;
98
- }
99
- bool operator !=(const iterator &other) const noexcept {
100
- return !operator ==(other);
101
- }
102
-
103
- iterator &operator ++() {
104
- parentIter_++;
105
- return *this ;
106
- }
107
-
108
- iterator operator ++(int ) {
109
- auto old = parentIter_;
110
- ++(*this );
111
- return {invoker_, old};
112
- }
113
-
114
- reference operator *() { return invoker_ (*parentIter_); }
115
-
116
- private:
117
- super_iterator parentIter_;
118
- invoker_type &invoker_;
119
- };
120
-
121
- template <typename Invoker, typename Iter>
122
- SlotInvokeIterator<typename Iter::value_type>
123
- MakeSlotInvokeIterator (Invoker invoker, Iter iter) {
124
- return {invoker, iter};
125
- }
126
-
127
- template <typename T,
128
- typename Combiner = LastValue<typename std::function<T>::result_type>>
129
- class Signal ;
130
-
131
- class ConnectionBody : public TrackableObject <ConnectionBody>,
132
- public IntrusiveListNode {
133
- public:
134
- template <typename T>
135
- ConnectionBody (HandlerTableEntry<T> *entry) : entry_(entry) {}
136
-
137
- virtual ~ConnectionBody () { remove (); }
138
-
139
- private:
140
- // Need type erasure here
141
- std::shared_ptr<void > entry_;
142
- };
143
-
68
+ // / \brief A connection instance. Can be used to query the existence of
69
+ // / connection.
144
70
class Connection {
145
71
public:
146
72
Connection () {}
@@ -165,6 +91,8 @@ class Connection {
165
91
TrackableObjectReference<ConnectionBody> body_;
166
92
};
167
93
94
+ // / \brief Connection that will disconnection when it goes out of scope.
95
+ // / \see Connection
168
96
class ScopedConnection : public Connection {
169
97
public:
170
98
// You must create two Connection if you really want two ScopedConnection
@@ -194,9 +122,14 @@ class ScopedConnection : public Connection {
194
122
195
123
class SignalBase {
196
124
public:
197
- virtual ~SignalBase () {}
125
+ virtual ~SignalBase () = default ;
198
126
};
199
127
128
+ // / \brief Class to represent a signal. May be used like a functor.
129
+ template <typename T,
130
+ typename Combiner = LastValue<typename std::function<T>::result_type>>
131
+ class Signal ;
132
+
200
133
template <typename Ret, typename Combiner, typename ... Args>
201
134
class Signal <Ret(Args...), Combiner> : public SignalBase {
202
135
struct SignalData {
@@ -217,7 +150,7 @@ class Signal<Ret(Args...), Combiner> : public SignalBase {
217
150
disconnectAll ();
218
151
}
219
152
}
220
- Signal (Signal &&other) noexcept { operator =(std::forward <Signal>(other)); }
153
+ Signal (Signal &&other) noexcept { operator =(std::move <Signal>(other)); }
221
154
Signal &operator =(Signal &&other) noexcept {
222
155
using std::swap;
223
156
swap (d_ptr, other.d_ptr );
0 commit comments