// A class for getting system properties with fallback lookup support. Different from // android::base::GetProperty, this class is mockable. class SystemProperties { public:
SystemProperties() = default;
SystemProperties(const SystemProperties& other) = default;
SystemProperties& operator=(const SystemProperties& other) = default;
SystemProperties(SystemProperties&& other) = default;
SystemProperties& operator=(SystemProperties&& other) = default; virtual ~SystemProperties() = default;
// Returns the current value of the system property `key`, or `default_value` if the property // doesn't have a value.
std::string Get(const std::string& key, const std::string& default_value) const{
std::string value = GetProperty(key); if (!value.empty()) { return value;
} return default_value;
}
// Same as above, but allows specifying one or more fallback keys. The last argument is a string // default value that will be used if none of the given keys has a value. // // Usage: // // Look up for "key_1", then "key_2", then "key_3". If none of them has a value, return "default": // Get("key_1", "key_2", "key_3", /*default_value=*/"default") template <typename... Args>
std::string Get(const std::string& key, const std::string& fallback_key, Args... args) const { return Get(key, Get(fallback_key, args...));
}
// Returns the current value of the system property `key` with zero or more fallback keys, or an // empty string if none of the given keys has a value. // // Usage: // // Look up for "key_1". If it doesn't have a value, return an empty string: // GetOrEmpty("key_1") // // Look up for "key_1", then "key_2", then "key_3". If none of them has a value, return an empty // string: // GetOrEmpty("key_1", "key_2", "key_3") template <typename... Args>
std::string GetOrEmpty(const std::string& key, Args... fallback_keys) const { return Get(key, fallback_keys..., /*default_value=*/"");
}
// Returns the current value of the boolean system property `key`, or `default_value` if the // property doesn't have a value. See `android::base::ParseBool` for how the value is parsed. bool GetBool(const std::string& key, bool default_value) const {
android::base::ParseBoolResult result = android::base::ParseBool(GetProperty(key)); if (result != android::base::ParseBoolResult::kError) { return result == android::base::ParseBoolResult::kTrue;
} return default_value;
}
// Same as above, but allows specifying one or more fallback keys. The last argument is a bool // default value that will be used if none of the given keys has a value. // // Usage: // // Look up for "key_1", then "key_2", then "key_3". If none of them has a value, return true: // Get("key_1", "key_2", "key_3", /*default_value=*/true) template <typename... Args> bool GetBool(const std::string& key, const std::string& fallback_key, Args... args) const { return GetBool(key, GetBool(fallback_key, args...));
}
protected: // The single source of truth of system properties. Can be mocked in unit tests. virtual std::string GetProperty(const std::string& key) const { return android::base::GetProperty(key, /*default_value=*/"");
}
};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.