blob: c743bb0145bc84fdf761c28c18304806292aee00 [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:
40 explicit FieldTrialParameterInterface(std::string key);
41 friend void ParseFieldTrial(
42 std::initializer_list<FieldTrialParameterInterface*> fields,
43 std::string raw_string);
Sebastian Janssonfea46372018-09-03 10:15:13 +020044 void MarkAsUsed() { used_ = true; }
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020045 virtual bool Parse(absl::optional<std::string> str_value) = 0;
Sebastian Jansson9eb38862018-06-14 16:47:42 +020046 std::string Key() const;
47
48 private:
49 const std::string key_;
Sebastian Janssonfea46372018-09-03 10:15:13 +020050 bool used_ = false;
Sebastian Jansson9eb38862018-06-14 16:47:42 +020051};
52
53// ParseFieldTrial function parses the given string and fills the given fields
54// with extrated values if available.
55void ParseFieldTrial(
56 std::initializer_list<FieldTrialParameterInterface*> fields,
57 std::string raw_string);
58
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020059// Specialize this in code file for custom types. Should return absl::nullopt if
Sebastian Jansson9eb38862018-06-14 16:47:42 +020060// the given string cannot be properly parsed.
61template <typename T>
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020062absl::optional<T> ParseTypedParameter(std::string);
Sebastian Jansson9eb38862018-06-14 16:47:42 +020063
64// This class uses the ParseTypedParameter function to implement a parameter
65// implementation with an enforced default value.
66template <typename T>
67class FieldTrialParameter : public FieldTrialParameterInterface {
68 public:
69 FieldTrialParameter(std::string key, T default_value)
70 : FieldTrialParameterInterface(key), value_(default_value) {}
71 T Get() const { return value_; }
72 operator T() const { return Get(); }
Sebastian Janssonfea46372018-09-03 10:15:13 +020073 const T* operator->() const { return &value_; }
Sebastian Jansson9eb38862018-06-14 16:47:42 +020074
75 protected:
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020076 bool Parse(absl::optional<std::string> str_value) override {
Sebastian Jansson9eb38862018-06-14 16:47:42 +020077 if (str_value) {
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020078 absl::optional<T> value = ParseTypedParameter<T>(*str_value);
Sebastian Jansson9eb38862018-06-14 16:47:42 +020079 if (value.has_value()) {
80 value_ = value.value();
81 return true;
82 }
83 }
84 return false;
85 }
86
87 private:
88 T value_;
89};
90
Sebastian Jansson2c74d852018-06-26 10:30:16 +020091class AbstractFieldTrialEnum : public FieldTrialParameterInterface {
92 public:
93 AbstractFieldTrialEnum(std::string key,
94 int default_value,
95 std::map<std::string, int> mapping);
96 ~AbstractFieldTrialEnum() override;
97 AbstractFieldTrialEnum(const AbstractFieldTrialEnum&);
98
99 protected:
100 bool Parse(absl::optional<std::string> str_value) override;
101
102 protected:
103 int value_;
104 std::map<std::string, int> enum_mapping_;
105 std::set<int> valid_values_;
106};
107
108// The FieldTrialEnum class can be used to quickly define a parser for a
109// specific enum. It handles values provided as integers and as strings if a
110// mapping is provided.
111template <typename T>
112class FieldTrialEnum : public AbstractFieldTrialEnum {
113 public:
114 FieldTrialEnum(std::string key,
115 T default_value,
116 std::map<std::string, T> mapping)
117 : AbstractFieldTrialEnum(key,
118 static_cast<int>(default_value),
119 ToIntMap(mapping)) {}
120 T Get() const { return static_cast<T>(value_); }
121 operator T() const { return Get(); }
122
123 private:
124 static std::map<std::string, int> ToIntMap(std::map<std::string, T> mapping) {
125 std::map<std::string, int> res;
126 for (const auto& it : mapping)
127 res[it.first] = static_cast<int>(it.second);
128 return res;
129 }
130};
131
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200132// This class uses the ParseTypedParameter function to implement an optional
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200133// parameter implementation that can default to absl::nullopt.
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200134template <typename T>
135class FieldTrialOptional : public FieldTrialParameterInterface {
136 public:
137 explicit FieldTrialOptional(std::string key)
138 : FieldTrialParameterInterface(key) {}
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200139 FieldTrialOptional(std::string key, absl::optional<T> default_value)
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200140 : FieldTrialParameterInterface(key), value_(default_value) {}
Sebastian Janssonfea46372018-09-03 10:15:13 +0200141 absl::optional<T> GetOptional() const { return value_; }
142 const T& Value() const { return value_.value(); }
143 const T& operator*() const { return value_.value(); }
144 const T* operator->() const { return &value_.value(); }
145 operator bool() const { return value_.has_value(); }
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200146
147 protected:
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200148 bool Parse(absl::optional<std::string> str_value) override {
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200149 if (str_value) {
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200150 absl::optional<T> value = ParseTypedParameter<T>(*str_value);
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200151 if (!value.has_value())
152 return false;
153 value_ = value.value();
154 } else {
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200155 value_ = absl::nullopt;
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200156 }
157 return true;
158 }
159
160 private:
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200161 absl::optional<T> value_;
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200162};
163
164// Equivalent to a FieldTrialParameter<bool> in the case that both key and value
165// are present. If key is missing, evaluates to false. If key is present, but no
166// explicit value is provided, the flag evaluates to true.
167class FieldTrialFlag : public FieldTrialParameterInterface {
168 public:
169 explicit FieldTrialFlag(std::string key);
170 FieldTrialFlag(std::string key, bool default_value);
171 bool Get() const;
Sebastian Janssonfea46372018-09-03 10:15:13 +0200172 operator bool() const;
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200173
174 protected:
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200175 bool Parse(absl::optional<std::string> str_value) override;
Sebastian Jansson9eb38862018-06-14 16:47:42 +0200176
177 private:
178 bool value_;
179};
180
181// Accepts true, false, else parsed with sscanf %i, true if != 0.
182extern template class FieldTrialParameter<bool>;
183// Interpreted using sscanf %lf.
184extern template class FieldTrialParameter<double>;
185// Interpreted using sscanf %i.
186extern template class FieldTrialParameter<int>;
187// Using the given value as is.
188extern template class FieldTrialParameter<std::string>;
189
190extern template class FieldTrialOptional<double>;
191extern template class FieldTrialOptional<int>;
192extern template class FieldTrialOptional<bool>;
193extern template class FieldTrialOptional<std::string>;
194
195} // namespace webrtc
196
197#endif // RTC_BASE_EXPERIMENTS_FIELD_TRIAL_PARSER_H_