Adds struct parameters parser/encoder.

This is similar to the field trial parser but it uses a normal struct
with normal fields as underlying storage. This makes it easier to
understand and use as only the encoding and parsing uses non-
standard constructs. Additionally, it makes it easier to use the
struct as a regular config struct when the values are not set
using field trials.

Bug: webrtc:9883
Change-Id: I5b16c2a71875b6f478383decff18fbaa62bc404a
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/145203
Reviewed-by: Jonas Olsson <jonasolsson@webrtc.org>
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28810}
This commit is contained in:
Sebastian Jansson
2019-08-08 11:14:51 +02:00
committed by Commit Bot
parent 940c2b5005
commit 55251c3d49
8 changed files with 437 additions and 0 deletions

View File

@ -47,6 +47,8 @@ rtc_static_library("field_trial_parser") {
"field_trial_parser.h",
"field_trial_units.cc",
"field_trial_units.h",
"struct_parameters_parser.cc",
"struct_parameters_parser.h",
]
deps = [
"../../api/units:data_rate",
@ -55,6 +57,8 @@ rtc_static_library("field_trial_parser") {
"../../rtc_base:checks",
"../../rtc_base:logging",
"../../rtc_base:stringutils",
"//third_party/abseil-cpp/absl/memory:memory",
"//third_party/abseil-cpp/absl/strings:strings",
"//third_party/abseil-cpp/absl/types:optional",
]
}
@ -208,6 +212,7 @@ if (rtc_include_tests) {
"quality_scaling_experiment_unittest.cc",
"rate_control_settings_unittest.cc",
"rtt_mult_experiment_unittest.cc",
"struct_parameters_parser_unittest.cc",
]
deps = [
":balanced_degradation_settings",

View File

@ -129,6 +129,22 @@ absl::optional<std::string> ParseTypedParameter<std::string>(std::string str) {
return std::move(str);
}
template <>
absl::optional<absl::optional<bool>> ParseTypedParameter<absl::optional<bool>>(
std::string str) {
return ParseOptionalParameter<bool>(str);
}
template <>
absl::optional<absl::optional<int>> ParseTypedParameter<absl::optional<int>>(
std::string str) {
return ParseOptionalParameter<int>(str);
}
template <>
absl::optional<absl::optional<double>>
ParseTypedParameter<absl::optional<double>>(std::string str) {
return ParseOptionalParameter<double>(str);
}
FieldTrialFlag::FieldTrialFlag(std::string key) : FieldTrialFlag(key, false) {}
FieldTrialFlag::FieldTrialFlag(std::string key, bool default_value)

View File

@ -228,6 +228,33 @@ class FieldTrialFlag : public FieldTrialParameterInterface {
bool value_;
};
template <typename T>
absl::optional<absl::optional<T>> ParseOptionalParameter(std::string str) {
if (str.empty())
return absl::optional<T>();
auto parsed = ParseTypedParameter<T>(str);
if (parsed.has_value())
return parsed;
return absl::nullopt;
}
template <>
absl::optional<bool> ParseTypedParameter<bool>(std::string str);
template <>
absl::optional<double> ParseTypedParameter<double>(std::string str);
template <>
absl::optional<std::string> ParseTypedParameter<std::string>(std::string str);
template <>
absl::optional<absl::optional<bool>> ParseTypedParameter<absl::optional<bool>>(
std::string str);
template <>
absl::optional<absl::optional<int>> ParseTypedParameter<absl::optional<int>>(
std::string str);
template <>
absl::optional<absl::optional<double>>
ParseTypedParameter<absl::optional<double>>(std::string str);
// Accepts true, false, else parsed with sscanf %i, true if != 0.
extern template class FieldTrialParameter<bool>;
// Interpreted using sscanf %lf.

View File

@ -84,6 +84,22 @@ absl::optional<TimeDelta> ParseTypedParameter<TimeDelta>(std::string str) {
return absl::nullopt;
}
template <>
absl::optional<absl::optional<DataRate>>
ParseTypedParameter<absl::optional<DataRate>>(std::string str) {
return ParseOptionalParameter<DataRate>(str);
}
template <>
absl::optional<absl::optional<DataSize>>
ParseTypedParameter<absl::optional<DataSize>>(std::string str) {
return ParseOptionalParameter<DataSize>(str);
}
template <>
absl::optional<absl::optional<TimeDelta>>
ParseTypedParameter<absl::optional<TimeDelta>>(std::string str) {
return ParseOptionalParameter<TimeDelta>(str);
}
template class FieldTrialParameter<DataRate>;
template class FieldTrialParameter<DataSize>;
template class FieldTrialParameter<TimeDelta>;

View File

@ -16,6 +16,14 @@
#include "rtc_base/experiments/field_trial_parser.h"
namespace webrtc {
template <>
absl::optional<DataRate> ParseTypedParameter<DataRate>(std::string str);
template <>
absl::optional<DataSize> ParseTypedParameter<DataSize>(std::string str);
template <>
absl::optional<TimeDelta> ParseTypedParameter<TimeDelta>(std::string str);
extern template class FieldTrialParameter<DataRate>;
extern template class FieldTrialParameter<DataSize>;
extern template class FieldTrialParameter<TimeDelta>;

View File

@ -0,0 +1,67 @@
/*
* Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/experiments/struct_parameters_parser.h"
#include <algorithm>
#include "rtc_base/logging.h"
#include "rtc_base/strings/string_builder.h"
namespace webrtc {
namespace struct_parser_impl {
namespace {
size_t FindOrEnd(absl::string_view str, size_t start, char delimiter) {
size_t pos = str.find(delimiter, start);
pos = (pos == std::string::npos) ? str.length() : pos;
return pos;
}
} // namespace
void ParseConfigParams(
absl::string_view config_str,
std::map<std::string, std::function<bool(absl::string_view)>> field_map) {
size_t i = 0;
while (i < config_str.length()) {
size_t val_end = FindOrEnd(config_str, i, ',');
size_t colon_pos = FindOrEnd(config_str, i, ':');
size_t key_end = std::min(val_end, colon_pos);
size_t val_begin = key_end + 1u;
std::string key(config_str.substr(i, key_end - i));
absl::string_view opt_value;
if (val_end >= val_begin)
opt_value = config_str.substr(val_begin, val_end - val_begin);
i = val_end + 1u;
auto field = field_map.find(key);
if (field != field_map.end()) {
if (!field->second(opt_value)) {
RTC_LOG(LS_WARNING) << "Failed to read field with key: '" << key
<< "' in trial: \"" << config_str << "\"";
}
} else {
RTC_LOG(LS_INFO) << "No field with key: '" << key
<< "' (found in trial: \"" << config_str << "\")";
}
}
}
std::string EncodeStringStringMap(std::map<std::string, std::string> mapping) {
rtc::StringBuilder sb;
bool first = true;
for (const auto& kv : mapping) {
if (!first)
sb << ",";
sb << kv.first << ":" << kv.second;
first = false;
}
return sb.Release();
}
} // namespace struct_parser_impl
} // namespace webrtc

View File

@ -0,0 +1,214 @@
/*
* Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef RTC_BASE_EXPERIMENTS_STRUCT_PARAMETERS_PARSER_H_
#define RTC_BASE_EXPERIMENTS_STRUCT_PARAMETERS_PARSER_H_
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/memory/memory.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "rtc_base/experiments/field_trial_parser.h"
#include "rtc_base/experiments/field_trial_units.h"
#include "rtc_base/string_encode.h"
namespace webrtc {
namespace struct_parser_impl {
inline std::string StringEncode(bool val) {
return rtc::ToString(val);
}
inline std::string StringEncode(double val) {
return rtc::ToString(val);
}
inline std::string StringEncode(int val) {
return rtc::ToString(val);
}
inline std::string StringEncode(std::string val) {
return val;
}
inline std::string StringEncode(DataRate val) {
return ToString(val);
}
inline std::string StringEncode(DataSize val) {
return ToString(val);
}
inline std::string StringEncode(TimeDelta val) {
return ToString(val);
}
template <typename T>
inline std::string StringEncode(absl::optional<T> val) {
if (val)
return StringEncode(*val);
return "";
}
template <typename T>
struct LambdaTraits : public LambdaTraits<decltype(&T::operator())> {};
template <typename ClassType, typename RetType, typename SourceType>
struct LambdaTraits<RetType* (ClassType::*)(SourceType*)const> {
using ret = RetType;
using src = SourceType;
};
void ParseConfigParams(
absl::string_view config_str,
std::map<std::string, std::function<bool(absl::string_view)>> field_map);
std::string EncodeStringStringMap(std::map<std::string, std::string> mapping);
template <typename StructType>
class StructParameterParser {
public:
virtual bool Parse(absl::string_view src, StructType* target) const = 0;
virtual bool Changed(const StructType& src, const StructType& base) const = 0;
virtual std::string Encode(const StructType& src) const = 0;
virtual ~StructParameterParser() = default;
};
template <typename StructType, typename T>
class StructParameterImpl : public StructParameterParser<StructType> {
public:
explicit StructParameterImpl(std::function<T*(StructType*)> field_getter)
: field_getter_(std::move(field_getter)) {}
bool Parse(absl::string_view src, StructType* target) const override {
auto parsed = ParseTypedParameter<T>(std::string(src));
if (parsed.has_value())
*field_getter_(target) = *parsed;
return parsed.has_value();
}
bool Changed(const StructType& src, const StructType& base) const override {
T base_value = *field_getter_(const_cast<StructType*>(&base));
T value = *field_getter_(const_cast<StructType*>(&src));
return value != base_value;
}
std::string Encode(const StructType& src) const override {
T value = *field_getter_(const_cast<StructType*>(&src));
return struct_parser_impl::StringEncode(value);
}
private:
const std::function<T*(StructType*)> field_getter_;
};
template <typename StructType>
struct StructParameter {
std::string key;
StructParameterParser<StructType>* parser;
};
template <typename S,
typename Closure,
typename T = typename LambdaTraits<Closure>::ret>
void AddParameters(std::vector<StructParameter<S>>* out,
std::string key,
Closure getter) {
auto* parser = new StructParameterImpl<S, T>(getter);
out->push_back(StructParameter<S>{std::move(key), parser});
}
template <typename S,
typename Closure,
typename T = typename LambdaTraits<Closure>::ret,
typename... Args>
void AddParameters(std::vector<StructParameter<S>>* out,
std::string key,
Closure getter,
Args... args) {
AddParameters(out, key, getter);
AddParameters<S>(out, args...);
}
} // namespace struct_parser_impl
template <typename StructType>
class StructParametersParser {
public:
~StructParametersParser() {
for (auto& param : parameters_) {
delete param.parser;
}
}
void Parse(StructType* target, absl::string_view src) {
std::map<std::string, std::function<bool(absl::string_view)>> field_parsers;
for (const auto& param : parameters_) {
field_parsers.emplace(param.key, [target, param](absl::string_view src) {
return param.parser->Parse(src, target);
});
}
struct_parser_impl::ParseConfigParams(src, std::move(field_parsers));
}
StructType Parse(absl::string_view src) {
StructType res;
Parse(&res, src);
return res;
}
std::string EncodeChanged(const StructType& src) {
static StructType base;
std::map<std::string, std::string> pairs;
for (const auto& param : parameters_) {
if (param.parser->Changed(src, base))
pairs[param.key] = param.parser->Encode(src);
}
return struct_parser_impl::EncodeStringStringMap(pairs);
}
std::string EncodeAll(const StructType& src) {
std::map<std::string, std::string> pairs;
for (const auto& param : parameters_) {
pairs[param.key] = param.parser->Encode(src);
}
return struct_parser_impl::EncodeStringStringMap(pairs);
}
private:
template <typename C, typename S, typename... Args>
friend std::unique_ptr<StructParametersParser<S>>
CreateStructParametersParser(std::string, C, Args...);
explicit StructParametersParser(
std::vector<struct_parser_impl::StructParameter<StructType>> parameters)
: parameters_(parameters) {}
std::vector<struct_parser_impl::StructParameter<StructType>> parameters_;
};
// Creates a struct parameters parser based on interleaved key and field
// accessor arguments, where the field accessor converts a struct pointer to a
// member pointer: FieldType*(StructType*). See the unit tests for example
// usage. Note that the struct type is inferred from the field getters. Beware
// of providing incorrect arguments to this, such as mixing the struct type or
// incorrect return values, as this will cause very confusing compile errors.
template <typename Closure,
typename S = typename struct_parser_impl::LambdaTraits<Closure>::src,
typename... Args>
std::unique_ptr<StructParametersParser<S>> CreateStructParametersParser(
std::string first_key,
Closure first_getter,
Args... args) {
std::vector<struct_parser_impl::StructParameter<S>> parameters;
struct_parser_impl::AddParameters<S>(&parameters, std::move(first_key),
first_getter, args...);
// absl::make_unique can't be used since the StructParametersParser
// constructor is only visible to this create function.
return absl::WrapUnique(new StructParametersParser<S>(std::move(parameters)));
}
} // namespace webrtc
#endif // RTC_BASE_EXPERIMENTS_STRUCT_PARAMETERS_PARSER_H_

View File

@ -0,0 +1,84 @@
/*
* Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/experiments/struct_parameters_parser.h"
#include "rtc_base/gunit.h"
namespace webrtc {
namespace {
struct DummyConfig {
bool enabled = false;
double factor = 0.5;
int retries = 5;
bool ping = 0;
std::string hash = "a80";
absl::optional<TimeDelta> duration;
absl::optional<TimeDelta> latency = TimeDelta::ms(100);
static StructParametersParser<DummyConfig>* Parser();
};
StructParametersParser<DummyConfig>* DummyConfig::Parser() {
using C = DummyConfig;
// The empty comments ensures that each pair is on a separate line.
static auto parser = CreateStructParametersParser(
"e", [](C* c) { return &c->enabled; }, //
"f", [](C* c) { return &c->factor; }, //
"r", [](C* c) { return &c->retries; }, //
"p", [](C* c) { return &c->ping; }, //
"h", [](C* c) { return &c->hash; }, //
"d", [](C* c) { return &c->duration; }, //
"l", [](C* c) { return &c->latency; }); //
return parser.get();
}
} // namespace
TEST(StructParametersParserTest, ParsesValidParameters) {
DummyConfig exp =
DummyConfig::Parser()->Parse("e:1,f:-1.7,r:2,p:1,h:x7c,d:8,l:,");
EXPECT_TRUE(exp.enabled);
EXPECT_EQ(exp.factor, -1.7);
EXPECT_EQ(exp.retries, 2);
EXPECT_EQ(exp.ping, true);
EXPECT_EQ(exp.duration.value().ms(), 8);
EXPECT_FALSE(exp.latency);
}
TEST(StructParametersParserTest, UsesDefaults) {
DummyConfig exp = DummyConfig::Parser()->Parse("");
EXPECT_FALSE(exp.enabled);
EXPECT_EQ(exp.factor, 0.5);
EXPECT_EQ(exp.retries, 5);
EXPECT_EQ(exp.ping, false);
EXPECT_EQ(exp.hash, "a80");
}
TEST(StructParametersParserTest, EmptyDefaults) {
DummyConfig exp;
auto encoded = DummyConfig::Parser()->EncodeChanged(exp);
// Unchanged parameters are not encoded.
EXPECT_EQ(encoded, "");
}
TEST(StructParametersParserTest, EncodeAll) {
DummyConfig exp;
auto encoded = DummyConfig::Parser()->EncodeAll(exp);
// All parameters are encoded.
EXPECT_EQ(encoded, "d:,e:false,f:0.5,h:a80,l:100 ms,p:false,r:5");
}
TEST(StructParametersParserTest, EncodeChanged) {
DummyConfig exp;
exp.ping = true;
exp.retries = 4;
auto encoded = DummyConfig::Parser()->EncodeChanged(exp);
// We expect the changed parameters to be encoded in alphabetical order.
EXPECT_EQ(encoded, "p:true,r:4");
}
} // namespace webrtc