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