andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 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 | |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 11 | #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_INCLUDE_CONFIG_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_PROCESSING_INCLUDE_CONFIG_H_ |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 13 | |
| 14 | #include <map> |
| 15 | |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 16 | #include "webrtc/rtc_base/basictypes.h" |
| 17 | #include "webrtc/rtc_base/constructormagic.h" |
Andrew MacDonald | 469c2c0 | 2015-05-22 17:50:26 -0700 | [diff] [blame] | 18 | |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 19 | namespace webrtc { |
| 20 | |
aluebs | 688e308 | 2016-01-14 04:32:46 -0800 | [diff] [blame] | 21 | // Only add new values to the end of the enumeration and never remove (only |
| 22 | // deprecate) to maintain binary compatibility. |
| 23 | enum class ConfigOptionID { |
| 24 | kMyExperimentForTest, |
| 25 | kAlgo1CostFunctionForTest, |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 26 | kTemporalLayersFactory, // Deprecated |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 27 | kNetEqCapacityConfig, // Deprecated |
| 28 | kNetEqFastAccelerate, // Deprecated |
| 29 | kVoicePacing, // Deprecated |
aluebs | 688e308 | 2016-01-14 04:32:46 -0800 | [diff] [blame] | 30 | kExtendedFilter, |
| 31 | kDelayAgnostic, |
| 32 | kExperimentalAgc, |
| 33 | kExperimentalNs, |
| 34 | kBeamforming, |
peah | a332e2d | 2016-02-17 01:11:16 -0800 | [diff] [blame] | 35 | kIntelligibility, |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 36 | kEchoCanceller3, // Deprecated |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 37 | kAecRefinedAdaptiveFilter, |
| 38 | kLevelControl |
aluebs | 688e308 | 2016-01-14 04:32:46 -0800 | [diff] [blame] | 39 | }; |
| 40 | |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 41 | // Class Config is designed to ease passing a set of options across webrtc code. |
| 42 | // Options are identified by typename in order to avoid incorrect casts. |
| 43 | // |
| 44 | // Usage: |
| 45 | // * declaring an option: |
| 46 | // struct Algo1_CostFunction { |
| 47 | // virtual float cost(int x) const { return x; } |
| 48 | // virtual ~Algo1_CostFunction() {} |
| 49 | // }; |
| 50 | // |
| 51 | // * accessing an option: |
| 52 | // config.Get<Algo1_CostFunction>().cost(value); |
| 53 | // |
| 54 | // * setting an option: |
| 55 | // struct SqrCost : Algo1_CostFunction { |
| 56 | // virtual float cost(int x) const { return x*x; } |
| 57 | // }; |
| 58 | // config.Set<Algo1_CostFunction>(new SqrCost()); |
| 59 | // |
| 60 | // Note: This class is thread-compatible (like STL containers). |
| 61 | class Config { |
| 62 | public: |
| 63 | // Returns the option if set or a default constructed one. |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 64 | // Callers that access options too often are encouraged to cache the result. |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 65 | // Returned references are owned by this. |
| 66 | // |
| 67 | // Requires std::is_default_constructible<T> |
| 68 | template<typename T> const T& Get() const; |
| 69 | |
| 70 | // Set the option, deleting any previous instance of the same. |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 71 | // This instance gets ownership of the newly set value. |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 72 | template<typename T> void Set(T* value); |
| 73 | |
kwiberg | 65fc8b9 | 2016-08-29 10:05:24 -0700 | [diff] [blame] | 74 | Config(); |
| 75 | ~Config(); |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 76 | |
| 77 | private: |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 78 | struct BaseOption { |
| 79 | virtual ~BaseOption() {} |
| 80 | }; |
| 81 | |
| 82 | template<typename T> |
| 83 | struct Option : BaseOption { |
| 84 | explicit Option(T* v): value(v) {} |
| 85 | ~Option() { |
| 86 | delete value; |
| 87 | } |
| 88 | T* value; |
| 89 | }; |
| 90 | |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 91 | template<typename T> |
aluebs | 688e308 | 2016-01-14 04:32:46 -0800 | [diff] [blame] | 92 | static ConfigOptionID identifier() { |
| 93 | return T::identifier; |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // Used to instantiate a default constructed object that doesn't needs to be |
| 97 | // owned. This allows Get<T> to be implemented without requiring explicitly |
| 98 | // locks. |
| 99 | template<typename T> |
| 100 | static const T& default_value() { |
Andrew MacDonald | 469c2c0 | 2015-05-22 17:50:26 -0700 | [diff] [blame] | 101 | RTC_DEFINE_STATIC_LOCAL(const T, def, ()); |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 102 | return def; |
| 103 | } |
| 104 | |
aluebs | 688e308 | 2016-01-14 04:32:46 -0800 | [diff] [blame] | 105 | typedef std::map<ConfigOptionID, BaseOption*> OptionMap; |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 106 | OptionMap options_; |
| 107 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 108 | // RTC_DISALLOW_COPY_AND_ASSIGN |
andresp@webrtc.org | 6b68c28 | 2013-05-13 08:06:36 +0000 | [diff] [blame] | 109 | Config(const Config&); |
| 110 | void operator=(const Config&); |
| 111 | }; |
| 112 | |
| 113 | template<typename T> |
| 114 | const T& Config::Get() const { |
| 115 | OptionMap::const_iterator it = options_.find(identifier<T>()); |
| 116 | if (it != options_.end()) { |
| 117 | const T* t = static_cast<Option<T>*>(it->second)->value; |
| 118 | if (t) { |
| 119 | return *t; |
| 120 | } |
| 121 | } |
| 122 | return default_value<T>(); |
| 123 | } |
| 124 | |
| 125 | template<typename T> |
| 126 | void Config::Set(T* value) { |
| 127 | BaseOption*& it = options_[identifier<T>()]; |
| 128 | delete it; |
| 129 | it = new Option<T>(value); |
| 130 | } |
| 131 | } // namespace webrtc |
andrew@webrtc.org | 61e596f | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 132 | |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 133 | #endif // WEBRTC_MODULES_AUDIO_PROCESSING_INCLUDE_CONFIG_H_ |