config/Param: add method With()

This commit is contained in:
Max Kellermann
2019-05-29 21:45:58 +02:00
parent b86d8d0cd8
commit fdbec694c6
6 changed files with 86 additions and 81 deletions

View File

@@ -54,6 +54,14 @@ struct ConfigData {
return list.empty() ? nullptr : &list.front();
}
template<typename F>
auto With(ConfigOption option, F &&f) const {
const auto *param = GetParam(option);
return param != nullptr
? param->With(std::forward<F>(f))
: f(nullptr);
}
gcc_pure
const char *GetString(ConfigOption option,
const char *default_value=nullptr) const noexcept;

View File

@@ -66,6 +66,19 @@ struct ConfigParam {
*/
[[noreturn]]
void ThrowWithNested() const;
/**
* Invoke a function with the configured value; if the
* function throws, call ThrowWithNested().
*/
template<typename F>
auto With(F &&f) const {
try {
return f(value.c_str());
} catch (...) {
ThrowWithNested();
}
}
};
#endif