Fcitx
configuration.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_CONFIGURATION_H_
8 #define _FCITX_CONFIG_CONFIGURATION_H_
9 
10 #include <memory>
11 #include <string>
12 #include <vector>
13 #include <fcitx-config/fcitxconfig_export.h>
14 #include <fcitx-config/option.h>
15 #include <fcitx-config/optiontypename.h>
16 #include <fcitx-config/rawconfig.h>
17 #include <fcitx-utils/macros.h>
18 
19 #define FCITX_CONFIGURATION_EXTEND(NAME, SUBCLASS, ...) \
20  class NAME; \
21  FCITX_SPECIALIZE_TYPENAME(NAME, #NAME) \
22  FCITX_CONFIGURATION_CLASS_EXTEND(NAME, SUBCLASS, __VA_ARGS__)
23 
24 #define FCITX_CONFIGURATION(NAME, ...) \
25  FCITX_CONFIGURATION_EXTEND(NAME, ::fcitx::Configuration, __VA_ARGS__)
26 
27 #define FCITX_CONFIGURATION_CLASS_EXTEND(NAME, SUBCLASS, ...) \
28  class NAME : public SUBCLASS { \
29  public: \
30  NAME() {} \
31  NAME(const NAME &other) : NAME() { copyHelper(other); } \
32  NAME &operator=(const NAME &other) { \
33  copyHelper(other); \
34  return *this; \
35  } \
36  bool operator==(const NAME &other) const { \
37  return compareHelper(other); \
38  } \
39  const char *typeName() const override { return #NAME; } \
40  \
41  public: \
42  __VA_ARGS__ \
43  };
44 
45 #define FCITX_CONFIGURATION_CLASS(NAME, ...) \
46  FCITX_CONFIGURATION_CLASS_EXTEND(NAME, ::fcitx::Configuration, __VA_ARGS__)
47 
48 namespace fcitx {
49 
50 class ConfigurationPrivate;
51 
52 class FCITXCONFIG_EXPORT Configuration {
53  friend class OptionBase;
54 
55 public:
56  Configuration();
57  virtual ~Configuration();
58 
59  /// Load configuration from RawConfig. If partial is true, non-exist option
60  /// will be reset to default value, otherwise it will be untouched.
61  void load(const RawConfig &config, bool partial = false);
62  void save(RawConfig &config) const;
63  void dumpDescription(RawConfig &config) const;
64  FCITX_NODISCARD virtual const char *typeName() const = 0;
65 
66  /**
67  * Set default value to current value.
68  *
69  * Sometimes, we need to customize the default value for the same type. This
70  * function will set the default value to current value.
71  */
72  void syncDefaultValueToCurrent();
73 
74 protected:
75  bool compareHelper(const Configuration &other) const;
76  void copyHelper(const Configuration &other);
77 
78 private:
79  void dumpDescriptionImpl(RawConfig &config,
80  const std::vector<std::string> &parentPaths) const;
81  void addOption(fcitx::OptionBase *option);
82 
83  FCITX_DECLARE_PRIVATE(Configuration);
84  std::unique_ptr<ConfigurationPrivate> d_ptr;
85 };
86 } // namespace fcitx
87 
88 #endif // _FCITX_CONFIG_CONFIGURATION_H_
Definition: action.cpp:17