Adds enum field trial parser.
Removed the need to create a custom parser function and reuses some of the code to reduce binary overhead of enums. Bug: webrtc:9346 Change-Id: I51c9da713ed5456a86a2afbcf0991477bb83b894 Reviewed-on: https://webrtc-review.googlesource.com/83623 Reviewed-by: Stefan Holmer <stefan@webrtc.org> Commit-Queue: Sebastian Jansson <srte@webrtc.org> Cr-Commit-Position: refs/heads/master@{#23752}
This commit is contained in:
committed by
Commit Bot
parent
b3f5aed433
commit
2c74d85c16
@ -12,6 +12,8 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <initializer_list>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include "absl/types/optional.h"
|
||||
|
||||
@ -83,6 +85,47 @@ class FieldTrialParameter : public FieldTrialParameterInterface {
|
||||
T value_;
|
||||
};
|
||||
|
||||
class AbstractFieldTrialEnum : public FieldTrialParameterInterface {
|
||||
public:
|
||||
AbstractFieldTrialEnum(std::string key,
|
||||
int default_value,
|
||||
std::map<std::string, int> mapping);
|
||||
~AbstractFieldTrialEnum() override;
|
||||
AbstractFieldTrialEnum(const AbstractFieldTrialEnum&);
|
||||
|
||||
protected:
|
||||
bool Parse(absl::optional<std::string> str_value) override;
|
||||
|
||||
protected:
|
||||
int value_;
|
||||
std::map<std::string, int> enum_mapping_;
|
||||
std::set<int> valid_values_;
|
||||
};
|
||||
|
||||
// The FieldTrialEnum class can be used to quickly define a parser for a
|
||||
// specific enum. It handles values provided as integers and as strings if a
|
||||
// mapping is provided.
|
||||
template <typename T>
|
||||
class FieldTrialEnum : public AbstractFieldTrialEnum {
|
||||
public:
|
||||
FieldTrialEnum(std::string key,
|
||||
T default_value,
|
||||
std::map<std::string, T> mapping)
|
||||
: AbstractFieldTrialEnum(key,
|
||||
static_cast<int>(default_value),
|
||||
ToIntMap(mapping)) {}
|
||||
T Get() const { return static_cast<T>(value_); }
|
||||
operator T() const { return Get(); }
|
||||
|
||||
private:
|
||||
static std::map<std::string, int> ToIntMap(std::map<std::string, T> mapping) {
|
||||
std::map<std::string, int> res;
|
||||
for (const auto& it : mapping)
|
||||
res[it.first] = static_cast<int>(it.second);
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
// This class uses the ParseTypedParameter function to implement an optional
|
||||
// parameter implementation that can default to absl::nullopt.
|
||||
template <typename T>
|
||||
|
||||
Reference in New Issue
Block a user