7 #ifndef _FCITX_UTILS_SIGNALS_H_ 8 #define _FCITX_UTILS_SIGNALS_H_ 18 #include <fcitx-utils/handlertable.h> 19 #include <fcitx-utils/intrusivelist.h> 20 #include <fcitx-utils/macros.h> 21 #include <fcitx-utils/signals_details.h> 23 #include <fcitx-utils/tuplehelpers.h> 31 LastValue(T defaultValue = T()) : initial_(defaultValue) {}
33 template <
typename InputIterator>
34 T operator()(InputIterator begin, InputIterator end) {
36 for (; begin != end; begin++) {
50 template <
typename InputIterator>
51 void operator()(InputIterator begin, InputIterator end) {
52 for (; begin != end; begin++) {
64 : body_(std::move(body)) {}
66 FCITX_INLINE_DEFINE_DEFAULT_DTOR_COPY_AND_MOVE(
Connection)
69 bool connected() {
return body_.isValid(); }
70 bool connected()
const {
return body_.isValid(); }
73 auto *body = body_.
get();
77 bool operator==(
const Connection &other)
const {
78 return body_.get() == other.body_.get();
80 bool operator!=(
const Connection &other)
const {
return !(*
this == other); }
100 if (&other ==
this) {
104 Connection::operator=(std::move(other));
124 template <
typename T,
128 template <
typename Ret,
typename Combiner,
typename... Args>
131 SignalData(Combiner combiner) : combiner_(std::move(combiner)) {}
139 using return_type = Ret;
140 using function_type = Ret(Args...);
141 Signal(Combiner combiner = Combiner())
142 : d_ptr(std::make_unique<SignalData>(std::move(combiner))) {}
148 Signal(
Signal &&other) noexcept { operator=(std::move(other)); }
151 swap(d_ptr, other.d_ptr);
155 Ret operator()(Args... args) {
156 auto view = d_ptr->table_.view();
157 Invoker<Ret, Args...> invoker(args...);
158 auto iter = MakeSlotInvokeIterator(invoker, view.begin());
159 auto end = MakeSlotInvokeIterator(invoker, view.end());
160 return d_ptr->combiner_(iter, end);
163 template <
typename Func>
167 d_ptr->connections_.push_back(*body);
171 void disconnectAll() {
172 while (!d_ptr->connections_.empty()) {
173 delete &d_ptr->connections_.front();
179 std::unique_ptr<SignalData> d_ptr;
183 #endif // _FCITX_UTILS_SIGNALS_H_ Utility class provides a weak reference to the object.
Utitliy classes for statically tracking the life of a object.
Connection that will disconnection when it goes out of scope.
Class to represent a signal. May be used like a functor.
Combiner that return the last value.
T * get() const
Get the referenced object. Return nullptr if it is not available.