Fcitx
element.cpp
1 /*
2  * SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 
8 #include "element.h"
9 #include <list>
10 #include <memory>
11 #include "macros.h"
12 #include "misc_p.h"
13 
14 namespace fcitx {
15 
17  using ElementList = std::list<Element *>;
18 
19 public:
20  OrderedSet<Element *> parents_, childs_;
21 };
22 
23 Element::Element() : d_ptr(std::make_unique<ElementPrivate>()) {}
24 
25 Element::~Element() {
26  removeAllParent();
27  removeAllChild();
28 }
29 
30 const std::list<Element *> &Element::childs() const {
31  FCITX_D();
32  return d->childs_.order();
33 }
34 
35 void Element::addChild(Element *child) {
36  addEdge(this, child, nullptr, nullptr);
37 }
38 void Element::addParent(Element *parent) {
39  addEdge(parent, this, nullptr, nullptr);
40 }
41 
42 bool Element::isChild(const Element *element) const {
43  FCITX_D();
44  return d->childs_.contains(const_cast<Element *>(element));
45 }
46 bool Element::isParent(const Element *element) const {
47  FCITX_D();
48  return d->parents_.contains(const_cast<Element *>(element));
49 }
50 
51 const std::list<Element *> &Element::parents() const {
52  FCITX_D();
53  return d->parents_.order();
54 }
55 
56 void Element::removeChild(Element *child) { removeEdge(this, child); }
57 
58 void Element::removeParent(Element *parent) { removeEdge(parent, this); }
59 
60 void Element::insertChild(Element *before, Element *child) {
61  addEdge(this, child, before, nullptr);
62 }
63 
64 void Element::insertParent(Element *before, Element *parent) {
65  addEdge(parent, this, nullptr, before);
66 }
67 
68 void Element::addEdge(Element *parent, Element *child, Element *beforeChild,
69  Element *beforeParent) {
70  // Try not to invalidate the list iterator of elements.
71  if (parent->d_func()->childs_.contains(child)) {
72  return;
73  }
74  removeEdge(parent, child);
75  parent->d_func()->childs_.insert(beforeChild, child);
76  child->d_func()->parents_.insert(beforeParent, parent);
77 }
78 
79 void Element::removeEdge(Element *parent, Element *child) {
80  parent->d_func()->childs_.remove(child);
81  child->d_func()->parents_.remove(parent);
82 }
83 
84 void Element::removeAllParent() {
85  while (!parents().empty()) {
86  removeParent(parents().front());
87  }
88 }
89 
90 void Element::removeAllChild() {
91  while (!childs().empty()) {
92  childs().front()->removeParent(this);
93  }
94 }
95 } // namespace fcitx
Utility class that provides a hierarchy between multiple objects.
Definition: action.cpp:17
const std::list< Element * > & parents() const
List all parents.
Definition: element.cpp:51
bool isParent(const Element *element) const
Enable query between different elements.
Definition: element.cpp:46
const std::list< Element * > & childs() const
List all childs.
Definition: element.cpp:30
bool isChild(const Element *element) const
Enable query between different elements.
Definition: element.cpp:42
Base class that can be used for UI composition or graph.
Definition: element.h:26