From 5469c71a824880eb646a0003cb001c58f24f9cce Mon Sep 17 00:00:00 2001 From: Alex Turbov Date: Fri, 30 Jul 2021 03:49:56 +0300 Subject: Refactor: Simplify `GetValue()` function calls --- Source/cmCMakeHostSystemInformationCommand.cxx | 42 +++++++++++++++++--------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/Source/cmCMakeHostSystemInformationCommand.cxx b/Source/cmCMakeHostSystemInformationCommand.cxx index 1464220..b2c4534 100644 --- a/Source/cmCMakeHostSystemInformationCommand.cxx +++ b/Source/cmCMakeHostSystemInformationCommand.cxx @@ -439,6 +439,21 @@ cm::optional GetValue(cmExecutionStatus& status, return {}; } #endif + +cm::optional GetValueChained() +{ + return {}; +} + +template +cm::optional GetValueChained(GetterFn current, Next... chain) +{ + auto value = current(); + if (value.has_value()) { + return value; + } + return GetValueChained(chain...); +} // END Private functions } // anonymous namespace @@ -475,24 +490,21 @@ bool cmCMakeHostSystemInformationCommand(std::vector const& args, result_list += DELIM[!result_list.empty()]; auto const& key = args[i]; - auto value = GetValue(info, key); - if (!value) { + // clang-format off + auto value = + GetValueChained( + [&]() { return GetValue(info, key); } +#ifdef __linux__ + , [&]() { return GetValue(status, key, variable); } +#endif #ifdef HAVE_VS_SETUP_HELPER - value = GetValue(status, key); - if (!value) { - status.SetError("does not recognize " + key); - return false; - } -#elif defined(__linux__) - value = GetValue(status, key, variable); - if (!value) { - status.SetError("does not recognize " + key); - return false; - } -#else + , [&]() { return GetValue(status, key); } +#endif + ); + // clang-format on + if (!value) { status.SetError("does not recognize " + key); return false; -#endif } result_list += value.value(); } -- cgit v0.12