blob: 22a88897170bc55072fd8d1e31a0dd7f037d8fd1 [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
58// with extrated values if available.
59void 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 Jansson2c74d852018-06-26 10:30:16 +020095class AbstractFieldTrialEnum : public FieldTrialParameterInterface {
96 public:
97 AbstractFieldTrialEnum(std::string key,
98 int default_value,
99 std::map<std::string, int> mapping);
100 ~AbstractFieldTrialEnum() override;
101 AbstractFieldTrialEnum(const AbstractFieldTrialEnum&);
102
103 protected:
104 bool Parse(absl::optional<std::string> str_value) override;
105
106 protected:
107 int value_;
108 std::map<std::string, int> enum_mapping_;
109 std::set<int> valid_values_;
110};
111
112// The FieldTrialEnum class can be used to quickly define a parser for a
113// specific enum. It handles values provided as integers and as strings if a
114// mapping is provided.
115template <typename T>
116class FieldTrialEnum : public AbstractFieldTrialEnum {
117 public:
118 FieldTrialEnum(std::string key,
119 T default_value,
120 std::map<std::string, T> mapping)
121 : AbstractFieldTrialEnum(key,
122 static_cast<int>(default_value),
123 ToIntMap(mapping)) {}
124 T Get() const { return static_cast<T>(value_); }
125 operator T() const { return Get(); }
126
127 private:
128 static std::map<std::string, int> ToIntMap(std::map<std::string, T> mapping) {
129 std::map<std::string, int> res;
130 for (const auto& it : mapping)
131 res[it.first] = static_cast<int>(it.second);
132 return res;
133 }
134};
135
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200136// This class uses the ParseTypedParameter function to implement an optional
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200137// parameter implementation that can default to absl::nullopt.
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200138template <typename T>
139class FieldTrialOptional : public FieldTrialParameterInterface {
140 public:
141 explicit FieldTrialOptional(std::string key)
142 : FieldTrialParameterInterface(key) {}
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200143 FieldTrialOptional(std::string key, absl::optional<T> default_value)
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200144 : FieldTrialParameterInterface(key), value_(default_value) {}
Sebastian Janssonfea46372018-09-03 10:15:13 +0200145 absl::optional<T> GetOptional() const { return value_; }
146 const T& Value() const { return value_.value(); }
147 const T& operator*() const { return value_.value(); }
148 const T* operator->() const { return &value_.value(); }
149 operator bool() const { return value_.has_value(); }
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200150
151 protected:
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200152 bool Parse(absl::optional<std::string> str_value) override {
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200153 if (str_value) {
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200154 absl::optional<T> value = ParseTypedParameter<T>(*str_value);
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200155 if (!value.has_value())
156 return false;
157 value_ = value.value();
158 } else {
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200159 value_ = absl::nullopt;
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200160 }
161 return true;
162 }
163
164 private:
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200165 absl::optional<T> value_;
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200166};
167
168// Equivalent to a FieldTrialParameter<bool> in the case that both key and value
169// are present. If key is missing, evaluates to false. If key is present, but no
170// explicit value is provided, the flag evaluates to true.
171class FieldTrialFlag : public FieldTrialParameterInterface {
172 public:
173 explicit FieldTrialFlag(std::string key);
174 FieldTrialFlag(std::string key, bool default_value);
175 bool Get() const;
Sebastian Janssonfea46372018-09-03 10:15:13 +0200176 operator bool() const;
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200177
178 protected:
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200179 bool Parse(absl::optional<std::string> str_value) override;
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200180
181 private:
182 bool value_;
183};
184
185// Accepts true, false, else parsed with sscanf %i, true if != 0.
186extern template class FieldTrialParameter<bool>;
187// Interpreted using sscanf %lf.
188extern template class FieldTrialParameter<double>;
189// Interpreted using sscanf %i.
190extern template class FieldTrialParameter<int>;
191// Using the given value as is.
192extern template class FieldTrialParameter<std::string>;
193
194extern template class FieldTrialOptional<double>;
195extern template class FieldTrialOptional<int>;
196extern template class FieldTrialOptional<bool>;
197extern template class FieldTrialOptional<std::string>;
198
199} // namespace webrtc
200
201#endif // RTC_BASE_EXPERIMENTS_FIELD_TRIAL_PARSER_H_