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

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

Issue 2804633003: Add base::FeatureParam<> struct (Closed)
Patch Set: Remove constexpr for Windows 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
« no previous file with comments | « base/BUILD.gn ('k') | base/metrics/field_trial_params.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 int value, then Get() will
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 // Declares a bool-valued parameter. Example:
177 //
178 // constexpr FeatureParam<int> kAssistantIsHelpful{
179 // &kAssistantFeature, "is_helpful", true};
180 //
181 // If the feature is not set, or set to value other than "true" or "false", then
182 // Get() will return the default value.
183 template <>
184 struct FeatureParam<bool> {
185 constexpr FeatureParam(const Feature* feature,
186 const char* name,
187 bool default_value)
188 : feature(feature), name(name), default_value(default_value) {}
189
190 BASE_EXPORT bool Get() const;
191
192 const Feature* const feature;
193 const char* const name;
194 const bool default_value;
195 };
196
197 BASE_EXPORT void LogInvalidEnumValue(const Feature& feature,
198 const std::string& param_name,
199 const std::string& value_as_string,
200 int default_value_as_int);
201
202 // Feature param declaration for an enum, with associated options. Example:
203 //
204 // constexpr FeatureParam<ShapeEnum>::Option[] kShapeParamOptions[] = {
205 // {SHAPE_CIRCLE, "circle"},
206 // {SHAPE_CYLINDER, "cylinder"},
207 // {SHAPE_PAPERCLIP, "paperclip"}};
208 // constexpr FeatureParam<ShapeEnum> kAssistantShapeParam{
209 // &kAssistantFeature, "shape", SHAPE_CIRCLE, &kShapeParamOptions};
210 //
211 // With this declaration, the parameter may be set to "circle", "cylinder", or
212 // "paperclip", and that will be translated to one of the three enum values. By
213 // default, or if the param is set to an unknown value, the parameter will be
214 // assumed to be SHAPE_CIRCLE.
215 template <typename Enum>
216 struct FeatureParam<Enum, true> {
217 struct Option {
218 constexpr Option(Enum value, const char* name) : value(value), name(name) {}
219
220 const Enum value;
221 const char* const name;
222 };
223
224 template <size_t option_count>
225 constexpr FeatureParam(const Feature* feature,
226 const char* name,
227 const Enum default_value,
228 const Option (*options)[option_count])
229 : feature(feature),
230 name(name),
231 default_value(default_value),
232 options(*options),
233 option_count(option_count) {
234 static_assert(option_count >= 1, "FeatureParam<enum> has no options");
235 }
236
237 Enum Get() const {
238 std::string value = GetFieldTrialParamValueByFeature(*feature, name);
239 if (value.empty())
240 return default_value;
241 for (size_t i = 0; i < option_count; ++i) {
242 if (value == options[i].name)
243 return options[i].value;
244 }
245 LogInvalidEnumValue(*feature, name, value, static_cast<int>(default_value));
246 return default_value;
247 }
248
249 const base::Feature* const feature;
250 const char* const name;
251 const Enum default_value;
252 const Option* const options;
253 const size_t option_count;
254 };
255
94 } // namespace base 256 } // namespace base
95 257
96 #endif // BASE_METRICS_FIELD_TRIAL_PARAMS_H_ 258 #endif // BASE_METRICS_FIELD_TRIAL_PARAMS_H_
OLDNEW
« no previous file with comments | « base/BUILD.gn ('k') | base/metrics/field_trial_params.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698