config/Param: add method With()
This commit is contained in:
@@ -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;
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user