From e7c0906521f19b645b2972c38ce94fe32da84c8c Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 5 May 2025 08:28:12 +0200 Subject: [PATCH] Simplify RegisterCommand --- src/libcmd/command.cc | 4 +--- src/libcmd/include/nix/cmd/command.hh | 11 +++++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/libcmd/command.cc b/src/libcmd/command.cc index ffb745742..f7aeb739b 100644 --- a/src/libcmd/command.cc +++ b/src/libcmd/command.cc @@ -14,12 +14,10 @@ namespace nix { -RegisterCommand::Commands * RegisterCommand::commands = nullptr; - nix::Commands RegisterCommand::getCommandsFor(const std::vector & prefix) { nix::Commands res; - for (auto & [name, command] : *RegisterCommand::commands) + for (auto & [name, command] : RegisterCommand::commands()) if (name.size() == prefix.size() + 1) { bool equal = true; for (size_t i = 0; i < prefix.size(); ++i) diff --git a/src/libcmd/include/nix/cmd/command.hh b/src/libcmd/include/nix/cmd/command.hh index 45739fdb2..9139594ac 100644 --- a/src/libcmd/include/nix/cmd/command.hh +++ b/src/libcmd/include/nix/cmd/command.hh @@ -285,13 +285,16 @@ struct StorePathCommand : public StorePathsCommand struct RegisterCommand { typedef std::map, std::function()>> Commands; - static Commands * commands; + + static Commands & commands() + { + static Commands commands; + return commands; + } RegisterCommand(std::vector && name, std::function()> command) { - if (!commands) - commands = new Commands; - commands->emplace(name, command); + commands().emplace(name, command); } static nix::Commands getCommandsFor(const std::vector & prefix);