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_INTOPTION_H_
8 #define _FCITX_CONFIG_INTOPTION_H_
9 
10 #include <type_traits>
11 #include <vector>
12 #include <fcitx-utils/color.h>
13 #include <fcitx-utils/i18nstring.h>
14 #include <fcitx-utils/key.h>
15 #include <fcitx-utils/semver.h>
16 #include "rawconfig.h"
17 
18 namespace fcitx {
19 
20 class Configuration;
21 
22 FCITXCONFIG_EXPORT void marshallOption(RawConfig &config, bool value);
23 FCITXCONFIG_EXPORT bool unmarshallOption(bool &value, const RawConfig &config,
24                                          bool partial);
25 
26 FCITXCONFIG_EXPORT void marshallOption(RawConfig &config, int value);
27 FCITXCONFIG_EXPORT bool unmarshallOption(int &value, const RawConfig &config,
28                                          bool partial);
29 
30 FCITXCONFIG_EXPORT void marshallOption(RawConfig &config,
31                                        const std::string &value);
32 FCITXCONFIG_EXPORT bool unmarshallOption(std::string &value,
33                                          const RawConfig &config, bool partial);
34 
35 FCITXCONFIG_EXPORT void marshallOption(RawConfig &config,
36                                        const SemanticVersion &value);
37 FCITXCONFIG_EXPORT bool unmarshallOption(SemanticVersion &value,
38                                          const RawConfig &config, bool partial);
39 
40 FCITXCONFIG_EXPORT void marshallOption(RawConfig &config, const Key &value);
41 FCITXCONFIG_EXPORT bool unmarshallOption(Key &value, const RawConfig &config,
42                                          bool partial);
43 
44 FCITXCONFIG_EXPORT void marshallOption(RawConfig &config, const Color &value);
45 FCITXCONFIG_EXPORT bool unmarshallOption(Color &value, const RawConfig &config,
46                                          bool partial);
47 
48 FCITXCONFIG_EXPORT void marshallOption(RawConfig &config,
49                                        const I18NString &value);
50 FCITXCONFIG_EXPORT bool unmarshallOption(I18NString &value,
51                                          const RawConfig &config, bool partial);
52 
53 FCITXCONFIG_EXPORT void marshallOption(RawConfig &config,
54                                        const Configuration &value);
55 FCITXCONFIG_EXPORT bool unmarshallOption(Configuration &value,
56                                          const RawConfig &config, bool partial);
57 
58 template <typename T>
marshallOption(RawConfig & config,const std::vector<T> & value)59 void marshallOption(RawConfig &config, const std::vector<T> &value) {
60     config.removeAll();
61     for (size_t i = 0; i < value.size(); i++) {
62         marshallOption(config[std::to_string(i)], value[i]);
63     }
64 }
65 
66 template <typename T>
unmarshallOption(std::vector<T> & value,const RawConfig & config,bool partial)67 bool unmarshallOption(std::vector<T> &value, const RawConfig &config,
68                       bool partial) {
69     value.clear();
70     int i = 0;
71     while (true) {
72         auto subConfigPtr = config.get(std::to_string(i));
73         if (!subConfigPtr) {
74             break;
75         }
76 
77         value.emplace_back();
78 
79         if (!unmarshallOption(value[i], *subConfigPtr, partial)) {
80             return false;
81         }
82         i++;
83     }
84     return true;
85 }
86 } // namespace fcitx
87 
88 #endif // _FCITX_CONFIG_INTOPTION_H_
89