blob: 3aeea814c6bae7305408ccaeb3a49c6725a93d48 [file] [log] [blame]
andresp@webrtc.org1dba6212013-05-13 08:06:361/*
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.org25613ea2013-07-25 18:28:2911#ifndef WEBRTC_COMMON_H_
12#define WEBRTC_COMMON_H_
andresp@webrtc.org1dba6212013-05-13 08:06:3613
14#include <map>
15
Andrew MacDonald0a666142015-05-23 00:50:2616#include "webrtc/base/basictypes.h"
kwiberg5fb5bd22016-04-26 15:14:3917#include "webrtc/base/constructormagic.h"
Andrew MacDonald0a666142015-05-23 00:50:2618
andresp@webrtc.org1dba6212013-05-13 08:06:3619namespace webrtc {
20
aluebsbf4689d2016-01-14 12:32:4621// 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,
peahf4c040d2016-02-17 09:11:1635 kIntelligibility,
peah780d5062016-04-15 18:23:3336 kEchoCanceller3,
37 kAecRefinedAdaptiveFilter
aluebsbf4689d2016-01-14 12:32:4638};
39
andresp@webrtc.org1dba6212013-05-13 08:06:3640// 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).
60class Config {
61 public:
62 // Returns the option if set or a default constructed one.
andrew@webrtc.org25613ea2013-07-25 18:28:2963 // Callers that access options too often are encouraged to cache the result.
andresp@webrtc.org1dba6212013-05-13 08:06:3664 // 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.org25613ea2013-07-25 18:28:2970 // This instance gets ownership of the newly set value.
andresp@webrtc.org1dba6212013-05-13 08:06:3671 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.org1dba6212013-05-13 08:06:3684 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.org1dba6212013-05-13 08:06:3697 template<typename T>
aluebsbf4689d2016-01-14 12:32:4698 static ConfigOptionID identifier() {
99 return T::identifier;
andresp@webrtc.org1dba6212013-05-13 08:06:36100 }
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 MacDonald0a666142015-05-23 00:50:26107 RTC_DEFINE_STATIC_LOCAL(const T, def, ());
andresp@webrtc.org1dba6212013-05-13 08:06:36108 return def;
109 }
110
aluebsbf4689d2016-01-14 12:32:46111 typedef std::map<ConfigOptionID, BaseOption*> OptionMap;
andresp@webrtc.org1dba6212013-05-13 08:06:36112 OptionMap options_;
113
henrikg9199c0e2015-09-16 12:37:44114 // RTC_DISALLOW_COPY_AND_ASSIGN
andresp@webrtc.org1dba6212013-05-13 08:06:36115 Config(const Config&);
116 void operator=(const Config&);
117};
118
119template<typename T>
120const 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
131template<typename T>
132void Config::Set(T* value) {
133 BaseOption*& it = options_[identifier<T>()];
134 delete it;
135 it = new Option<T>(value);
136}
andrew@webrtc.org25613ea2013-07-25 18:28:29137
andresp@webrtc.org1dba6212013-05-13 08:06:36138} // namespace webrtc
andrew@webrtc.org25613ea2013-07-25 18:28:29139
140#endif // WEBRTC_COMMON_H_