blob: 8795064fd07e3ebb0e63e0a51cdb2ee8e7b1cc6b [file] [log] [blame]
andresp@webrtc.org6b68c282013-05-13 08:06:36 +00001/*
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.org61e596f2013-07-25 18:28:29 +000011#ifndef WEBRTC_COMMON_H_
12#define WEBRTC_COMMON_H_
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000013
14#include <map>
15
Andrew MacDonald469c2c02015-05-22 17:50:26 -070016#include "webrtc/base/basictypes.h"
kwiberg4485ffb2016-04-26 08:14:39 -070017#include "webrtc/base/constructormagic.h"
Andrew MacDonald469c2c02015-05-22 17:50:26 -070018
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000019namespace webrtc {
20
aluebs688e3082016-01-14 04:32:46 -080021// Only add new values to the end of the enumeration and never remove (only
22// deprecate) to maintain binary compatibility.
23enum class ConfigOptionID {
24 kMyExperimentForTest,
25 kAlgo1CostFunctionForTest,
26 kTemporalLayersFactory,
27 kNetEqCapacityConfig,
28 kNetEqFastAccelerate,
29 kVoicePacing,
30 kExtendedFilter,
31 kDelayAgnostic,
32 kExperimentalAgc,
33 kExperimentalNs,
34 kBeamforming,
peaha332e2d2016-02-17 01:11:16 -080035 kIntelligibility,
peah0332c2d2016-04-15 11:23:33 -070036 kEchoCanceller3,
peahca4cac72016-06-29 15:26:12 -070037 kAecRefinedAdaptiveFilter,
38 kLevelControl
aluebs688e3082016-01-14 04:32:46 -080039};
40
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000041// 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).
61class Config {
62 public:
63 // Returns the option if set or a default constructed one.
andrew@webrtc.org61e596f2013-07-25 18:28:29 +000064 // Callers that access options too often are encouraged to cache the result.
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000065 // 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.org61e596f2013-07-25 18:28:29 +000071 // This instance gets ownership of the newly set value.
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000072 template<typename T> void Set(T* value);
73
74 Config() {}
75 ~Config() {
76 // Note: this method is inline so webrtc public API depends only
77 // on the headers.
78 for (OptionMap::iterator it = options_.begin();
79 it != options_.end(); ++it) {
80 delete it->second;
81 }
82 }
83
84 private:
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000085 struct BaseOption {
86 virtual ~BaseOption() {}
87 };
88
89 template<typename T>
90 struct Option : BaseOption {
91 explicit Option(T* v): value(v) {}
92 ~Option() {
93 delete value;
94 }
95 T* value;
96 };
97
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000098 template<typename T>
aluebs688e3082016-01-14 04:32:46 -080099 static ConfigOptionID identifier() {
100 return T::identifier;
andresp@webrtc.org6b68c282013-05-13 08:06:36 +0000101 }
102
103 // Used to instantiate a default constructed object that doesn't needs to be
104 // owned. This allows Get<T> to be implemented without requiring explicitly
105 // locks.
106 template<typename T>
107 static const T& default_value() {
Andrew MacDonald469c2c02015-05-22 17:50:26 -0700108 RTC_DEFINE_STATIC_LOCAL(const T, def, ());
andresp@webrtc.org6b68c282013-05-13 08:06:36 +0000109 return def;
110 }
111
aluebs688e3082016-01-14 04:32:46 -0800112 typedef std::map<ConfigOptionID, BaseOption*> OptionMap;
andresp@webrtc.org6b68c282013-05-13 08:06:36 +0000113 OptionMap options_;
114
henrikg3c089d72015-09-16 05:37:44 -0700115 // RTC_DISALLOW_COPY_AND_ASSIGN
andresp@webrtc.org6b68c282013-05-13 08:06:36 +0000116 Config(const Config&);
117 void operator=(const Config&);
118};
119
120template<typename T>
121const T& Config::Get() const {
122 OptionMap::const_iterator it = options_.find(identifier<T>());
123 if (it != options_.end()) {
124 const T* t = static_cast<Option<T>*>(it->second)->value;
125 if (t) {
126 return *t;
127 }
128 }
129 return default_value<T>();
130}
131
132template<typename T>
133void Config::Set(T* value) {
134 BaseOption*& it = options_[identifier<T>()];
135 delete it;
136 it = new Option<T>(value);
137}
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000138
andresp@webrtc.org6b68c282013-05-13 08:06:36 +0000139} // namespace webrtc
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000140
141#endif // WEBRTC_COMMON_H_