Skip to content

Commit

Permalink
Simplify double-nested lambdas in command selection. (#1526)
Browse files Browse the repository at this point in the history
The lambda 'find_command' was potentially confusable with the function that implements `vcpkg find`. Moreover, the capture of the whole VcpkgCmdArguments when only the desired command name was needed was overkill. The simple for loop is shorter, easier to debug, and avoids needing to form the complicated 'decltype' to return nullptr.
  • Loading branch information
BillyONeal authored Dec 16, 2024
1 parent 074906a commit 7dbf07e
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/vcpkg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ namespace
return false;
}

template<class CommandRegistrationKind>
static const CommandRegistrationKind* choose_command(const std::string& command_name,
View<CommandRegistrationKind> command_registrations)
{
for (const auto& command_registration : command_registrations)
{
if (Strings::case_insensitive_ascii_equals(command_registration.metadata.name, command_name))
{
return &command_registration;
}
}

return nullptr;
}

void inner(const Filesystem& fs, const VcpkgCmdArguments& args, const BundleSettings& bundle)
{
// track version on each invocation
Expand All @@ -104,24 +119,9 @@ namespace
Checks::exit_fail(VCPKG_LINE_INFO);
}

static const auto find_command = [&](auto&& commands) {
auto it = Util::find_if(commands, [&](auto&& commandc) {
return Strings::case_insensitive_ascii_equals(commandc.metadata.name, args.get_command());
});
using std::end;
if (it != end(commands))
{
return &*it;
}
else
{
return static_cast<decltype(&*it)>(nullptr);
}
};

get_global_metrics_collector().track_bool(BoolMetric::DetectedContainer, detect_container(fs));

if (const auto command_function = find_command(basic_commands))
if (const auto command_function = choose_command(args.get_command(), basic_commands))
{
get_global_metrics_collector().track_string(StringMetric::CommandName, command_function->metadata.name);
return command_function->function(args, fs);
Expand All @@ -133,15 +133,15 @@ namespace

fs.current_path(paths.root, VCPKG_LINE_INFO);

if (const auto command_function = find_command(paths_commands))
if (const auto command_function = choose_command(args.get_command(), paths_commands))
{
get_global_metrics_collector().track_string(StringMetric::CommandName, command_function->metadata.name);
return command_function->function(args, paths);
}

Triplet default_triplet = vcpkg::default_triplet(args, paths.get_triplet_db());
Triplet host_triplet = vcpkg::default_host_triplet(args, paths.get_triplet_db());
if (const auto command_function = find_command(triplet_commands))
if (const auto command_function = choose_command(args.get_command(), triplet_commands))
{
get_global_metrics_collector().track_string(StringMetric::CommandName, command_function->metadata.name);
return command_function->function(args, paths, default_triplet, host_triplet);
Expand Down

0 comments on commit 7dbf07e

Please sign in to comment.