1 /*
2  * Copyright (c) 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 "rs_profiler_settings.h"
17 
18 #include <string>
19 
20 namespace OHOS::Rosen {
21 
22 std::mutex SystemParameter::mutex_;
23 std::vector<SystemParameter*> SystemParameter::registry_;
24 
SystemParameter(std::string name)25 SystemParameter::SystemParameter(std::string name) : name_(std::move(name))
26 {
27     if (!name_.empty() && !Exists(name_)) {
28         const std::lock_guard<std::mutex> guard(mutex_);
29         registry_.emplace_back(this);
30     }
31 }
32 
~SystemParameter()33 SystemParameter::~SystemParameter()
34 {
35     const std::lock_guard<std::mutex> guard(mutex_);
36     const auto position = std::find(registry_.begin(), registry_.end(), this);
37     if (position != registry_.end()) {
38         registry_.erase(position);
39     }
40 }
41 
operator =(const SystemParameter & parameter)42 SystemParameter& SystemParameter::operator=(const SystemParameter& parameter)
43 {
44     if (this != &parameter) {
45         FromString(parameter.ToString());
46     }
47     return *this;
48 }
49 
Find(const std::string & name)50 SystemParameter* SystemParameter::Find(const std::string& name)
51 {
52     const std::lock_guard<std::mutex> guard(mutex_);
53     const auto parameter = std::find_if(
54         registry_.begin(), registry_.end(), [&name](SystemParameter* parameter) { return parameter->Name() == name; });
55     return (parameter != registry_.end()) ? *parameter : nullptr;
56 }
57 
Exists(const std::string & name)58 bool SystemParameter::Exists(const std::string& name)
59 {
60     return Find(name) != nullptr;
61 }
62 
Set(const std::string & name,const std::string & value)63 bool SystemParameter::Set(const std::string& name, const std::string& value)
64 {
65     if (auto parameter = Find(name)) {
66         parameter->FromString(value);
67         return true;
68     }
69     return false;
70 }
71 
Get(const std::string & name)72 std::string SystemParameter::Get(const std::string& name)
73 {
74     static const std::string DUMMY;
75     const auto parameter = Find(name);
76     return parameter ? parameter->ToString() : DUMMY;
77 }
78 
Dump()79 std::string SystemParameter::Dump()
80 {
81     std::string out;
82     const std::lock_guard<std::mutex> guard(mutex_);
83     for (const auto parameter : registry_) {
84         out += parameter->Name() + " = " + parameter->ToString() + "\n";
85     }
86     return out;
87 }
88 
89 } // namespace OHOS::Rosen