struct_parameters_parser.h 3.7 KB

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