1 /*
2  * Copyright (c) 2022-2024 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::map<std::string, std::string> paramMap;
24 std::mutex mutex;
25 
GetDeviceType()26 std::string GetDeviceType()
27 {
28     return "default";
29 }
30 
GetBoolParameter(const std::string & key,bool def)31 bool GetBoolParameter(const std::string& key, bool def)
32 {
33 #ifndef GET_BOOL_PARAMETER_TRUE
34     return false;
35 #else
36     return true;
37 #endif
38 }
39 
40 template<typename T>
GetIntParameter(const std::string & key,T def)41 T GetIntParameter(const std::string& key, T def)
42 {
43     std::lock_guard<std::mutex> lock(mutex);
44     try {
45         auto item = paramMap.find(key);
46         if (item == paramMap.end()) {
47             return def;
48         }
49         return std::stoi(item->second);
50     } catch (...) {
51         return def;
52     }
53 }
54 
GetParameter(const std::string & key,const std::string & def)55 std::string GetParameter(const std::string& key, const std::string& def)
56 {
57     std::lock_guard<std::mutex> lock(mutex);
58     auto item = paramMap.find(key);
59     if (item == paramMap.end()) {
60         return def;
61     }
62     return item->second;
63 }
64 
SetParameter(const std::string & key,const std::string & val)65 bool SetParameter(const std::string& key, const std::string& val)
66 {
67     std::lock_guard<std::mutex> lock(mutex);
68     paramMap[key] = val;
69     return true;
70 }
71 
72 template int32_t GetIntParameter(const std::string& key, int32_t def);
73 } // system
74 } // OHOS