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

Unified Diff: base/metrics/field_trial_params.h

Issue 2804633003: Add base::FeatureParam<> struct (Closed)
Patch Set: rebase Created 3 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: base/metrics/field_trial_params.h
diff --git a/base/metrics/field_trial_params.h b/base/metrics/field_trial_params.h
index 2490149f69457cca85f9798bd192ebd194ae7bfc..eff75bece7fffe4c06adb14a42e20acff63abf51 100644
--- a/base/metrics/field_trial_params.h
+++ b/base/metrics/field_trial_params.h
@@ -91,6 +91,160 @@ BASE_EXPORT bool GetFieldTrialParamByFeatureAsBool(
const std::string& param_name,
bool default_value);
+// Shared declaration for various FeatureParam<T> types.
+//
+// This template is defined for the following types T:
+// bool
+// int
+// double
+// std::string
+// enum types
+//
+// See the individual definitions below for the appropriate interfaces.
+// Attempting to use it with any other type is a compile error.
+template <typename T, bool IsEnum = std::is_enum<T>::value>
+struct FeatureParam {
+ // Prevent use of FeatureParam<> with unsupported types (e.g. void*). Uses T
+ // in its definition so that evaluation is deferred until the template is
+ // instantiated.
+ static_assert(!std::is_same<T, T>::value, "unsupported FeatureParam<> type");
+};
+
+// Declares a string-valued parameter. Example:
+//
+// constexpr FeatureParam<string> kAssistantName{
+// &kAssistantFeature, "assistant_name", "HAL"};
+//
+// If the feature is not set, or set to the empty string, then Get() will return
+// the default value.
+template <>
+struct FeatureParam<std::string> {
+ constexpr FeatureParam(const Feature* feature,
+ const char* name,
+ const char* default_value)
+ : feature(feature), name(name), default_value(default_value) {}
+
+ BASE_EXPORT std::string Get() const;
+
+ const Feature* const feature;
+ const char* const name;
+ const char* const default_value;
+};
+
+// Declares a double-valued parameter. Example:
+//
+// constexpr FeatureParam<double> kAssistantTriggerThreshold{
+// &kAssistantFeature, "trigger_threshold", 0.10};
+//
+// If the feature is not set, or set to an invalid double value, then Get() will
+// return the default value.
+template <>
+struct FeatureParam<double> {
+ constexpr FeatureParam(const Feature* feature,
+ const char* name,
+ double default_value)
+ : feature(feature), name(name), default_value(default_value) {}
+
+ BASE_EXPORT double Get() const;
+
+ const Feature* const feature;
+ const char* const name;
+ const double default_value;
+};
+
+// Declares an int-valued parameter. Example:
+//
+// constexpr FeatureParam<int> kAssistantParallelism{
+// &kAssistantFeature, "parallelism", 4};
+//
+// 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.
+// return the default value.
+template <>
+struct FeatureParam<int> {
+ constexpr FeatureParam(const Feature* feature,
+ const char* name,
+ int default_value)
+ : feature(feature), name(name), default_value(default_value) {}
+
+ BASE_EXPORT int Get() const;
+
+ const Feature* const feature;
+ const char* const name;
+ const int default_value;
+};
+
+template <>
Alexei Svitkine (slow) 2017/08/24 18:13:05 Nit: Add comment for this too.
sfiera 2017/08/25 11:56:16 Done.
+struct FeatureParam<bool> {
+ constexpr FeatureParam(const Feature* feature,
+ const char* name,
+ bool default_value)
+ : feature(feature), name(name), default_value(default_value) {}
+
+ BASE_EXPORT bool Get() const;
+
+ const Feature* const feature;
+ const char* const name;
+ const bool default_value;
+};
+
+BASE_EXPORT void LogInvalidEnumValue(const Feature& feature,
+ const std::string& param_name,
+ const std::string& value_as_string,
+ int default_value_as_int);
+
+// Feature param declaration for an enum, with associated options. Example:
+//
+// constexpr FeatureParam<ShapeEnum>::Option[] kShapeParamOptions[] = {
+// {SHAPE_CIRCLE, "circle"},
+// {SHAPE_CYLINDER, "cylinder"},
+// {SHAPE_PAPERCLIP, "paperclip"}};
+// constexpr FeatureParam<ShapeEnum> kAssistantShapeParam{
+// &kAssistantFeature, "shape", SHAPE_CIRCLE, &kShapeParamOptions};
+//
+// With this declaration, the parameter may be set to "circle", "cylinder", or
+// "paperclip", and that will be translated to one of the three enum values. By
+// default, or if the param is set to an unknown value, the parameter will be
+// assumed to be SHAPE_CIRCLE.
+template <typename Enum>
+struct FeatureParam<Enum, true> {
+ struct Option {
+ 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.
+ Enum value;
Alexei Svitkine (slow) 2017/08/24 18:13:05 const?
sfiera 2017/08/25 11:56:16 Done.
+ const char* const name;
+ };
+
+ 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.
+ constexpr FeatureParam(const Feature* feature,
+ const char* name,
+ const Enum default_value,
+ const Option (*options)[option_count])
+ : feature(feature),
+ name(name),
+ default_value(default_value),
+ options(*options),
+ option_count(option_count) {
+ static_assert(option_count >= 1, "FeatureParam<enum> has no options");
+ }
+
+ Enum Get() const {
+ std::string value = GetFieldTrialParamValueByFeature(*feature, name);
+ if (value.empty())
+ return default_value;
+ for (int i = 0; i < option_count; ++i) {
+ if (value == options[i].name)
+ return options[i].value;
+ }
+ LogInvalidEnumValue(*feature, name, value, static_cast<int>(default_value));
+ return default_value;
+ }
+
+ const base::Feature* const feature;
+ const char* const name;
+ const Enum default_value;
+ const Option* const options;
+ const int option_count;
+};
+
} // namespace base
#endif // BASE_METRICS_FIELD_TRIAL_PARAMS_H_

Powered by Google App Engine
This is Rietveld 408576698