• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_ROSEN_WINDOW_MANAGER_CONFIG_H
17 #define OHOS_ROSEN_WINDOW_MANAGER_CONFIG_H
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 
23 #include <refbase.h>
24 #include "libxml/parser.h"
25 #include "libxml/tree.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 class WindowManagerConfig : public RefBase {
30 public:
31     enum class ValueType {
32         UNDIFINED,
33         MAP,
34         BOOL,
35         STRING,
36         INTS,
37         FLOATS,
38         POSITIVE_FLOATS,
39     };
40     struct ConfigItem {
41         std::map<std::string, ConfigItem>* property_ = nullptr;
42         ValueType type_ = ValueType::UNDIFINED;
43         union {
44             std::map<std::string, ConfigItem>* mapValue_ = nullptr;
45             bool boolValue_;
46             std::string stringValue_;
47             std::vector<int>* intsValue_;
48             std::vector<float>* floatsValue_;
49         };
ConfigItemConfigItem50         ConfigItem() {}
~ConfigItemConfigItem51         ~ConfigItem()
52         {
53             Destroy();
54         };
DestroyConfigItem55         void Destroy()
56         {
57             ClearValue();
58             if (property_) {
59                 delete property_;
60                 property_ = nullptr;
61             }
62         }
ClearValueConfigItem63         void ClearValue()
64         {
65             switch (type_) {
66                 case ValueType::MAP:
67                     delete mapValue_;
68                     mapValue_ = nullptr;
69                     break;
70                 case ValueType::STRING:
71                     stringValue_.~basic_string();
72                     break;
73                 case ValueType::INTS:
74                     delete intsValue_;
75                     intsValue_ = nullptr;
76                     break;
77                 case ValueType::FLOATS:
78                     delete floatsValue_;
79                     floatsValue_ = nullptr;
80                     break;
81                 default:
82                     break;
83             }
84         }
ConfigItemConfigItem85         ConfigItem(const ConfigItem& value)
86         {
87             *this = value;
88         }
89         ConfigItem& operator=(const ConfigItem& value)
90         {
91             Destroy();
92             switch (value.type_) {
93                 case ValueType::MAP:
94                     mapValue_ = new std::map<std::string, ConfigItem>(*value.mapValue_);
95                     break;
96                 case ValueType::BOOL:
97                     boolValue_ = value.boolValue_;
98                     break;
99                 case ValueType::STRING:
100                     new(&stringValue_)std::string(value.stringValue_);
101                     break;
102                 case ValueType::INTS:
103                     intsValue_ = new std::vector<int>(*value.intsValue_);
104                     break;
105                 case ValueType::FLOATS:
106                     floatsValue_ = new std::vector<float>(*value.floatsValue_);
107                     break;
108                 default:
109                     break;
110             }
111             type_ = value.type_;
112             if (value.property_) {
113                 property_ = new std::map<std::string, ConfigItem>(*value.property_);
114             }
115             return *this;
116         }
ConfigItemConfigItem117         ConfigItem(ConfigItem&& value) noexcept
118         {
119             *this = std::move(value);
120         }
121         ConfigItem& operator=(ConfigItem&& value) noexcept
122         {
123             Destroy();
124             switch (value.type_) {
125                 case ValueType::MAP:
126                     mapValue_ = value.mapValue_;
127                     value.mapValue_ = nullptr;
128                     break;
129                 case ValueType::BOOL:
130                     boolValue_ = value.boolValue_;
131                     break;
132                 case ValueType::STRING:
133                     new(&stringValue_)std::string(std::move(value.stringValue_));
134                     break;
135                 case ValueType::INTS:
136                     intsValue_ = value.intsValue_;
137                     value.intsValue_ = nullptr;
138                     break;
139                 case ValueType::FLOATS:
140                     floatsValue_ = value.floatsValue_;
141                     value.floatsValue_ = nullptr;
142                     break;
143                 default:
144                     break;
145             }
146             type_ = value.type_;
147             property_ = value.property_;
148             value.type_ = ValueType::UNDIFINED;
149             value.property_ = nullptr;
150             return *this;
151         }
SetPropertyConfigItem152         void SetProperty(const std::map<std::string, ConfigItem>& prop)
153         {
154             if (property_) {
155                 delete property_;
156             }
157             property_ = new std::map<std::string, ConfigItem>(prop);
158         }
159         // set map value
SetValueConfigItem160         void SetValue(const std::map<std::string, ConfigItem>& value)
161         {
162             ClearValue();
163             type_ = ValueType::MAP;
164             mapValue_ = new std::map<std::string, ConfigItem>(value);
165         }
166         // set bool value
SetValueConfigItem167         void SetValue(bool value)
168         {
169             ClearValue();
170             type_ = ValueType::BOOL;
171             boolValue_ = value;
172         }
173         // set string value
SetValueConfigItem174         void SetValue(const std::string& value)
175         {
176             ClearValue();
177             type_ = ValueType::STRING;
178             new(&stringValue_)std::string(value);
179         }
180         // set ints value
SetValueConfigItem181         void SetValue(const std::vector<int>& value)
182         {
183             ClearValue();
184             type_ = ValueType::INTS;
185             intsValue_ = new std::vector<int>(value);
186         }
187         // set floats value
SetValueConfigItem188         void SetValue(const std::vector<float>& value)
189         {
190             ClearValue();
191             type_ = ValueType::FLOATS;
192             floatsValue_ = new std::vector<float>(value);
193         }
IsIntsConfigItem194         bool IsInts() const
195         {
196             return type_ == ValueType::INTS;
197         }
IsFloatsConfigItem198         bool IsFloats() const
199         {
200             return type_ == ValueType::FLOATS;
201         }
IsStringConfigItem202         bool IsString() const
203         {
204             return type_ == ValueType::STRING;
205         }
IsBoolConfigItem206         bool IsBool() const
207         {
208             return type_ == ValueType::BOOL;
209         }
IsMapConfigItem210         bool IsMap() const
211         {
212             return type_ == ValueType::MAP;
213         }
214         const ConfigItem& operator[](const std::string& key) const
215         {
216             if (type_ != ValueType::MAP) {
217                 return DEFAULT;
218             }
219             if (mapValue_->count(key) == 0) {
220                 return DEFAULT;
221             }
222             return mapValue_->at(key);
223         }
GetPropConfigItem224         const ConfigItem& GetProp(const std::string& key) const
225         {
226             if (!property_) {
227                 return DEFAULT;
228             }
229             if (property_->count(key) == 0) {
230                 return DEFAULT;
231             }
232             return property_->at(key);
233         }
234         static const ConfigItem DEFAULT;
235     };
236     WindowManagerConfig() = delete;
237     ~WindowManagerConfig() = default;
238 
239     static bool LoadConfigXml();
GetConfig()240     static const ConfigItem& GetConfig()
241     {
242         return config_;
243     }
244     static void DumpConfig(const std::map<std::string, ConfigItem>& config);
245 
246 private:
247     static ConfigItem config_;
248     static const std::map<std::string, ValueType> configItemTypeMap_;
249 
250     static bool IsValidNode(const xmlNode& currNode);
251     static void ReadProperty(const xmlNodePtr& currNode, std::map<std::string, ConfigItem>& property);
252     static void ReadIntNumbersConfigInfo(const xmlNodePtr& currNode, std::vector<int>& intsValue);
253     static void ReadFloatNumbersConfigInfo(const xmlNodePtr& currNode, std::vector<float>& floatsValue, bool allowNeg);
254     static void ReadStringConfigInfo(const xmlNodePtr& currNode, std::string& stringValue);
255     static void ReadConfig(const xmlNodePtr& rootPtr, std::map<std::string, ConfigItem>& mapValue);
256     static std::string GetConfigPath(const std::string& configFileName);
257     static std::vector<std::string> ReadNumberStrings(const xmlNodePtr& node);
258 };
259 } // namespace Rosen
260 } // namespace OHOS
261 #endif // OHOS_ROSEN_WINDOW_MANAGER_CONFIG_H
262