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 #ifndef RS_PROFILER_SETTINGS_H
17 #define RS_PROFILER_SETTINGS_H
18 
19 #include <mutex>
20 
21 #include "rs_profiler_utils.h"
22 
23 namespace OHOS::Rosen {
24 
25 // SystemParameter
26 class RSB_EXPORT SystemParameter {
27 public:
28     explicit SystemParameter(std::string name);
29     explicit SystemParameter(const SystemParameter&) = delete;
30     virtual ~SystemParameter();
31 
32     SystemParameter& operator=(const SystemParameter& parameter);
33 
Name()34     const std::string& Name() const
35     {
36         return name_;
37     }
38 
39     virtual void FromString(const std::string& value) = 0;
40     virtual std::string ToString() const = 0;
41 
42     static SystemParameter* Find(const std::string& name);
43     static bool Exists(const std::string& name);
44 
45     static bool Set(const std::string& name, const std::string& value);
46     static std::string Get(const std::string& name);
47 
48     static std::string Dump();
49 
50 protected:
51     std::string name_;
52 
53 private:
54     static std::mutex mutex_;
55     static std::vector<SystemParameter*> registry_;
56 };
57 
58 // TemplateParameter
59 template<typename Value>
60 class RSB_EXPORT TemplateParameter : public SystemParameter {
61 public:
TemplateParameter(const std::string & name,Value value)62     explicit TemplateParameter(const std::string& name, Value value) : SystemParameter(name), value_(std::move(value))
63     {}
64     ~TemplateParameter() override = default;
65 
Set(const Value & value)66     void Set(const Value& value)
67     {
68         value_ = value;
69     }
70 
Get()71     const Value& Get() const
72     {
73         return value_;
74     }
75 
76     const Value& operator*() const
77     {
78         return Get();
79     }
80 
81 protected:
82     Value value_;
83 };
84 
85 // NumeralParameter
86 template<typename Numeral>
87 class RSB_EXPORT NumeralParameter final : public TemplateParameter<Numeral> {
88 public:
89     explicit NumeralParameter(const std::string& name, Numeral value = 0) : TemplateParameter<Numeral>(name, value) {}
90     ~NumeralParameter() override = default;
91 
FromString(const std::string & value)92     void FromString(const std::string& value) override
93     {
94         NumeralParameter::Set(Utils::ToNumber<Numeral>(value));
95     }
96 
ToString()97     std::string ToString() const override
98     {
99         return std::to_string(NumeralParameter::Get());
100     }
101 
102     NumeralParameter& operator=(Numeral value)
103     {
104         Set(value);
105         return *this;
106     }
107 };
108 
109 using Int8Parameter = NumeralParameter<int8_t>;
110 using Int16Parameter = NumeralParameter<int16_t>;
111 using Int32Parameter = NumeralParameter<int32_t>;
112 using Int64Parameter = NumeralParameter<int64_t>;
113 using Uint8Parameter = NumeralParameter<uint8_t>;
114 using Uint16Parameter = NumeralParameter<uint16_t>;
115 using Uint32Parameter = NumeralParameter<uint32_t>;
116 using Uint64Parameter = NumeralParameter<uint64_t>;
117 using FloatParameter = NumeralParameter<float>;
118 using DoubleParameter = NumeralParameter<double>;
119 
120 // BoolParameter
121 class RSB_EXPORT BoolParameter final : public TemplateParameter<bool> {
122 public:
123     explicit BoolParameter(const std::string& name, bool value = false) : TemplateParameter<bool>(name, value) {}
124     ~BoolParameter() override = default;
125 
FromString(const std::string & value)126     void FromString(const std::string& value) override
127     {
128         value_ = (value == "true") || (value == "on") || (value == "1");
129     }
130 
ToString()131     std::string ToString() const override
132     {
133         return value_ ? "true" : "false";
134     }
135 
136     BoolParameter& operator=(bool value)
137     {
138         Set(value);
139         return *this;
140     }
141 };
142 
143 // StringParameter
144 class RSB_EXPORT StringParameter final : public TemplateParameter<std::string> {
145 public:
146     explicit StringParameter(const std::string& name, const std::string& value = "") : TemplateParameter(name, value) {}
147     ~StringParameter() override = default;
148 
FromString(const std::string & value)149     void FromString(const std::string& value) override
150     {
151         Set(value);
152     }
153 
ToString()154     std::string ToString() const override
155     {
156         return Get();
157     }
158 
Data()159     const char* Data() const
160     {
161         return value_.data();
162     }
163 
164     StringParameter& operator=(const std::string& value)
165     {
166         Set(value);
167         return *this;
168     }
169 };
170 
171 } // namespace OHOS::Rosen
172 
173 #endif // RS_PROFILER_SETTINGS_H