| 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  | 
|   20 // Class Config is designed to ease passing a set of options across webrtc code. |   37 // Class Config is designed to ease passing a set of options across webrtc code. | 
|   21 // Options are identified by typename in order to avoid incorrect casts. |   38 // Options are identified by typename in order to avoid incorrect casts. | 
|   22 // |   39 // | 
|   23 // Usage: |   40 // Usage: | 
|   24 // * declaring an option: |   41 // * declaring an option: | 
|   25 //    struct Algo1_CostFunction { |   42 //    struct Algo1_CostFunction { | 
|   26 //      virtual float cost(int x) const { return x; } |   43 //      virtual float cost(int x) const { return x; } | 
|   27 //      virtual ~Algo1_CostFunction() {} |   44 //      virtual ~Algo1_CostFunction() {} | 
|   28 //    }; |   45 //    }; | 
|   29 // |   46 // | 
| (...skipping 24 matching lines...) Expand all  Loading... | 
|   54   ~Config() { |   71   ~Config() { | 
|   55     // Note: this method is inline so webrtc public API depends only |   72     // Note: this method is inline so webrtc public API depends only | 
|   56     // on the headers. |   73     // on the headers. | 
|   57     for (OptionMap::iterator it = options_.begin(); |   74     for (OptionMap::iterator it = options_.begin(); | 
|   58          it != options_.end(); ++it) { |   75          it != options_.end(); ++it) { | 
|   59       delete it->second; |   76       delete it->second; | 
|   60     } |   77     } | 
|   61   } |   78   } | 
|   62  |   79  | 
|   63  private: |   80  private: | 
|   64   typedef void* OptionIdentifier; |  | 
|   65  |  | 
|   66   struct BaseOption { |   81   struct BaseOption { | 
|   67     virtual ~BaseOption() {} |   82     virtual ~BaseOption() {} | 
|   68   }; |   83   }; | 
|   69  |   84  | 
|   70   template<typename T> |   85   template<typename T> | 
|   71   struct Option : BaseOption { |   86   struct Option : BaseOption { | 
|   72     explicit Option(T* v): value(v) {} |   87     explicit Option(T* v): value(v) {} | 
|   73     ~Option() { |   88     ~Option() { | 
|   74       delete value; |   89       delete value; | 
|   75     } |   90     } | 
|   76     T* value; |   91     T* value; | 
|   77   }; |   92   }; | 
|   78  |   93  | 
|   79   // Own implementation of rtti-subset to avoid depending on rtti and its costs. |  | 
|   80   template<typename T> |   94   template<typename T> | 
|   81   static OptionIdentifier identifier() { |   95   static ConfigOptionID identifier() { | 
|   82     static char id_placeholder; |   96     return T::identifier; | 
|   83     return &id_placeholder; |  | 
|   84   } |   97   } | 
|   85  |   98  | 
|   86   // Used to instantiate a default constructed object that doesn't needs to be |   99   // Used to instantiate a default constructed object that doesn't needs to be | 
|   87   // owned. This allows Get<T> to be implemented without requiring explicitly |  100   // owned. This allows Get<T> to be implemented without requiring explicitly | 
|   88   // locks. |  101   // locks. | 
|   89   template<typename T> |  102   template<typename T> | 
|   90   static const T& default_value() { |  103   static const T& default_value() { | 
|   91     RTC_DEFINE_STATIC_LOCAL(const T, def, ()); |  104     RTC_DEFINE_STATIC_LOCAL(const T, def, ()); | 
|   92     return def; |  105     return def; | 
|   93   } |  106   } | 
|   94  |  107  | 
|   95   typedef std::map<OptionIdentifier, BaseOption*> OptionMap; |  108   typedef std::map<ConfigOptionID, BaseOption*> OptionMap; | 
|   96   OptionMap options_; |  109   OptionMap options_; | 
|   97  |  110  | 
|   98   // RTC_DISALLOW_COPY_AND_ASSIGN |  111   // RTC_DISALLOW_COPY_AND_ASSIGN | 
|   99   Config(const Config&); |  112   Config(const Config&); | 
|  100   void operator=(const Config&); |  113   void operator=(const Config&); | 
|  101 }; |  114 }; | 
|  102  |  115  | 
|  103 template<typename T> |  116 template<typename T> | 
|  104 const T& Config::Get() const { |  117 const T& Config::Get() const { | 
|  105   OptionMap::const_iterator it = options_.find(identifier<T>()); |  118   OptionMap::const_iterator it = options_.find(identifier<T>()); | 
|  106   if (it != options_.end()) { |  119   if (it != options_.end()) { | 
|  107     const T* t = static_cast<Option<T>*>(it->second)->value; |  120     const T* t = static_cast<Option<T>*>(it->second)->value; | 
|  108     if (t) { |  121     if (t) { | 
|  109       return *t; |  122       return *t; | 
|  110     } |  123     } | 
|  111   } |  124   } | 
|  112   return default_value<T>(); |  125   return default_value<T>(); | 
|  113 } |  126 } | 
|  114  |  127  | 
|  115 template<typename T> |  128 template<typename T> | 
|  116 void Config::Set(T* value) { |  129 void Config::Set(T* value) { | 
|  117   BaseOption*& it = options_[identifier<T>()]; |  130   BaseOption*& it = options_[identifier<T>()]; | 
|  118   delete it; |  131   delete it; | 
|  119   it = new Option<T>(value); |  132   it = new Option<T>(value); | 
|  120 } |  133 } | 
|  121  |  134  | 
|  122 }  // namespace webrtc |  135 }  // namespace webrtc | 
|  123  |  136  | 
|  124 #endif  // WEBRTC_COMMON_H_ |  137 #endif  // WEBRTC_COMMON_H_ | 
| OLD | NEW |