blob: 2605da8fef6184ea61ea88895c7faab0b2921b5b [file] [log] [blame]
Sebastian Jansson55251c32019-08-08 11:14:51 +02001/*
2 * Copyright (c) 2019 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#include "rtc_base/experiments/struct_parameters_parser.h"
11
Bjorn Terelius9f00f0e2019-08-30 09:39:31 +020012#include <algorithm>
13
Sebastian Jansson55251c32019-08-08 11:14:51 +020014#include "rtc_base/logging.h"
Sebastian Jansson55251c32019-08-08 11:14:51 +020015
16namespace webrtc {
Sebastian Jansson55251c32019-08-08 11:14:51 +020017namespace {
18size_t FindOrEnd(absl::string_view str, size_t start, char delimiter) {
19 size_t pos = str.find(delimiter, start);
20 pos = (pos == std::string::npos) ? str.length() : pos;
21 return pos;
22}
23} // namespace
24
Sebastian Jansson0ee80082019-08-14 13:16:26 +020025namespace struct_parser_impl {
26namespace {
27inline void StringEncode(std::string* target, bool val) {
28 *target += rtc::ToString(val);
29}
30inline void StringEncode(std::string* target, double val) {
31 *target += rtc::ToString(val);
32}
33inline void StringEncode(std::string* target, int val) {
34 *target += rtc::ToString(val);
35}
Bjorn Terelius9f00f0e2019-08-30 09:39:31 +020036inline void StringEncode(std::string* target, unsigned val) {
37 *target += rtc::ToString(val);
38}
Sebastian Jansson0ee80082019-08-14 13:16:26 +020039inline void StringEncode(std::string* target, DataRate val) {
40 *target += webrtc::ToString(val);
41}
42inline void StringEncode(std::string* target, DataSize val) {
43 *target += webrtc::ToString(val);
44}
45inline void StringEncode(std::string* target, TimeDelta val) {
46 *target += webrtc::ToString(val);
47}
48
49template <typename T>
50inline void StringEncode(std::string* sb, absl::optional<T> val) {
51 if (val)
52 StringEncode(sb, *val);
53}
54} // namespace
55template <typename T>
56bool TypedParser<T>::Parse(absl::string_view src, void* target) {
57 auto parsed = ParseTypedParameter<T>(std::string(src));
58 if (parsed.has_value())
59 *reinterpret_cast<T*>(target) = *parsed;
60 return parsed.has_value();
61}
62template <typename T>
63void TypedParser<T>::Encode(const void* src, std::string* target) {
64 StringEncode(target, *reinterpret_cast<const T*>(src));
65}
66
67template class TypedParser<bool>;
68template class TypedParser<double>;
69template class TypedParser<int>;
Bjorn Terelius9f00f0e2019-08-30 09:39:31 +020070template class TypedParser<unsigned>;
Sebastian Jansson0ee80082019-08-14 13:16:26 +020071template class TypedParser<absl::optional<double>>;
72template class TypedParser<absl::optional<int>>;
Bjorn Terelius9f00f0e2019-08-30 09:39:31 +020073template class TypedParser<absl::optional<unsigned>>;
Sebastian Jansson0ee80082019-08-14 13:16:26 +020074
75template class TypedParser<DataRate>;
76template class TypedParser<DataSize>;
77template class TypedParser<TimeDelta>;
78template class TypedParser<absl::optional<DataRate>>;
79template class TypedParser<absl::optional<DataSize>>;
80template class TypedParser<absl::optional<TimeDelta>>;
81} // namespace struct_parser_impl
82
83StructParametersParser::StructParametersParser(
84 std::vector<struct_parser_impl::MemberParameter> members)
85 : members_(std::move(members)) {}
86
87void StructParametersParser::Parse(absl::string_view src) {
Sebastian Jansson55251c32019-08-08 11:14:51 +020088 size_t i = 0;
Sebastian Jansson0ee80082019-08-14 13:16:26 +020089 while (i < src.length()) {
90 size_t val_end = FindOrEnd(src, i, ',');
91 size_t colon_pos = FindOrEnd(src, i, ':');
Sebastian Jansson55251c32019-08-08 11:14:51 +020092 size_t key_end = std::min(val_end, colon_pos);
93 size_t val_begin = key_end + 1u;
Sebastian Jansson0ee80082019-08-14 13:16:26 +020094 absl::string_view key(src.substr(i, key_end - i));
Sebastian Jansson55251c32019-08-08 11:14:51 +020095 absl::string_view opt_value;
96 if (val_end >= val_begin)
Sebastian Jansson0ee80082019-08-14 13:16:26 +020097 opt_value = src.substr(val_begin, val_end - val_begin);
Sebastian Jansson55251c32019-08-08 11:14:51 +020098 i = val_end + 1u;
Sebastian Jansson0ee80082019-08-14 13:16:26 +020099 bool found = false;
100 for (auto& member : members_) {
101 if (key == member.key) {
102 found = true;
103 if (!member.parser.parse(opt_value, member.member_ptr)) {
104 RTC_LOG(LS_WARNING) << "Failed to read field with key: '" << key
105 << "' in trial: \"" << src << "\"";
106 }
107 break;
Sebastian Jansson55251c32019-08-08 11:14:51 +0200108 }
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200109 }
110 if (!found) {
Sebastian Jansson55251c32019-08-08 11:14:51 +0200111 RTC_LOG(LS_INFO) << "No field with key: '" << key
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200112 << "' (found in trial: \"" << src << "\")";
Sebastian Jansson55251c32019-08-08 11:14:51 +0200113 }
114 }
115}
116
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200117std::string StructParametersParser::Encode() const {
118 std::string res;
Sebastian Jansson55251c32019-08-08 11:14:51 +0200119 bool first = true;
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200120 for (const auto& member : members_) {
Sebastian Jansson55251c32019-08-08 11:14:51 +0200121 if (!first)
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200122 res += ",";
123 res += member.key;
124 res += ":";
125 member.parser.encode(member.member_ptr, &res);
Sebastian Jansson55251c32019-08-08 11:14:51 +0200126 first = false;
127 }
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200128 return res;
Sebastian Jansson55251c32019-08-08 11:14:51 +0200129}
Sebastian Jansson55251c32019-08-08 11:14:51 +0200130
131} // namespace webrtc