blob: b40f381594394447fdd60be7fc21d2e9c1ec7e19 [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#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
27namespace webrtc {
28namespace struct_parser_impl {
Sebastian Jansson0ee80082019-08-14 13:16:26 +020029struct TypedMemberParser {
30 public:
31 bool (*parse)(const absl::string_view src, void* target);
32 void (*encode)(const void* src, std::string* target);
33};
34
35struct MemberParameter {
36 const char* key;
37 void* member_ptr;
38 TypedMemberParser parser;
39};
Sebastian Jansson55251c32019-08-08 11:14:51 +020040
41template <typename T>
Sebastian Jansson0ee80082019-08-14 13:16:26 +020042class 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.
53extern template class TypedParser<bool>;
54extern template class TypedParser<double>;
55extern template class TypedParser<int>;
56extern template class TypedParser<absl::optional<double>>;
57extern template class TypedParser<absl::optional<int>>;
58
59extern template class TypedParser<DataRate>;
60extern template class TypedParser<DataSize>;
61extern template class TypedParser<TimeDelta>;
62extern template class TypedParser<absl::optional<DataRate>>;
63extern template class TypedParser<absl::optional<DataSize>>;
64extern template class TypedParser<absl::optional<TimeDelta>>;
Sebastian Jansson55251c32019-08-08 11:14:51 +020065
66template <typename T>
Sebastian Jansson0ee80082019-08-14 13:16:26 +020067void AddMembers(MemberParameter* out, const char* key, T* member) {
68 *out = MemberParameter{
69 key, member,
70 TypedMemberParser{&TypedParser<T>::Parse, &TypedParser<T>::Encode}};
Sebastian Jansson55251c32019-08-08 11:14:51 +020071}
72
Sebastian Jansson0ee80082019-08-14 13:16:26 +020073template <typename T, typename... Args>
74void AddMembers(MemberParameter* out,
75 const char* key,
76 T* member,
77 Args... args) {
78 AddMembers(out, key, member);
79 AddMembers(++out, args...);
Sebastian Jansson55251c32019-08-08 11:14:51 +020080}
Sebastian Jansson55251c32019-08-08 11:14:51 +020081} // namespace struct_parser_impl
82
Sebastian Jansson55251c32019-08-08 11:14:51 +020083class StructParametersParser {
84 public:
Sebastian Jansson0ee80082019-08-14 13:16:26 +020085 template <typename T, typename... Args>
86 static std::unique_ptr<StructParametersParser> Create(const char* first_key,
87 T* first_member,
88 Args... args) {
89 std::vector<struct_parser_impl::MemberParameter> members(
90 sizeof...(args) / 2 + 1);
91 struct_parser_impl::AddMembers(&members.front(), std::move(first_key),
92 first_member, args...);
93 return absl::WrapUnique(new StructParametersParser(std::move(members)));
Sebastian Jansson55251c32019-08-08 11:14:51 +020094 }
95
Sebastian Jansson0ee80082019-08-14 13:16:26 +020096 void Parse(absl::string_view src);
97 std::string Encode() const;
Sebastian Jansson55251c32019-08-08 11:14:51 +020098
99 private:
Sebastian Jansson55251c32019-08-08 11:14:51 +0200100 explicit StructParametersParser(
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200101 std::vector<struct_parser_impl::MemberParameter> members);
Sebastian Jansson55251c32019-08-08 11:14:51 +0200102
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200103 std::vector<struct_parser_impl::MemberParameter> members_;
Sebastian Jansson55251c32019-08-08 11:14:51 +0200104};
105
Sebastian Jansson55251c32019-08-08 11:14:51 +0200106} // namespace webrtc
107
108#endif // RTC_BASE_EXPERIMENTS_STRUCT_PARAMETERS_PARSER_H_