OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_COMMON_H_ | 11 #ifndef WEBRTC_COMMON_H_ |
12 #define WEBRTC_COMMON_H_ | 12 #define WEBRTC_COMMON_H_ |
13 | 13 |
14 #include <map> | 14 #include <map> |
15 | 15 |
16 #include "webrtc/base/basictypes.h" | 16 #include "webrtc/base/basictypes.h" |
17 | 17 |
18 namespace webrtc { | 18 namespace webrtc { |
19 | 19 |
20 // Only add new values to the end of the enumeration and never remove (only | |
21 // deprecate) to maintain binary compatibility. | |
22 enum class ConfigOptionID { | |
23 kMyExperimentForTest, | |
24 kAlgo1CostFunctionForTest, | |
25 kTemporalLayersFactory, | |
26 kNetEqCapacityConfig, | |
27 kNetEqFastAccelerate, | |
28 kVoicePacing, | |
29 kExtendedFilter, | |
30 kDelayAgnostic, | |
31 kExperimentalAgc, | |
32 kExperimentalNs, | |
33 kBeamforming, | |
34 kIntelligibility | |
35 }; | |
36 | |
37 // Class Config is designed to ease passing a set of options across webrtc code. | 20 // Class Config is designed to ease passing a set of options across webrtc code. |
38 // Options are identified by typename in order to avoid incorrect casts. | 21 // Options are identified by typename in order to avoid incorrect casts. |
39 // | 22 // |
40 // Usage: | 23 // Usage: |
41 // * declaring an option: | 24 // * declaring an option: |
42 // struct Algo1_CostFunction { | 25 // struct Algo1_CostFunction { |
43 // virtual float cost(int x) const { return x; } | 26 // virtual float cost(int x) const { return x; } |
44 // virtual ~Algo1_CostFunction() {} | 27 // virtual ~Algo1_CostFunction() {} |
45 // }; | 28 // }; |
46 // | 29 // |
(...skipping 24 matching lines...) Expand all Loading... |
71 ~Config() { | 54 ~Config() { |
72 // Note: this method is inline so webrtc public API depends only | 55 // Note: this method is inline so webrtc public API depends only |
73 // on the headers. | 56 // on the headers. |
74 for (OptionMap::iterator it = options_.begin(); | 57 for (OptionMap::iterator it = options_.begin(); |
75 it != options_.end(); ++it) { | 58 it != options_.end(); ++it) { |
76 delete it->second; | 59 delete it->second; |
77 } | 60 } |
78 } | 61 } |
79 | 62 |
80 private: | 63 private: |
| 64 typedef void* OptionIdentifier; |
| 65 |
81 struct BaseOption { | 66 struct BaseOption { |
82 virtual ~BaseOption() {} | 67 virtual ~BaseOption() {} |
83 }; | 68 }; |
84 | 69 |
85 template<typename T> | 70 template<typename T> |
86 struct Option : BaseOption { | 71 struct Option : BaseOption { |
87 explicit Option(T* v): value(v) {} | 72 explicit Option(T* v): value(v) {} |
88 ~Option() { | 73 ~Option() { |
89 delete value; | 74 delete value; |
90 } | 75 } |
91 T* value; | 76 T* value; |
92 }; | 77 }; |
93 | 78 |
| 79 // Own implementation of rtti-subset to avoid depending on rtti and its costs. |
94 template<typename T> | 80 template<typename T> |
95 static ConfigOptionID identifier() { | 81 static OptionIdentifier identifier() { |
96 return T::identifier; | 82 static char id_placeholder; |
| 83 return &id_placeholder; |
97 } | 84 } |
98 | 85 |
99 // Used to instantiate a default constructed object that doesn't needs to be | 86 // Used to instantiate a default constructed object that doesn't needs to be |
100 // owned. This allows Get<T> to be implemented without requiring explicitly | 87 // owned. This allows Get<T> to be implemented without requiring explicitly |
101 // locks. | 88 // locks. |
102 template<typename T> | 89 template<typename T> |
103 static const T& default_value() { | 90 static const T& default_value() { |
104 RTC_DEFINE_STATIC_LOCAL(const T, def, ()); | 91 RTC_DEFINE_STATIC_LOCAL(const T, def, ()); |
105 return def; | 92 return def; |
106 } | 93 } |
107 | 94 |
108 typedef std::map<ConfigOptionID, BaseOption*> OptionMap; | 95 typedef std::map<OptionIdentifier, BaseOption*> OptionMap; |
109 OptionMap options_; | 96 OptionMap options_; |
110 | 97 |
111 // RTC_DISALLOW_COPY_AND_ASSIGN | 98 // RTC_DISALLOW_COPY_AND_ASSIGN |
112 Config(const Config&); | 99 Config(const Config&); |
113 void operator=(const Config&); | 100 void operator=(const Config&); |
114 }; | 101 }; |
115 | 102 |
116 template<typename T> | 103 template<typename T> |
117 const T& Config::Get() const { | 104 const T& Config::Get() const { |
118 OptionMap::const_iterator it = options_.find(identifier<T>()); | 105 OptionMap::const_iterator it = options_.find(identifier<T>()); |
119 if (it != options_.end()) { | 106 if (it != options_.end()) { |
120 const T* t = static_cast<Option<T>*>(it->second)->value; | 107 const T* t = static_cast<Option<T>*>(it->second)->value; |
121 if (t) { | 108 if (t) { |
122 return *t; | 109 return *t; |
123 } | 110 } |
124 } | 111 } |
125 return default_value<T>(); | 112 return default_value<T>(); |
126 } | 113 } |
127 | 114 |
128 template<typename T> | 115 template<typename T> |
129 void Config::Set(T* value) { | 116 void Config::Set(T* value) { |
130 BaseOption*& it = options_[identifier<T>()]; | 117 BaseOption*& it = options_[identifier<T>()]; |
131 delete it; | 118 delete it; |
132 it = new Option<T>(value); | 119 it = new Option<T>(value); |
133 } | 120 } |
134 | 121 |
135 } // namespace webrtc | 122 } // namespace webrtc |
136 | 123 |
137 #endif // WEBRTC_COMMON_H_ | 124 #endif // WEBRTC_COMMON_H_ |
OLD | NEW |