Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | #ifndef RTC_BASE_EXPERIMENTS_FIELD_TRIAL_PARSER_H_ |
| 11 | #define RTC_BASE_EXPERIMENTS_FIELD_TRIAL_PARSER_H_ |
| 12 | |
| 13 | #include <stdint.h> |
| 14 | #include <initializer_list> |
Sebastian Jansson | 2c74d85 | 2018-06-26 10:30:16 +0200 | [diff] [blame] | 15 | #include <map> |
| 16 | #include <set> |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 17 | #include <string> |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 18 | #include "absl/types/optional.h" |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 19 | |
| 20 | // Field trial parser functionality. Provides funcitonality to parse field trial |
| 21 | // argument strings in key:value format. Each parameter is described using |
| 22 | // key:value, parameters are separated with a ,. Values can't include the comma |
| 23 | // character, since there's no quote facility. For most types, white space is |
| 24 | // ignored. Parameters are declared with a given type for which an |
| 25 | // implementation of ParseTypedParameter should be provided. The |
| 26 | // ParseTypedParameter implementation is given whatever is between the : and the |
| 27 | // ,. FieldTrialOptional will use nullopt if the key is provided without :. |
| 28 | |
| 29 | // Example string: "my_optional,my_int:3,my_string:hello" |
| 30 | |
| 31 | // For further description of usage and behavior, see the examples in the unit |
| 32 | // tests. |
| 33 | |
| 34 | namespace webrtc { |
| 35 | class FieldTrialParameterInterface { |
| 36 | public: |
| 37 | virtual ~FieldTrialParameterInterface(); |
| 38 | |
| 39 | protected: |
Sebastian Jansson | 343f414 | 2018-10-05 19:44:46 +0200 | [diff] [blame] | 40 | // Protected to allow implementations to provide assignment and copy. |
| 41 | FieldTrialParameterInterface(const FieldTrialParameterInterface&) = default; |
| 42 | FieldTrialParameterInterface& operator=(const FieldTrialParameterInterface&) = |
| 43 | default; |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 44 | explicit FieldTrialParameterInterface(std::string key); |
| 45 | friend void ParseFieldTrial( |
| 46 | std::initializer_list<FieldTrialParameterInterface*> fields, |
| 47 | std::string raw_string); |
Sebastian Jansson | fea4637 | 2018-09-03 10:15:13 +0200 | [diff] [blame] | 48 | void MarkAsUsed() { used_ = true; } |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 49 | virtual bool Parse(absl::optional<std::string> str_value) = 0; |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 50 | std::string Key() const; |
| 51 | |
| 52 | private: |
Sebastian Jansson | 343f414 | 2018-10-05 19:44:46 +0200 | [diff] [blame] | 53 | std::string key_; |
Sebastian Jansson | fea4637 | 2018-09-03 10:15:13 +0200 | [diff] [blame] | 54 | bool used_ = false; |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | // ParseFieldTrial function parses the given string and fills the given fields |
Sebastian Jansson | d69998a | 2018-12-20 12:25:19 +0100 | [diff] [blame] | 58 | // with extracted values if available. |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 59 | void ParseFieldTrial( |
| 60 | std::initializer_list<FieldTrialParameterInterface*> fields, |
| 61 | std::string raw_string); |
| 62 | |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 63 | // Specialize this in code file for custom types. Should return absl::nullopt if |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 64 | // the given string cannot be properly parsed. |
| 65 | template <typename T> |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 66 | absl::optional<T> ParseTypedParameter(std::string); |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 67 | |
| 68 | // This class uses the ParseTypedParameter function to implement a parameter |
| 69 | // implementation with an enforced default value. |
| 70 | template <typename T> |
| 71 | class FieldTrialParameter : public FieldTrialParameterInterface { |
| 72 | public: |
| 73 | FieldTrialParameter(std::string key, T default_value) |
| 74 | : FieldTrialParameterInterface(key), value_(default_value) {} |
| 75 | T Get() const { return value_; } |
| 76 | operator T() const { return Get(); } |
Sebastian Jansson | fea4637 | 2018-09-03 10:15:13 +0200 | [diff] [blame] | 77 | const T* operator->() const { return &value_; } |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 78 | |
| 79 | protected: |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 80 | bool Parse(absl::optional<std::string> str_value) override { |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 81 | if (str_value) { |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 82 | absl::optional<T> value = ParseTypedParameter<T>(*str_value); |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 83 | if (value.has_value()) { |
| 84 | value_ = value.value(); |
| 85 | return true; |
| 86 | } |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | T value_; |
| 93 | }; |
| 94 | |
Sebastian Jansson | b22f077 | 2018-11-19 17:44:33 +0100 | [diff] [blame] | 95 | // This class uses the ParseTypedParameter function to implement a parameter |
| 96 | // implementation with an enforced default value and a range constraint. Values |
| 97 | // outside the configured range will be ignored. |
| 98 | template <typename T> |
| 99 | class FieldTrialConstrained : public FieldTrialParameterInterface { |
| 100 | public: |
| 101 | FieldTrialConstrained(std::string key, |
| 102 | T default_value, |
| 103 | absl::optional<T> lower_limit, |
| 104 | absl::optional<T> upper_limit) |
| 105 | : FieldTrialParameterInterface(key), |
| 106 | value_(default_value), |
| 107 | lower_limit_(lower_limit), |
| 108 | upper_limit_(upper_limit) {} |
| 109 | T Get() const { return value_; } |
| 110 | operator T() const { return Get(); } |
| 111 | const T* operator->() const { return &value_; } |
| 112 | |
| 113 | protected: |
| 114 | bool Parse(absl::optional<std::string> str_value) override { |
| 115 | if (str_value) { |
| 116 | absl::optional<T> value = ParseTypedParameter<T>(*str_value); |
| 117 | if (value && (!lower_limit_ || *value >= *lower_limit_) && |
| 118 | (!upper_limit_ || *value <= *upper_limit_)) { |
| 119 | value_ = *value; |
| 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | private: |
| 127 | T value_; |
| 128 | absl::optional<T> lower_limit_; |
| 129 | absl::optional<T> upper_limit_; |
| 130 | }; |
| 131 | |
Sebastian Jansson | 2c74d85 | 2018-06-26 10:30:16 +0200 | [diff] [blame] | 132 | class AbstractFieldTrialEnum : public FieldTrialParameterInterface { |
| 133 | public: |
| 134 | AbstractFieldTrialEnum(std::string key, |
| 135 | int default_value, |
| 136 | std::map<std::string, int> mapping); |
| 137 | ~AbstractFieldTrialEnum() override; |
| 138 | AbstractFieldTrialEnum(const AbstractFieldTrialEnum&); |
| 139 | |
| 140 | protected: |
| 141 | bool Parse(absl::optional<std::string> str_value) override; |
| 142 | |
| 143 | protected: |
| 144 | int value_; |
| 145 | std::map<std::string, int> enum_mapping_; |
| 146 | std::set<int> valid_values_; |
| 147 | }; |
| 148 | |
| 149 | // The FieldTrialEnum class can be used to quickly define a parser for a |
| 150 | // specific enum. It handles values provided as integers and as strings if a |
| 151 | // mapping is provided. |
| 152 | template <typename T> |
| 153 | class FieldTrialEnum : public AbstractFieldTrialEnum { |
| 154 | public: |
| 155 | FieldTrialEnum(std::string key, |
| 156 | T default_value, |
| 157 | std::map<std::string, T> mapping) |
| 158 | : AbstractFieldTrialEnum(key, |
| 159 | static_cast<int>(default_value), |
| 160 | ToIntMap(mapping)) {} |
| 161 | T Get() const { return static_cast<T>(value_); } |
| 162 | operator T() const { return Get(); } |
| 163 | |
| 164 | private: |
| 165 | static std::map<std::string, int> ToIntMap(std::map<std::string, T> mapping) { |
| 166 | std::map<std::string, int> res; |
| 167 | for (const auto& it : mapping) |
| 168 | res[it.first] = static_cast<int>(it.second); |
| 169 | return res; |
| 170 | } |
| 171 | }; |
| 172 | |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 173 | // This class uses the ParseTypedParameter function to implement an optional |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 174 | // parameter implementation that can default to absl::nullopt. |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 175 | template <typename T> |
| 176 | class FieldTrialOptional : public FieldTrialParameterInterface { |
| 177 | public: |
| 178 | explicit FieldTrialOptional(std::string key) |
| 179 | : FieldTrialParameterInterface(key) {} |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 180 | FieldTrialOptional(std::string key, absl::optional<T> default_value) |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 181 | : FieldTrialParameterInterface(key), value_(default_value) {} |
Sebastian Jansson | fea4637 | 2018-09-03 10:15:13 +0200 | [diff] [blame] | 182 | absl::optional<T> GetOptional() const { return value_; } |
| 183 | const T& Value() const { return value_.value(); } |
| 184 | const T& operator*() const { return value_.value(); } |
| 185 | const T* operator->() const { return &value_.value(); } |
Jonas Olsson | cb96809 | 2019-03-12 13:57:15 +0100 | [diff] [blame^] | 186 | explicit operator bool() const { return value_.has_value(); } |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 187 | |
| 188 | protected: |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 189 | bool Parse(absl::optional<std::string> str_value) override { |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 190 | if (str_value) { |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 191 | absl::optional<T> value = ParseTypedParameter<T>(*str_value); |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 192 | if (!value.has_value()) |
| 193 | return false; |
| 194 | value_ = value.value(); |
| 195 | } else { |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 196 | value_ = absl::nullopt; |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 197 | } |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | private: |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 202 | absl::optional<T> value_; |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | // Equivalent to a FieldTrialParameter<bool> in the case that both key and value |
| 206 | // are present. If key is missing, evaluates to false. If key is present, but no |
| 207 | // explicit value is provided, the flag evaluates to true. |
| 208 | class FieldTrialFlag : public FieldTrialParameterInterface { |
| 209 | public: |
| 210 | explicit FieldTrialFlag(std::string key); |
| 211 | FieldTrialFlag(std::string key, bool default_value); |
| 212 | bool Get() const; |
Sebastian Jansson | fea4637 | 2018-09-03 10:15:13 +0200 | [diff] [blame] | 213 | operator bool() const; |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 214 | |
| 215 | protected: |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 216 | bool Parse(absl::optional<std::string> str_value) override; |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 217 | |
| 218 | private: |
| 219 | bool value_; |
| 220 | }; |
| 221 | |
| 222 | // Accepts true, false, else parsed with sscanf %i, true if != 0. |
| 223 | extern template class FieldTrialParameter<bool>; |
| 224 | // Interpreted using sscanf %lf. |
| 225 | extern template class FieldTrialParameter<double>; |
| 226 | // Interpreted using sscanf %i. |
| 227 | extern template class FieldTrialParameter<int>; |
| 228 | // Using the given value as is. |
| 229 | extern template class FieldTrialParameter<std::string>; |
| 230 | |
Sebastian Jansson | b22f077 | 2018-11-19 17:44:33 +0100 | [diff] [blame] | 231 | extern template class FieldTrialConstrained<double>; |
| 232 | extern template class FieldTrialConstrained<int>; |
| 233 | |
Sebastian Jansson | 9eb3886 | 2018-06-14 16:47:42 +0200 | [diff] [blame] | 234 | extern template class FieldTrialOptional<double>; |
| 235 | extern template class FieldTrialOptional<int>; |
| 236 | extern template class FieldTrialOptional<bool>; |
| 237 | extern template class FieldTrialOptional<std::string>; |
| 238 | |
| 239 | } // namespace webrtc |
| 240 | |
| 241 | #endif // RTC_BASE_EXPERIMENTS_FIELD_TRIAL_PARSER_H_ |