1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 14:51:16 +02:00

Simplify RegisterCommand

This commit is contained in:
Eelco Dolstra 2025-05-05 08:28:12 +02:00
parent 4de7a986d4
commit e7c0906521
2 changed files with 8 additions and 7 deletions

View file

@ -14,12 +14,10 @@
namespace nix { namespace nix {
RegisterCommand::Commands * RegisterCommand::commands = nullptr;
nix::Commands RegisterCommand::getCommandsFor(const std::vector<std::string> & prefix) nix::Commands RegisterCommand::getCommandsFor(const std::vector<std::string> & prefix)
{ {
nix::Commands res; nix::Commands res;
for (auto & [name, command] : *RegisterCommand::commands) for (auto & [name, command] : RegisterCommand::commands())
if (name.size() == prefix.size() + 1) { if (name.size() == prefix.size() + 1) {
bool equal = true; bool equal = true;
for (size_t i = 0; i < prefix.size(); ++i) for (size_t i = 0; i < prefix.size(); ++i)

View file

@ -285,13 +285,16 @@ struct StorePathCommand : public StorePathsCommand
struct RegisterCommand struct RegisterCommand
{ {
typedef std::map<std::vector<std::string>, std::function<ref<Command>()>> Commands; typedef std::map<std::vector<std::string>, std::function<ref<Command>()>> Commands;
static Commands * commands;
static Commands & commands()
{
static Commands commands;
return commands;
}
RegisterCommand(std::vector<std::string> && name, std::function<ref<Command>()> command) RegisterCommand(std::vector<std::string> && name, std::function<ref<Command>()> command)
{ {
if (!commands) commands().emplace(name, command);
commands = new Commands;
commands->emplace(name, command);
} }
static nix::Commands getCommandsFor(const std::vector<std::string> & prefix); static nix::Commands getCommandsFor(const std::vector<std::string> & prefix);