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 #ifndef RTC_BASE_EXPERIMENTS_STRUCT_PARAMETERS_PARSER_H_
11 #define RTC_BASE_EXPERIMENTS_STRUCT_PARAMETERS_PARSER_H_
12 
13 #include <functional>
14 #include <map>
15 #include <memory>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 #include "absl/memory/memory.h"
21 #include "absl/strings/string_view.h"
22 #include "absl/types/optional.h"
23 #include "rtc_base/experiments/field_trial_parser.h"
24 #include "rtc_base/experiments/field_trial_units.h"
25 #include "rtc_base/string_encode.h"
26 
27 namespace webrtc {
28 namespace struct_parser_impl {
29 struct TypedMemberParser {
30  public:
31   bool (*parse)(const absl::string_view src, void* target);
32   void (*encode)(const void* src, std::string* target);
33 };
34 
35 struct MemberParameter {
36   const char* key;
37   void* member_ptr;
38   TypedMemberParser parser;
39 };
40 
41 template <typename T>
42 class TypedParser {
43  public:
44   static bool Parse(absl::string_view src, void* target);
45   static void Encode(const void* src, std::string* target);
46 };
47 
48 // Instantiated in cc file to avoid duplication during compile. Add additional
49 // parsers as needed. Generally, try to use these suggested types even if the
50 // context where the value is used might require a different type. For instance,
51 // a size_t representing a packet size should use an int parameter as there's no
52 // need to support packet sizes larger than INT32_MAX.
53 extern template class TypedParser<bool>;
54 extern template class TypedParser<double>;
55 extern template class TypedParser<int>;
56 extern template class TypedParser<unsigned>;
57 extern template class TypedParser<absl::optional<double>>;
58 extern template class TypedParser<absl::optional<int>>;
59 extern template class TypedParser<absl::optional<unsigned>>;
60 
61 extern template class TypedParser<DataRate>;
62 extern template class TypedParser<DataSize>;
63 extern template class TypedParser<TimeDelta>;
64 extern template class TypedParser<absl::optional<DataRate>>;
65 extern template class TypedParser<absl::optional<DataSize>>;
66 extern template class TypedParser<absl::optional<TimeDelta>>;
67 
68 template <typename T>
AddMembers(MemberParameter * out,const char * key,T * member)69 void AddMembers(MemberParameter* out, const char* key, T* member) {
70   *out = MemberParameter{
71       key, member,
72       TypedMemberParser{&TypedParser<T>::Parse, &TypedParser<T>::Encode}};
73 }
74 
75 template <typename T, typename... Args>
AddMembers(MemberParameter * out,const char * key,T * member,Args...args)76 void AddMembers(MemberParameter* out,
77                 const char* key,
78                 T* member,
79                 Args... args) {
80   AddMembers(out, key, member);
81   AddMembers(++out, args...);
82 }
83 }  // namespace struct_parser_impl
84 
85 class StructParametersParser {
86  public:
87   template <typename T, typename... Args>
Create(const char * first_key,T * first_member,Args...args)88   static std::unique_ptr<StructParametersParser> Create(const char* first_key,
89                                                         T* first_member,
90                                                         Args... args) {
91     std::vector<struct_parser_impl::MemberParameter> members(
92         sizeof...(args) / 2 + 1);
93     struct_parser_impl::AddMembers(&members.front(), std::move(first_key),
94                                    first_member, args...);
95     return absl::WrapUnique(new StructParametersParser(std::move(members)));
96   }
97 
98   void Parse(absl::string_view src);
99   std::string Encode() const;
100 
101  private:
102   explicit StructParametersParser(
103       std::vector<struct_parser_impl::MemberParameter> members);
104 
105   std::vector<struct_parser_impl::MemberParameter> members_;
106 };
107 
108 }  // namespace webrtc
109 
110 #endif  // RTC_BASE_EXPERIMENTS_STRUCT_PARAMETERS_PARSER_H_
111