1 /*
2 * Copyright (c) 2022-2025 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 #include "parameters.h"
17
18 #include <map>
19 #include <mutex>
20
21 namespace OHOS {
22 namespace system {
23 std::mutex mutex;
24
25 class ParameterMapWrapper {
26 public:
27 std::map<std::string, std::string> paramMap;
28 };
29
GetParameterMap()30 ParameterMapWrapper* GetParameterMap()
31 {
32 static auto* paramMapWrapper = new (std::nothrow) ParameterMapWrapper();
33 return paramMapWrapper;
34 }
35
GetDeviceType()36 std::string GetDeviceType()
37 {
38 return "default";
39 }
40
GetBoolParameter(const std::string & key,bool def)41 bool GetBoolParameter(const std::string& key, bool def)
42 {
43 #ifndef GET_BOOL_PARAMETER_TRUE
44 auto item = GetParameterMap()->paramMap.find(key);
45 if (item == GetParameterMap()->paramMap.end()) {
46 return def;
47 }
48 return "true" == item->second;
49 #else
50 auto item = GetParameterMap()->paramMap.find(key);
51 if (item == GetParameterMap()->paramMap.end()) {
52 return true;
53 }
54 return "true" == item->second;
55 #endif
56 }
57
58 template<typename T>
GetIntParameter(const std::string & key,T def)59 T GetIntParameter(const std::string& key, T def)
60 {
61 std::lock_guard<std::mutex> lock(mutex);
62 try {
63 auto item = GetParameterMap()->paramMap.find(key);
64 if (item == GetParameterMap()->paramMap.end()) {
65 return def;
66 }
67 return atoi(item->second.c_str());
68 } catch (...) {
69 return def;
70 }
71 }
72
GetParameter(const std::string & key,const std::string & def)73 std::string GetParameter(const std::string& key, const std::string& def)
74 {
75 std::lock_guard<std::mutex> lock(mutex);
76 auto item = GetParameterMap()->paramMap.find(key);
77 if (item == GetParameterMap()->paramMap.end()) {
78 return def;
79 }
80 return item->second;
81 }
82
RemoveParameter(const std::string & key)83 void RemoveParameter(const std::string& key)
84 {
85 std::lock_guard<std::mutex> lock(mutex);
86 GetParameterMap()->paramMap.erase(key);
87 }
88
SetParameter(const std::string & key,const std::string & val)89 bool SetParameter(const std::string& key, const std::string& val)
90 {
91 std::lock_guard<std::mutex> lock(mutex);
92 GetParameterMap()->paramMap[key] = val;
93 return true;
94 }
95
96 template int32_t GetIntParameter(const std::string& key, int32_t def);
97 } // system
98 } // OHOS