Fcitx
rawconfig.h
1 /*
2  * SPDX-FileCopyrightText: 2015-2015 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 #ifndef _FCITX_CONFIG_RAWCONFIG_H_
8 #define _FCITX_CONFIG_RAWCONFIG_H_
9 
10 #include <cstddef>
11 #include <functional>
12 #include <memory>
13 #include <string>
14 #include <utility>
15 #include <vector>
16 #include <fcitx-config/fcitxconfig_export.h>
17 #include <fcitx-utils/log.h>
18 #include <fcitx-utils/macros.h>
19 
20 namespace fcitx {
21 
22 class RawConfig;
23 
24 using RawConfigPtr = std::shared_ptr<RawConfig>;
25 
26 class RawConfigPrivate;
27 class FCITXCONFIG_EXPORT RawConfig {
28 public:
29  RawConfig();
30  FCITX_DECLARE_VIRTUAL_DTOR_COPY(RawConfig)
31 
32  std::shared_ptr<RawConfig> get(const std::string &path,
33  bool create = false);
34  std::shared_ptr<const RawConfig> get(const std::string &path) const;
35  bool remove(const std::string &path);
36  void removeAll();
37  void setValue(std::string value);
38  void setComment(std::string comment);
39  void setLineNumber(unsigned int lineNumber);
40  const std::string &name() const;
41  const std::string &comment() const;
42  const std::string &value() const;
43  unsigned int lineNumber() const;
44  bool hasSubItems() const;
45  size_t subItemsSize() const;
46  std::vector<std::string> subItems() const;
47  void setValueByPath(const std::string &path, std::string value) {
48  (*this)[path] = std::move(value);
49  }
50 
51  const std::string *valueByPath(const std::string &path) const {
52  auto config = get(path);
53  return config ? &config->value() : nullptr;
54  }
55 
56  RawConfig &operator[](const std::string &path) { return *get(path, true); }
57  RawConfig &operator=(std::string value) {
58  setValue(std::move(value));
59  return *this;
60  }
61 
62  bool operator==(const RawConfig &other) const {
63  if (this == &other) {
64  return true;
65  }
66  if (value() != other.value()) {
67  return false;
68  }
69  if (subItemsSize() != other.subItemsSize()) {
70  return false;
71  }
72  return visitSubItems(
73  [&other](const RawConfig &subConfig, const std::string &path) {
74  auto otherSubConfig = other.get(path);
75  return (otherSubConfig && *otherSubConfig == subConfig);
76  });
77  }
78 
79  bool operator!=(const RawConfig &config) const {
80  return !(*this == config);
81  }
82 
83  RawConfig *parent() const;
84  std::shared_ptr<RawConfig> detach();
85 
86  bool visitSubItems(
87  std::function<bool(RawConfig &, const std::string &path)> callback,
88  const std::string &path = "", bool recursive = false,
89  const std::string &pathPrefix = "");
90  bool visitSubItems(
91  std::function<bool(const RawConfig &, const std::string &path)>
92  callback,
93  const std::string &path = "", bool recursive = false,
94  const std::string &pathPrefix = "") const;
95  void visitItemsOnPath(
96  std::function<void(RawConfig &, const std::string &path)> callback,
97  const std::string &path);
98  void visitItemsOnPath(
99  std::function<void(const RawConfig &, const std::string &path)>
100  callback,
101  const std::string &path) const;
102 
103 private:
104  friend class RawConfigPrivate;
105  RawConfig(std::string name);
106  std::shared_ptr<RawConfig> createSub(std::string name);
107  FCITX_DECLARE_PRIVATE(RawConfig);
108  std::unique_ptr<RawConfigPrivate> d_ptr;
109 };
110 
111 FCITXCONFIG_EXPORT LogMessageBuilder &operator<<(LogMessageBuilder &log,
112  const RawConfig &config);
113 } // namespace fcitx
114 
115 #endif // _FCITX_CONFIG_RAWCONFIG_H_
Definition: action.cpp:17
Log utilities.