1 /* 2 * Copyright (c) 2023 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_XML_CONFIG_BASE_H 17 #define OHOS_ROSEN_XML_CONFIG_BASE_H 18 19 #include <map> 20 #include <mutex> 21 #include <string> 22 #include <vector> 23 24 namespace OHOS { 25 namespace Rosen { 26 class XmlConfigBase { 27 public: 28 enum class ValueType { 29 UNDIFINED, 30 MAP, 31 BOOL, 32 STRING, 33 INTS, 34 FLOATS, 35 POSITIVE_FLOATS, 36 }; 37 struct ConfigItem { 38 std::map<std::string, ConfigItem>* property_ = nullptr; 39 ValueType type_ = ValueType::UNDIFINED; 40 union { 41 std::map<std::string, ConfigItem>* mapValue_ = nullptr; 42 bool boolValue_; 43 std::string stringValue_; 44 std::vector<int>* intsValue_; 45 std::vector<float>* floatsValue_; 46 }; ConfigItemConfigItem47 ConfigItem() {} ~ConfigItemConfigItem48 ~ConfigItem() 49 { 50 Destroy(); 51 }; DestroyConfigItem52 void Destroy() 53 { 54 ClearValue(); 55 if (property_) { 56 delete property_; 57 property_ = nullptr; 58 } 59 } ClearValueConfigItem60 void ClearValue() 61 { 62 switch (type_) { 63 case ValueType::MAP: 64 delete mapValue_; 65 mapValue_ = nullptr; 66 break; 67 case ValueType::STRING: 68 stringValue_.~basic_string(); 69 break; 70 case ValueType::INTS: 71 delete intsValue_; 72 intsValue_ = nullptr; 73 break; 74 case ValueType::FLOATS: 75 delete floatsValue_; 76 floatsValue_ = nullptr; 77 break; 78 default: 79 break; 80 } 81 } ConfigItemConfigItem82 ConfigItem(const ConfigItem& value) 83 { 84 *this = value; 85 } 86 ConfigItem& operator=(const ConfigItem& value) 87 { 88 Destroy(); 89 switch (value.type_) { 90 case ValueType::MAP: 91 mapValue_ = new std::map<std::string, ConfigItem>(*value.mapValue_); 92 break; 93 case ValueType::BOOL: 94 boolValue_ = value.boolValue_; 95 break; 96 case ValueType::STRING: 97 new(&stringValue_)std::string(value.stringValue_); 98 break; 99 case ValueType::INTS: 100 intsValue_ = new std::vector<int>(*value.intsValue_); 101 break; 102 case ValueType::FLOATS: 103 floatsValue_ = new std::vector<float>(*value.floatsValue_); 104 break; 105 default: 106 break; 107 } 108 type_ = value.type_; 109 if (value.property_) { 110 property_ = new std::map<std::string, ConfigItem>(*value.property_); 111 } 112 return *this; 113 } ConfigItemConfigItem114 ConfigItem(ConfigItem&& value) noexcept 115 { 116 *this = std::move(value); 117 } 118 ConfigItem& operator=(ConfigItem&& value) noexcept 119 { 120 Destroy(); 121 switch (value.type_) { 122 case ValueType::MAP: 123 mapValue_ = value.mapValue_; 124 value.mapValue_ = nullptr; 125 break; 126 case ValueType::BOOL: 127 boolValue_ = value.boolValue_; 128 break; 129 case ValueType::STRING: 130 new(&stringValue_)std::string(std::move(value.stringValue_)); 131 break; 132 case ValueType::INTS: 133 intsValue_ = value.intsValue_; 134 value.intsValue_ = nullptr; 135 break; 136 case ValueType::FLOATS: 137 floatsValue_ = value.floatsValue_; 138 value.floatsValue_ = nullptr; 139 break; 140 default: 141 break; 142 } 143 type_ = value.type_; 144 property_ = value.property_; 145 value.type_ = ValueType::UNDIFINED; 146 value.property_ = nullptr; 147 return *this; 148 } SetPropertyConfigItem149 void SetProperty(const std::map<std::string, ConfigItem>& prop) 150 { 151 if (property_) { 152 delete property_; 153 } 154 property_ = new std::map<std::string, ConfigItem>(prop); 155 } 156 // set map value SetValueConfigItem157 void SetValue(const std::map<std::string, ConfigItem>& value) 158 { 159 ClearValue(); 160 type_ = ValueType::MAP; 161 mapValue_ = new std::map<std::string, ConfigItem>(value); 162 } 163 // set bool value SetValueConfigItem164 void SetValue(bool value) 165 { 166 ClearValue(); 167 type_ = ValueType::BOOL; 168 boolValue_ = value; 169 } 170 // set string value SetValueConfigItem171 void SetValue(const std::string& value) 172 { 173 ClearValue(); 174 type_ = ValueType::STRING; 175 new(&stringValue_)std::string(value); 176 } 177 // set ints value SetValueConfigItem178 void SetValue(const std::vector<int>& value) 179 { 180 ClearValue(); 181 type_ = ValueType::INTS; 182 intsValue_ = new std::vector<int>(value); 183 } 184 // set floats value SetValueConfigItem185 void SetValue(const std::vector<float>& value) 186 { 187 ClearValue(); 188 type_ = ValueType::FLOATS; 189 floatsValue_ = new std::vector<float>(value); 190 } IsIntsConfigItem191 bool IsInts() const 192 { 193 return type_ == ValueType::INTS; 194 } IsFloatsConfigItem195 bool IsFloats() const 196 { 197 return type_ == ValueType::FLOATS; 198 } IsStringConfigItem199 bool IsString() const 200 { 201 return type_ == ValueType::STRING; 202 } IsBoolConfigItem203 bool IsBool() const 204 { 205 return type_ == ValueType::BOOL; 206 } IsMapConfigItem207 bool IsMap() const 208 { 209 return type_ == ValueType::MAP; 210 } 211 const ConfigItem& operator[](const std::string& key) const 212 { 213 if (type_ != ValueType::MAP) { 214 return DEFAULT; 215 } 216 if (mapValue_->count(key) == 0) { 217 return DEFAULT; 218 } 219 return mapValue_->at(key); 220 } GetPropConfigItem221 const ConfigItem& GetProp(const std::string& key) const 222 { 223 if (!property_) { 224 return DEFAULT; 225 } 226 if (property_->count(key) == 0) { 227 return DEFAULT; 228 } 229 return property_->at(key); 230 } 231 static const ConfigItem DEFAULT; 232 }; 233 static std::recursive_mutex mutex_; 234 }; 235 } // namespace Rosen 236 } // namespace OHOS 237 238 #endif // OHOS_ROSEN_XML_CONFIG_BASE_H