blob: 0330f1970a8c19b7bdfeb858c28ac5a70701f5c5 [file] [log] [blame]
Sebastian Jansson9eb38862018-06-14 16:47:42 +02001/*
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 Jansson2c74d852018-06-26 10:30:16 +020015#include <map>
16#include <set>
Sebastian Jansson9eb38862018-06-14 16:47:42 +020017#include <string>
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020018#include "absl/types/optional.h"
Sebastian Jansson9eb38862018-06-14 16:47:42 +020019
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
34namespace webrtc {
35class FieldTrialParameterInterface {
36 public:
37 virtual ~FieldTrialParameterInterface();
38
39 protected:
Sebastian Jansson343f4142018-10-05 19:44:46 +020040 // Protected to allow implementations to provide assignment and copy.
41 FieldTrialParameterInterface(const FieldTrialParameterInterface&) = default;
42 FieldTrialParameterInterface& operator=(const FieldTrialParameterInterface&) =
43 default;
Sebastian Jansson9eb38862018-06-14 16:47:42 +020044 explicit FieldTrialParameterInterface(std::string key);
45 friend void ParseFieldTrial(
46 std::initializer_list<FieldTrialParameterInterface*> fields,
47 std::string raw_string);
Sebastian Janssonfea46372018-09-03 10:15:13 +020048 void MarkAsUsed() { used_ = true; }
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020049 virtual bool Parse(absl::optional<std::string> str_value) = 0;
Sebastian Jansson9eb38862018-06-14 16:47:42 +020050 std::string Key() const;
51
52 private:
Sebastian Jansson343f4142018-10-05 19:44:46 +020053 std::string key_;
Sebastian Janssonfea46372018-09-03 10:15:13 +020054 bool used_ = false;
Sebastian Jansson9eb38862018-06-14 16:47:42 +020055};
56
57// ParseFieldTrial function parses the given string and fills the given fields
Sebastian Janssond69998a2018-12-20 12:25:19 +010058// with extracted values if available.
Sebastian Jansson9eb38862018-06-14 16:47:42 +020059void ParseFieldTrial(
60 std::initializer_list<FieldTrialParameterInterface*> fields,
61 std::string raw_string);
62
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020063// Specialize this in code file for custom types. Should return absl::nullopt if
Sebastian Jansson9eb38862018-06-14 16:47:42 +020064// the given string cannot be properly parsed.
65template <typename T>
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020066absl::optional<T> ParseTypedParameter(std::string);
Sebastian Jansson9eb38862018-06-14 16:47:42 +020067
68// This class uses the ParseTypedParameter function to implement a parameter
69// implementation with an enforced default value.
70template <typename T>
71class 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 Janssonfea46372018-09-03 10:15:13 +020077 const T* operator->() const { return &value_; }
Sebastian Jansson9eb38862018-06-14 16:47:42 +020078
79 protected:
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020080 bool Parse(absl::optional<std::string> str_value) override {
Sebastian Jansson9eb38862018-06-14 16:47:42 +020081 if (str_value) {
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020082 absl::optional<T> value = ParseTypedParameter<T>(*str_value);
Sebastian Jansson9eb38862018-06-14 16:47:42 +020083 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 Janssonb22f0772018-11-19 17:44:33 +010095// 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.
98template <typename T>
99class 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 Jansson2c74d852018-06-26 10:30:16 +0200132class 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.
152template <typename T>
153class 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 Jansson9eb38862018-06-14 16:47:42 +0200173// This class uses the ParseTypedParameter function to implement an optional
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200174// parameter implementation that can default to absl::nullopt.
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200175template <typename T>
176class FieldTrialOptional : public FieldTrialParameterInterface {
177 public:
178 explicit FieldTrialOptional(std::string key)
179 : FieldTrialParameterInterface(key) {}
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200180 FieldTrialOptional(std::string key, absl::optional<T> default_value)
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200181 : FieldTrialParameterInterface(key), value_(default_value) {}
Sebastian Janssonfea46372018-09-03 10:15:13 +0200182 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(); }
186 operator bool() const { return value_.has_value(); }
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200187
188 protected:
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200189 bool Parse(absl::optional<std::string> str_value) override {
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200190 if (str_value) {
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200191 absl::optional<T> value = ParseTypedParameter<T>(*str_value);
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200192 if (!value.has_value())
193 return false;
194 value_ = value.value();
195 } else {
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200196 value_ = absl::nullopt;
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200197 }
198 return true;
199 }
200
201 private:
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200202 absl::optional<T> value_;
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200203};
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.
208class FieldTrialFlag : public FieldTrialParameterInterface {
209 public:
210 explicit FieldTrialFlag(std::string key);
211 FieldTrialFlag(std::string key, bool default_value);
212 bool Get() const;
Sebastian Janssonfea46372018-09-03 10:15:13 +0200213 operator bool() const;
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200214
215 protected:
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200216 bool Parse(absl::optional<std::string> str_value) override;
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200217
218 private:
219 bool value_;
220};
221
222// Accepts true, false, else parsed with sscanf %i, true if != 0.
223extern template class FieldTrialParameter<bool>;
224// Interpreted using sscanf %lf.
225extern template class FieldTrialParameter<double>;
226// Interpreted using sscanf %i.
227extern template class FieldTrialParameter<int>;
228// Using the given value as is.
229extern template class FieldTrialParameter<std::string>;
230
Sebastian Janssonb22f0772018-11-19 17:44:33 +0100231extern template class FieldTrialConstrained<double>;
232extern template class FieldTrialConstrained<int>;
233
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200234extern template class FieldTrialOptional<double>;
235extern template class FieldTrialOptional<int>;
236extern template class FieldTrialOptional<bool>;
237extern template class FieldTrialOptional<std::string>;
238
239} // namespace webrtc
240
241#endif // RTC_BASE_EXPERIMENTS_FIELD_TRIAL_PARSER_H_