Fcitx
connectableobject.cpp
1 /*
2  * SPDX-FileCopyrightText: 2016-2016 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 
8 #include "connectableobject.h"
9 #include <memory>
10 #include <string>
11 #include <unordered_map>
12 #include <utility>
13 #include "macros.h"
14 #include "signals.h"
15 
16 namespace fcitx {
17 
19 public:
20  ConnectableObjectPrivate() = default;
21  std::unordered_map<std::string, std::unique_ptr<fcitx::SignalBase>>
22  signals_;
23  bool destroyed_ = false;
24  std::unique_ptr<SignalAdaptor<ConnectableObject::Destroyed>> adaptor_;
25 };
26 
27 ConnectableObject::ConnectableObject()
28  : d_ptr(std::make_unique<ConnectableObjectPrivate>()) {
29  FCITX_D();
30  d->adaptor_ = std::make_unique<decltype(d->adaptor_)::element_type>(this);
31 }
32 
33 ConnectableObject::~ConnectableObject() { destroy(); }
34 
35 void ConnectableObject::_registerSignal(
36  std::string name, std::unique_ptr<fcitx::SignalBase> signal) {
37  FCITX_D();
38  d->signals_.emplace(std::move(name), std::move(signal));
39 }
40 void ConnectableObject::_unregisterSignal(const std::string &name) {
41  FCITX_D();
42  d->signals_.erase(name);
43 }
44 
45 SignalBase *ConnectableObject::findSignal(const std::string &name) {
46  return std::as_const(*this).findSignal(name);
47 }
48 
49 SignalBase *ConnectableObject::findSignal(const std::string &name) const {
50  FCITX_D();
51  auto iter = d->signals_.find(name);
52  if (iter != d->signals_.end()) {
53  return iter->second.get();
54  }
55  return nullptr;
56 }
57 
59  FCITX_D();
60  if (!d->destroyed_) {
61  emit<ConnectableObject::Destroyed>(this);
62  disconnectAll<ConnectableObject::Destroyed>();
63  d->adaptor_.reset();
64  d->destroyed_ = true;
65  }
66 }
67 } // namespace fcitx
void destroy()
Allow user to notify the destroy event earlier.
Definition: action.cpp:17
Utilities to enable use object with signal.
A signal-slot implemention.