Fcitx
signals_details.h
1 /*
2  * SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 #ifndef _FCITX_UTILS_SIGNAL_DETAILS_H_
8 #define _FCITX_UTILS_SIGNAL_DETAILS_H_
9 
10 #include <cstddef>
11 #include <functional>
12 #include <iterator>
13 #include <memory>
14 #include <tuple>
15 #include <fcitx-utils/handlertable.h>
16 #include <fcitx-utils/intrusivelist.h>
18 #include <fcitx-utils/tuplehelpers.h>
19 
20 namespace fcitx {
21 
22 template <typename Ret, typename... Args>
23 class Invoker {
24 public:
25  Invoker(Args &...args) : args_(args...) {}
26 
27  template <typename Func>
28  Ret operator()(const Func &func) {
29  return callWithTuple(func, args_);
30  }
31 
32 private:
33  std::tuple<Args &...> args_;
34 };
35 
36 template <typename T>
38 
39 template <typename Ret, typename... Args>
40 class SlotInvokeIterator<std::function<Ret(Args...)>> {
41 public:
42  using iterator_category = std::input_iterator_tag;
43  using function_type = std::function<Ret(Args...)>;
44  using value_type = typename function_type::result_type;
45  using difference_type = std::ptrdiff_t;
46  using reference = value_type;
47  using super_iterator = typename HandlerTableView<function_type>::iterator;
49  using invoker_type = Invoker<Ret, Args...>;
50 
51  SlotInvokeIterator(invoker_type &invoker, super_iterator iter)
52  : parentIter_(iter), invoker_(invoker) {}
53 
54  SlotInvokeIterator(const iterator &other) = default;
55 
56  iterator &operator=(const iterator &other) = default;
57 
58  bool operator==(const iterator &other) const noexcept {
59  return parentIter_ == other.parentIter_;
60  }
61  bool operator!=(const iterator &other) const noexcept {
62  return !operator==(other);
63  }
64 
65  iterator &operator++() {
66  parentIter_++;
67  return *this;
68  }
69 
70  iterator operator++(int) {
71  auto old = parentIter_;
72  ++(*this);
73  return {invoker_, old};
74  }
75 
76  reference operator*() { return invoker_(*parentIter_); }
77 
78 private:
79  super_iterator parentIter_;
80  invoker_type &invoker_;
81 };
82 
83 template <typename Invoker, typename Iter>
85 MakeSlotInvokeIterator(Invoker &invoker, Iter iter) {
86  return {invoker, iter};
87 }
88 
89 class ConnectionBody : public TrackableObject<ConnectionBody>,
90  public IntrusiveListNode {
91 public:
92  template <typename T>
93  ConnectionBody(std::unique_ptr<HandlerTableEntry<T>> entry)
94  : entry_(std::move(entry)) {}
95 
96  ~ConnectionBody() override { remove(); }
97 
98 private:
99  std::unique_ptr<HandlerTableEntryBase> entry_;
100 };
101 } // namespace fcitx
102 
103 #endif // _FCITX_UTILS_SIGNAL_DETAILS_H_
Utitliy classes for statically tracking the life of a object.
Definition: action.cpp:17
Definition: matchrule.h:78
Helper class to be used with TrackableObjectReference.