andresp@webrtc.org | 1dba621 | 2013-05-13 08:06:36 | [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 | |
andrew@webrtc.org | 25613ea | 2013-07-25 18:28:29 | [diff] [blame] | 11 | #ifndef WEBRTC_COMMON_H_ |
| 12 | #define WEBRTC_COMMON_H_ |
andresp@webrtc.org | 1dba621 | 2013-05-13 08:06:36 | [diff] [blame] | 13 | |
| 14 | #include <map> |
| 15 | |
Andrew MacDonald | 0a66614 | 2015-05-23 00:50:26 | [diff] [blame] | 16 | #include "webrtc/base/basictypes.h" |
kwiberg | 5fb5bd2 | 2016-04-26 15:14:39 | [diff] [blame] | 17 | #include "webrtc/base/constructormagic.h" |
Andrew MacDonald | 0a66614 | 2015-05-23 00:50:26 | [diff] [blame] | 18 | |
andresp@webrtc.org | 1dba621 | 2013-05-13 08:06:36 | [diff] [blame] | 19 | namespace webrtc { |
| 20 | |
aluebs | bf4689d | 2016-01-14 12:32:46 | [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, |
| 26 | kTemporalLayersFactory, |
| 27 | kNetEqCapacityConfig, |
| 28 | kNetEqFastAccelerate, |
| 29 | kVoicePacing, |
| 30 | kExtendedFilter, |
| 31 | kDelayAgnostic, |
| 32 | kExperimentalAgc, |
| 33 | kExperimentalNs, |
| 34 | kBeamforming, |
peah | f4c040d | 2016-02-17 09:11:16 | [diff] [blame] | 35 | kIntelligibility, |
peah | 780d506 | 2016-04-15 18:23:33 | [diff] [blame] | 36 | kEchoCanceller3, |
| 37 | kAecRefinedAdaptiveFilter |
aluebs | bf4689d | 2016-01-14 12:32:46 | [diff] [blame] | 38 | }; |
| 39 | |
andresp@webrtc.org | 1dba621 | 2013-05-13 08:06:36 | [diff] [blame] | 40 | // Class Config is designed to ease passing a set of options across webrtc code. |
| 41 | // Options are identified by typename in order to avoid incorrect casts. |
| 42 | // |
| 43 | // Usage: |
| 44 | // * declaring an option: |
| 45 | // struct Algo1_CostFunction { |
| 46 | // virtual float cost(int x) const { return x; } |
| 47 | // virtual ~Algo1_CostFunction() {} |
| 48 | // }; |
| 49 | // |
| 50 | // * accessing an option: |
| 51 | // config.Get<Algo1_CostFunction>().cost(value); |
| 52 | // |
| 53 | // * setting an option: |
| 54 | // struct SqrCost : Algo1_CostFunction { |
| 55 | // virtual float cost(int x) const { return x*x; } |
| 56 | // }; |
| 57 | // config.Set<Algo1_CostFunction>(new SqrCost()); |
| 58 | // |
| 59 | // Note: This class is thread-compatible (like STL containers). |
| 60 | class Config { |
| 61 | public: |
| 62 | // Returns the option if set or a default constructed one. |
andrew@webrtc.org | 25613ea | 2013-07-25 18:28:29 | [diff] [blame] | 63 | // Callers that access options too often are encouraged to cache the result. |
andresp@webrtc.org | 1dba621 | 2013-05-13 08:06:36 | [diff] [blame] | 64 | // Returned references are owned by this. |
| 65 | // |
| 66 | // Requires std::is_default_constructible<T> |
| 67 | template<typename T> const T& Get() const; |
| 68 | |
| 69 | // Set the option, deleting any previous instance of the same. |
andrew@webrtc.org | 25613ea | 2013-07-25 18:28:29 | [diff] [blame] | 70 | // This instance gets ownership of the newly set value. |
andresp@webrtc.org | 1dba621 | 2013-05-13 08:06:36 | [diff] [blame] | 71 | template<typename T> void Set(T* value); |
| 72 | |
| 73 | Config() {} |
| 74 | ~Config() { |
| 75 | // Note: this method is inline so webrtc public API depends only |
| 76 | // on the headers. |
| 77 | for (OptionMap::iterator it = options_.begin(); |
| 78 | it != options_.end(); ++it) { |
| 79 | delete it->second; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private: |
andresp@webrtc.org | 1dba621 | 2013-05-13 08:06:36 | [diff] [blame] | 84 | struct BaseOption { |
| 85 | virtual ~BaseOption() {} |
| 86 | }; |
| 87 | |
| 88 | template<typename T> |
| 89 | struct Option : BaseOption { |
| 90 | explicit Option(T* v): value(v) {} |
| 91 | ~Option() { |
| 92 | delete value; |
| 93 | } |
| 94 | T* value; |
| 95 | }; |
| 96 | |
andresp@webrtc.org | 1dba621 | 2013-05-13 08:06:36 | [diff] [blame] | 97 | template<typename T> |
aluebs | bf4689d | 2016-01-14 12:32:46 | [diff] [blame] | 98 | static ConfigOptionID identifier() { |
| 99 | return T::identifier; |
andresp@webrtc.org | 1dba621 | 2013-05-13 08:06:36 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Used to instantiate a default constructed object that doesn't needs to be |
| 103 | // owned. This allows Get<T> to be implemented without requiring explicitly |
| 104 | // locks. |
| 105 | template<typename T> |
| 106 | static const T& default_value() { |
Andrew MacDonald | 0a66614 | 2015-05-23 00:50:26 | [diff] [blame] | 107 | RTC_DEFINE_STATIC_LOCAL(const T, def, ()); |
andresp@webrtc.org | 1dba621 | 2013-05-13 08:06:36 | [diff] [blame] | 108 | return def; |
| 109 | } |
| 110 | |
aluebs | bf4689d | 2016-01-14 12:32:46 | [diff] [blame] | 111 | typedef std::map<ConfigOptionID, BaseOption*> OptionMap; |
andresp@webrtc.org | 1dba621 | 2013-05-13 08:06:36 | [diff] [blame] | 112 | OptionMap options_; |
| 113 | |
henrikg | 9199c0e | 2015-09-16 12:37:44 | [diff] [blame] | 114 | // RTC_DISALLOW_COPY_AND_ASSIGN |
andresp@webrtc.org | 1dba621 | 2013-05-13 08:06:36 | [diff] [blame] | 115 | Config(const Config&); |
| 116 | void operator=(const Config&); |
| 117 | }; |
| 118 | |
| 119 | template<typename T> |
| 120 | const T& Config::Get() const { |
| 121 | OptionMap::const_iterator it = options_.find(identifier<T>()); |
| 122 | if (it != options_.end()) { |
| 123 | const T* t = static_cast<Option<T>*>(it->second)->value; |
| 124 | if (t) { |
| 125 | return *t; |
| 126 | } |
| 127 | } |
| 128 | return default_value<T>(); |
| 129 | } |
| 130 | |
| 131 | template<typename T> |
| 132 | void Config::Set(T* value) { |
| 133 | BaseOption*& it = options_[identifier<T>()]; |
| 134 | delete it; |
| 135 | it = new Option<T>(value); |
| 136 | } |
andrew@webrtc.org | 25613ea | 2013-07-25 18:28:29 | [diff] [blame] | 137 | |
andresp@webrtc.org | 1dba621 | 2013-05-13 08:06:36 | [diff] [blame] | 138 | } // namespace webrtc |
andrew@webrtc.org | 25613ea | 2013-07-25 18:28:29 | [diff] [blame] | 139 | |
| 140 | #endif // WEBRTC_COMMON_H_ |