blob: 523ecfb05d4eb854766d88d9c5ca6e6f181567f0 [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>;
Bjorn Terelius9f00f0e2019-08-30 09:39:31 +020056extern template class TypedParser<unsigned>;
Sebastian Jansson0ee80082019-08-14 13:16:26 +020057extern template class TypedParser<absl::optional<double>>;
58extern template class TypedParser<absl::optional<int>>;
Bjorn Terelius9f00f0e2019-08-30 09:39:31 +020059extern template class TypedParser<absl::optional<unsigned>>;
Sebastian Jansson0ee80082019-08-14 13:16:26 +020060
61extern template class TypedParser<DataRate>;
62extern template class TypedParser<DataSize>;
63extern template class TypedParser<TimeDelta>;
64extern template class TypedParser<absl::optional<DataRate>>;
65extern template class TypedParser<absl::optional<DataSize>>;
66extern template class TypedParser<absl::optional<TimeDelta>>;
Sebastian Jansson55251c32019-08-08 11:14:51 +020067
68template <typename T>
Sebastian Jansson0ee80082019-08-14 13:16:26 +020069void AddMembers(MemberParameter* out, const char* key, T* member) {
70 *out = MemberParameter{
71 key, member,
72 TypedMemberParser{&TypedParser<T>::Parse, &TypedParser<T>::Encode}};
Sebastian Jansson55251c32019-08-08 11:14:51 +020073}
74
Sebastian Jansson0ee80082019-08-14 13:16:26 +020075template <typename T, typename... Args>
76void AddMembers(MemberParameter* out,
77 const char* key,
78 T* member,
79 Args... args) {
80 AddMembers(out, key, member);
81 AddMembers(++out, args...);
Sebastian Jansson55251c32019-08-08 11:14:51 +020082}
Sebastian Jansson55251c32019-08-08 11:14:51 +020083} // namespace struct_parser_impl
84
Sebastian Jansson55251c32019-08-08 11:14:51 +020085class StructParametersParser {
86 public:
Sebastian Jansson0ee80082019-08-14 13:16:26 +020087 template <typename T, typename... 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)));
Sebastian Jansson55251c32019-08-08 11:14:51 +020096 }
97
Sebastian Jansson0ee80082019-08-14 13:16:26 +020098 void Parse(absl::string_view src);
99 std::string Encode() const;
Sebastian Jansson55251c32019-08-08 11:14:51 +0200100
101 private:
Sebastian Jansson55251c32019-08-08 11:14:51 +0200102 explicit StructParametersParser(
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200103 std::vector<struct_parser_impl::MemberParameter> members);
Sebastian Jansson55251c32019-08-08 11:14:51 +0200104
Sebastian Jansson0ee80082019-08-14 13:16:26 +0200105 std::vector<struct_parser_impl::MemberParameter> members_;
Sebastian Jansson55251c32019-08-08 11:14:51 +0200106};
107
Sebastian Jansson55251c32019-08-08 11:14:51 +0200108} // namespace webrtc
109
110#endif // RTC_BASE_EXPERIMENTS_STRUCT_PARAMETERS_PARSER_H_