• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
12 #include <algorithm>
13 
14 #include "rtc_base/logging.h"
15 
16 namespace webrtc {
17 namespace {
FindOrEnd(absl::string_view str,size_t start,char delimiter)18 size_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 
25 namespace struct_parser_impl {
26 namespace {
StringEncode(std::string * target,bool val)27 inline void StringEncode(std::string* target, bool val) {
28   *target += rtc::ToString(val);
29 }
StringEncode(std::string * target,double val)30 inline void StringEncode(std::string* target, double val) {
31   *target += rtc::ToString(val);
32 }
StringEncode(std::string * target,int val)33 inline void StringEncode(std::string* target, int val) {
34   *target += rtc::ToString(val);
35 }
StringEncode(std::string * target,unsigned val)36 inline void StringEncode(std::string* target, unsigned val) {
37   *target += rtc::ToString(val);
38 }
StringEncode(std::string * target,DataRate val)39 inline void StringEncode(std::string* target, DataRate val) {
40   *target += webrtc::ToString(val);
41 }
StringEncode(std::string * target,DataSize val)42 inline void StringEncode(std::string* target, DataSize val) {
43   *target += webrtc::ToString(val);
44 }
StringEncode(std::string * target,TimeDelta val)45 inline void StringEncode(std::string* target, TimeDelta val) {
46   *target += webrtc::ToString(val);
47 }
48 
49 template <typename T>
StringEncode(std::string * sb,absl::optional<T> val)50 inline void StringEncode(std::string* sb, absl::optional<T> val) {
51   if (val)
52     StringEncode(sb, *val);
53 }
54 }  // namespace
55 template <typename T>
Parse(absl::string_view src,void * target)56 bool 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 }
62 template <typename T>
Encode(const void * src,std::string * target)63 void TypedParser<T>::Encode(const void* src, std::string* target) {
64   StringEncode(target, *reinterpret_cast<const T*>(src));
65 }
66 
67 template class TypedParser<bool>;
68 template class TypedParser<double>;
69 template class TypedParser<int>;
70 template class TypedParser<unsigned>;
71 template class TypedParser<absl::optional<double>>;
72 template class TypedParser<absl::optional<int>>;
73 template class TypedParser<absl::optional<unsigned>>;
74 
75 template class TypedParser<DataRate>;
76 template class TypedParser<DataSize>;
77 template class TypedParser<TimeDelta>;
78 template class TypedParser<absl::optional<DataRate>>;
79 template class TypedParser<absl::optional<DataSize>>;
80 template class TypedParser<absl::optional<TimeDelta>>;
81 }  // namespace struct_parser_impl
82 
StructParametersParser(std::vector<struct_parser_impl::MemberParameter> members)83 StructParametersParser::StructParametersParser(
84     std::vector<struct_parser_impl::MemberParameter> members)
85     : members_(std::move(members)) {}
86 
Parse(absl::string_view src)87 void StructParametersParser::Parse(absl::string_view src) {
88   size_t i = 0;
89   while (i < src.length()) {
90     size_t val_end = FindOrEnd(src, i, ',');
91     size_t colon_pos = FindOrEnd(src, i, ':');
92     size_t key_end = std::min(val_end, colon_pos);
93     size_t val_begin = key_end + 1u;
94     absl::string_view key(src.substr(i, key_end - i));
95     absl::string_view opt_value;
96     if (val_end >= val_begin)
97       opt_value = src.substr(val_begin, val_end - val_begin);
98     i = val_end + 1u;
99     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;
108       }
109     }
110     if (!found) {
111       RTC_LOG(LS_INFO) << "No field with key: '" << key
112                        << "' (found in trial: \"" << src << "\")";
113     }
114   }
115 }
116 
Encode() const117 std::string StructParametersParser::Encode() const {
118   std::string res;
119   bool first = true;
120   for (const auto& member : members_) {
121     if (!first)
122       res += ",";
123     res += member.key;
124     res += ":";
125     member.parser.encode(member.member_ptr, &res);
126     first = false;
127   }
128   return res;
129 }
130 
131 }  // namespace webrtc
132