config.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2013 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 MODULES_AUDIO_PROCESSING_INCLUDE_CONFIG_H_
  11. #define MODULES_AUDIO_PROCESSING_INCLUDE_CONFIG_H_
  12. #include <map>
  13. #include "rtc_base/constructor_magic.h"
  14. #include "rtc_base/system/rtc_export.h"
  15. namespace webrtc {
  16. // Only add new values to the end of the enumeration and never remove (only
  17. // deprecate) to maintain binary compatibility.
  18. enum class ConfigOptionID {
  19. kMyExperimentForTest,
  20. kAlgo1CostFunctionForTest,
  21. kTemporalLayersFactory, // Deprecated
  22. kNetEqCapacityConfig, // Deprecated
  23. kNetEqFastAccelerate, // Deprecated
  24. kVoicePacing, // Deprecated
  25. kExtendedFilter, // Deprecated
  26. kDelayAgnostic, // Deprecated
  27. kExperimentalAgc,
  28. kExperimentalNs,
  29. kBeamforming, // Deprecated
  30. kIntelligibility, // Deprecated
  31. kEchoCanceller3, // Deprecated
  32. kAecRefinedAdaptiveFilter, // Deprecated
  33. kLevelControl // Deprecated
  34. };
  35. // Class Config is designed to ease passing a set of options across webrtc code.
  36. // Options are identified by typename in order to avoid incorrect casts.
  37. //
  38. // Usage:
  39. // * declaring an option:
  40. // struct Algo1_CostFunction {
  41. // virtual float cost(int x) const { return x; }
  42. // virtual ~Algo1_CostFunction() {}
  43. // };
  44. //
  45. // * accessing an option:
  46. // config.Get<Algo1_CostFunction>().cost(value);
  47. //
  48. // * setting an option:
  49. // struct SqrCost : Algo1_CostFunction {
  50. // virtual float cost(int x) const { return x*x; }
  51. // };
  52. // config.Set<Algo1_CostFunction>(new SqrCost());
  53. //
  54. // Note: This class is thread-compatible (like STL containers).
  55. class RTC_EXPORT Config {
  56. public:
  57. // Returns the option if set or a default constructed one.
  58. // Callers that access options too often are encouraged to cache the result.
  59. // Returned references are owned by this.
  60. //
  61. // Requires std::is_default_constructible<T>
  62. template <typename T>
  63. const T& Get() const;
  64. // Set the option, deleting any previous instance of the same.
  65. // This instance gets ownership of the newly set value.
  66. template <typename T>
  67. void Set(T* value);
  68. Config();
  69. ~Config();
  70. private:
  71. struct BaseOption {
  72. virtual ~BaseOption() {}
  73. };
  74. template <typename T>
  75. struct Option : BaseOption {
  76. explicit Option(T* v) : value(v) {}
  77. ~Option() { delete value; }
  78. T* value;
  79. };
  80. template <typename T>
  81. static ConfigOptionID identifier() {
  82. return T::identifier;
  83. }
  84. // Used to instantiate a default constructed object that doesn't needs to be
  85. // owned. This allows Get<T> to be implemented without requiring explicitly
  86. // locks.
  87. template <typename T>
  88. static const T& default_value() {
  89. static const T* const def = new T();
  90. return *def;
  91. }
  92. typedef std::map<ConfigOptionID, BaseOption*> OptionMap;
  93. OptionMap options_;
  94. // RTC_DISALLOW_COPY_AND_ASSIGN
  95. Config(const Config&);
  96. void operator=(const Config&);
  97. };
  98. template <typename T>
  99. const T& Config::Get() const {
  100. OptionMap::const_iterator it = options_.find(identifier<T>());
  101. if (it != options_.end()) {
  102. const T* t = static_cast<Option<T>*>(it->second)->value;
  103. if (t) {
  104. return *t;
  105. }
  106. }
  107. return default_value<T>();
  108. }
  109. template <typename T>
  110. void Config::Set(T* value) {
  111. BaseOption*& it = options_[identifier<T>()];
  112. delete it;
  113. it = new Option<T>(value);
  114. }
  115. } // namespace webrtc
  116. #endif // MODULES_AUDIO_PROCESSING_INCLUDE_CONFIG_H_