Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: base/metrics/field_trial_params.h

Issue 2804633003: Add base::FeatureParam<> struct (Closed)
Patch Set: rebase Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_METRICS_FIELD_TRIAL_PARAMS_H_ 5 #ifndef BASE_METRICS_FIELD_TRIAL_PARAMS_H_
6 #define BASE_METRICS_FIELD_TRIAL_PARAMS_H_ 6 #define BASE_METRICS_FIELD_TRIAL_PARAMS_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 10
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 // Same as GetFieldTrialParamValueByFeature(). On top of that, it converts the 84 // Same as GetFieldTrialParamValueByFeature(). On top of that, it converts the
85 // string value into a boolean and returns it, if successful. Otherwise, it 85 // string value into a boolean and returns it, if successful. Otherwise, it
86 // returns |default_value|. The only string representations accepted here are 86 // returns |default_value|. The only string representations accepted here are
87 // "true" and "false". If the string value is not empty and the conversion does 87 // "true" and "false". If the string value is not empty and the conversion does
88 // not succeed, it produces a warning to LOG. 88 // not succeed, it produces a warning to LOG.
89 BASE_EXPORT bool GetFieldTrialParamByFeatureAsBool( 89 BASE_EXPORT bool GetFieldTrialParamByFeatureAsBool(
90 const base::Feature& feature, 90 const base::Feature& feature,
91 const std::string& param_name, 91 const std::string& param_name,
92 bool default_value); 92 bool default_value);
93 93
94 // Shared declaration for various FeatureParam<T> types.
95 //
96 // This template is defined for the following types T:
97 // bool
98 // int
99 // double
100 // std::string
101 // enum types
102 //
103 // See the individual definitions below for the appropriate interfaces.
104 // Attempting to use it with any other type is a compile error.
105 template <typename T, bool IsEnum = std::is_enum<T>::value>
106 struct FeatureParam {
107 // Prevent use of FeatureParam<> with unsupported types (e.g. void*). Uses T
108 // in its definition so that evaluation is deferred until the template is
109 // instantiated.
110 static_assert(!std::is_same<T, T>::value, "unsupported FeatureParam<> type");
111 };
112
113 // Declares a string-valued parameter. Example:
114 //
115 // constexpr FeatureParam<string> kAssistantName{
116 // &kAssistantFeature, "assistant_name", "HAL"};
117 //
118 // If the feature is not set, or set to the empty string, then Get() will return
119 // the default value.
120 template <>
121 struct FeatureParam<std::string> {
122 constexpr FeatureParam(const Feature* feature,
123 const char* name,
124 const char* default_value)
125 : feature(feature), name(name), default_value(default_value) {}
126
127 BASE_EXPORT std::string Get() const;
128
129 const Feature* const feature;
130 const char* const name;
131 const char* const default_value;
132 };
133
134 // Declares a double-valued parameter. Example:
135 //
136 // constexpr FeatureParam<double> kAssistantTriggerThreshold{
137 // &kAssistantFeature, "trigger_threshold", 0.10};
138 //
139 // If the feature is not set, or set to an invalid double value, then Get() will
140 // return the default value.
141 template <>
142 struct FeatureParam<double> {
143 constexpr FeatureParam(const Feature* feature,
144 const char* name,
145 double default_value)
146 : feature(feature), name(name), default_value(default_value) {}
147
148 BASE_EXPORT double Get() const;
149
150 const Feature* const feature;
151 const char* const name;
152 const double default_value;
153 };
154
155 // Declares an int-valued parameter. Example:
156 //
157 // constexpr FeatureParam<int> kAssistantParallelism{
158 // &kAssistantFeature, "parallelism", 4};
159 //
160 // If the feature is not set, or set to an invalid double value, then Get() will
Alexei Svitkine (slow) 2017/08/24 18:13:05 Nit: "invalid int value"
sfiera 2017/08/25 11:56:16 Done.
161 // return the default value.
162 template <>
163 struct FeatureParam<int> {
164 constexpr FeatureParam(const Feature* feature,
165 const char* name,
166 int default_value)
167 : feature(feature), name(name), default_value(default_value) {}
168
169 BASE_EXPORT int Get() const;
170
171 const Feature* const feature;
172 const char* const name;
173 const int default_value;
174 };
175
176 template <>
Alexei Svitkine (slow) 2017/08/24 18:13:05 Nit: Add comment for this too.
sfiera 2017/08/25 11:56:16 Done.
177 struct FeatureParam<bool> {
178 constexpr FeatureParam(const Feature* feature,
179 const char* name,
180 bool default_value)
181 : feature(feature), name(name), default_value(default_value) {}
182
183 BASE_EXPORT bool Get() const;
184
185 const Feature* const feature;
186 const char* const name;
187 const bool default_value;
188 };
189
190 BASE_EXPORT void LogInvalidEnumValue(const Feature& feature,
191 const std::string& param_name,
192 const std::string& value_as_string,
193 int default_value_as_int);
194
195 // Feature param declaration for an enum, with associated options. Example:
196 //
197 // constexpr FeatureParam<ShapeEnum>::Option[] kShapeParamOptions[] = {
198 // {SHAPE_CIRCLE, "circle"},
199 // {SHAPE_CYLINDER, "cylinder"},
200 // {SHAPE_PAPERCLIP, "paperclip"}};
201 // constexpr FeatureParam<ShapeEnum> kAssistantShapeParam{
202 // &kAssistantFeature, "shape", SHAPE_CIRCLE, &kShapeParamOptions};
203 //
204 // With this declaration, the parameter may be set to "circle", "cylinder", or
205 // "paperclip", and that will be translated to one of the three enum values. By
206 // default, or if the param is set to an unknown value, the parameter will be
207 // assumed to be SHAPE_CIRCLE.
208 template <typename Enum>
209 struct FeatureParam<Enum, true> {
210 struct Option {
211 constexpr Option(Enum value, const char* name) : value(value), name(name) {}
Alexei Svitkine (slow) 2017/08/24 18:13:05 Nit: Add empty line below it.
sfiera 2017/08/25 11:56:16 Done.
212 Enum value;
Alexei Svitkine (slow) 2017/08/24 18:13:05 const?
sfiera 2017/08/25 11:56:16 Done.
213 const char* const name;
214 };
215
216 template <int option_count>
Alexei Svitkine (slow) 2017/08/24 18:13:05 Nit: size_t? If you change it, also change the i i
sfiera 2017/08/25 11:56:16 Done.
217 constexpr FeatureParam(const Feature* feature,
218 const char* name,
219 const Enum default_value,
220 const Option (*options)[option_count])
221 : feature(feature),
222 name(name),
223 default_value(default_value),
224 options(*options),
225 option_count(option_count) {
226 static_assert(option_count >= 1, "FeatureParam<enum> has no options");
227 }
228
229 Enum Get() const {
230 std::string value = GetFieldTrialParamValueByFeature(*feature, name);
231 if (value.empty())
232 return default_value;
233 for (int i = 0; i < option_count; ++i) {
234 if (value == options[i].name)
235 return options[i].value;
236 }
237 LogInvalidEnumValue(*feature, name, value, static_cast<int>(default_value));
238 return default_value;
239 }
240
241 const base::Feature* const feature;
242 const char* const name;
243 const Enum default_value;
244 const Option* const options;
245 const int option_count;
246 };
247
94 } // namespace base 248 } // namespace base
95 249
96 #endif // BASE_METRICS_FIELD_TRIAL_PARAMS_H_ 250 #endif // BASE_METRICS_FIELD_TRIAL_PARAMS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698