blob: f2a868241d2fb13c8ca6b7b86dc62b991666866b [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"
17
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000018namespace webrtc {
19
aluebs688e3082016-01-14 04:32:46 -080020// Only add new values to the end of the enumeration and never remove (only
21// deprecate) to maintain binary compatibility.
22enum class ConfigOptionID {
23 kMyExperimentForTest,
24 kAlgo1CostFunctionForTest,
25 kTemporalLayersFactory,
26 kNetEqCapacityConfig,
27 kNetEqFastAccelerate,
28 kVoicePacing,
29 kExtendedFilter,
30 kDelayAgnostic,
31 kExperimentalAgc,
32 kExperimentalNs,
33 kBeamforming,
peaha332e2d2016-02-17 01:11:16 -080034 kIntelligibility,
peah0332c2d2016-04-15 11:23:33 -070035 kEchoCanceller3,
36 kAecRefinedAdaptiveFilter
aluebs688e3082016-01-14 04:32:46 -080037};
38
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000039// Class Config is designed to ease passing a set of options across webrtc code.
40// Options are identified by typename in order to avoid incorrect casts.
41//
42// Usage:
43// * declaring an option:
44// struct Algo1_CostFunction {
45// virtual float cost(int x) const { return x; }
46// virtual ~Algo1_CostFunction() {}
47// };
48//
49// * accessing an option:
50// config.Get<Algo1_CostFunction>().cost(value);
51//
52// * setting an option:
53// struct SqrCost : Algo1_CostFunction {
54// virtual float cost(int x) const { return x*x; }
55// };
56// config.Set<Algo1_CostFunction>(new SqrCost());
57//
58// Note: This class is thread-compatible (like STL containers).
59class Config {
60 public:
61 // Returns the option if set or a default constructed one.
andrew@webrtc.org61e596f2013-07-25 18:28:29 +000062 // Callers that access options too often are encouraged to cache the result.
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000063 // Returned references are owned by this.
64 //
65 // Requires std::is_default_constructible<T>
66 template<typename T> const T& Get() const;
67
68 // Set the option, deleting any previous instance of the same.
andrew@webrtc.org61e596f2013-07-25 18:28:29 +000069 // This instance gets ownership of the newly set value.
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000070 template<typename T> void Set(T* value);
71
72 Config() {}
73 ~Config() {
74 // Note: this method is inline so webrtc public API depends only
75 // on the headers.
76 for (OptionMap::iterator it = options_.begin();
77 it != options_.end(); ++it) {
78 delete it->second;
79 }
80 }
81
82 private:
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000083 struct BaseOption {
84 virtual ~BaseOption() {}
85 };
86
87 template<typename T>
88 struct Option : BaseOption {
89 explicit Option(T* v): value(v) {}
90 ~Option() {
91 delete value;
92 }
93 T* value;
94 };
95
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000096 template<typename T>
aluebs688e3082016-01-14 04:32:46 -080097 static ConfigOptionID identifier() {
98 return T::identifier;
andresp@webrtc.org6b68c282013-05-13 08:06:36 +000099 }
100
101 // Used to instantiate a default constructed object that doesn't needs to be
102 // owned. This allows Get<T> to be implemented without requiring explicitly
103 // locks.
104 template<typename T>
105 static const T& default_value() {
Andrew MacDonald469c2c02015-05-22 17:50:26 -0700106 RTC_DEFINE_STATIC_LOCAL(const T, def, ());
andresp@webrtc.org6b68c282013-05-13 08:06:36 +0000107 return def;
108 }
109
aluebs688e3082016-01-14 04:32:46 -0800110 typedef std::map<ConfigOptionID, BaseOption*> OptionMap;
andresp@webrtc.org6b68c282013-05-13 08:06:36 +0000111 OptionMap options_;
112
henrikg3c089d72015-09-16 05:37:44 -0700113 // RTC_DISALLOW_COPY_AND_ASSIGN
andresp@webrtc.org6b68c282013-05-13 08:06:36 +0000114 Config(const Config&);
115 void operator=(const Config&);
116};
117
118template<typename T>
119const T& Config::Get() const {
120 OptionMap::const_iterator it = options_.find(identifier<T>());
121 if (it != options_.end()) {
122 const T* t = static_cast<Option<T>*>(it->second)->value;
123 if (t) {
124 return *t;
125 }
126 }
127 return default_value<T>();
128}
129
130template<typename T>
131void Config::Set(T* value) {
132 BaseOption*& it = options_[identifier<T>()];
133 delete it;
134 it = new Option<T>(value);
135}
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000136
andresp@webrtc.org6b68c282013-05-13 08:06:36 +0000137} // namespace webrtc
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000138
139#endif // WEBRTC_COMMON_H_