File: config.h

package info (click to toggle)
webrtc-audio-processing 1.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,112 kB
  • sloc: cpp: 50,766; ansic: 19,793; asm: 236; makefile: 4
file content (131 lines) | stat: -rw-r--r-- 3,737 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef MODULES_AUDIO_PROCESSING_INCLUDE_CONFIG_H_
#define MODULES_AUDIO_PROCESSING_INCLUDE_CONFIG_H_

#include <map>

#include "rtc_base/system/rtc_export.h"

namespace webrtc {

// Only add new values to the end of the enumeration and never remove (only
// deprecate) to maintain binary compatibility.
enum class ConfigOptionID {
  kMyExperimentForTest,
  kAlgo1CostFunctionForTest,
  kTemporalLayersFactory,  // Deprecated
  kNetEqCapacityConfig,    // Deprecated
  kNetEqFastAccelerate,    // Deprecated
  kVoicePacing,            // Deprecated
  kExtendedFilter,         // Deprecated
  kDelayAgnostic,          // Deprecated
  kExperimentalAgc,
  kExperimentalNs,
  kBeamforming,               // Deprecated
  kIntelligibility,           // Deprecated
  kEchoCanceller3,            // Deprecated
  kAecRefinedAdaptiveFilter,  // Deprecated
  kLevelControl               // Deprecated
};

// Class Config is designed to ease passing a set of options across webrtc code.
// Options are identified by typename in order to avoid incorrect casts.
//
// Usage:
// * declaring an option:
//    struct Algo1_CostFunction {
//      virtual float cost(int x) const { return x; }
//      virtual ~Algo1_CostFunction() {}
//    };
//
// * accessing an option:
//    config.Get<Algo1_CostFunction>().cost(value);
//
// * setting an option:
//    struct SqrCost : Algo1_CostFunction {
//      virtual float cost(int x) const { return x*x; }
//    };
//    config.Set<Algo1_CostFunction>(new SqrCost());
//
// Note: This class is thread-compatible (like STL containers).
class RTC_EXPORT Config {
 public:
  // Returns the option if set or a default constructed one.
  // Callers that access options too often are encouraged to cache the result.
  // Returned references are owned by this.
  //
  // Requires std::is_default_constructible<T>
  template <typename T>
  const T& Get() const;

  // Set the option, deleting any previous instance of the same.
  // This instance gets ownership of the newly set value.
  template <typename T>
  void Set(T* value);

  Config();
  ~Config();

 private:
  struct BaseOption {
    virtual ~BaseOption() {}
  };

  template <typename T>
  struct Option : BaseOption {
    explicit Option(T* v) : value(v) {}
    ~Option() { delete value; }
    T* value;
  };

  template <typename T>
  static ConfigOptionID identifier() {
    return T::identifier;
  }

  // Used to instantiate a default constructed object that doesn't needs to be
  // owned. This allows Get<T> to be implemented without requiring explicitly
  // locks.
  template <typename T>
  static const T& default_value() {
    static const T* const def = new T();
    return *def;
  }

  typedef std::map<ConfigOptionID, BaseOption*> OptionMap;
  OptionMap options_;

  Config(const Config&);
  void operator=(const Config&);
};

template <typename T>
const T& Config::Get() const {
  OptionMap::const_iterator it = options_.find(identifier<T>());
  if (it != options_.end()) {
    const T* t = static_cast<Option<T>*>(it->second)->value;
    if (t) {
      return *t;
    }
  }
  return default_value<T>();
}

template <typename T>
void Config::Set(T* value) {
  BaseOption*& it = options_[identifier<T>()];
  delete it;
  it = new Option<T>(value);
}
}  // namespace webrtc

#endif  // MODULES_AUDIO_PROCESSING_INCLUDE_CONFIG_H_