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