blob: 1b104b06e1bcec03e83babac90c269f576251e71 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
// This source code is released into the public domain.
export module nihil.cli:registry;
import nihil.std;
namespace nihil {
export struct command;
///////////////////////////////////////////////////////////////////////
// Command registry storage. This is where command::command() registers
// itself when the global command objects are constructed at startup.
//
// Because we sometimes create stub commands dynamically, the registry
// storage is a list of shared_ptr<command>. The "real" commands (which
// refer to global objects) will be created with a null deleter so they
// are never deleted. This allows real and stub commands to mix in the
// same command_tree.
// Get the current registry.
auto get_registry() -> std::vector<std::shared_ptr<command>> &
{
static auto commands = std::vector<std::shared_ptr<command>>();
return commands;
}
// Register a new command.
auto register_command(command *cmd) noexcept -> void
{
auto constexpr null_deleter = [] (command const *) -> void {};
get_registry().emplace_back(cmd, null_deleter);
}
// Unregister a command. This is not very efficient, but it shouldn't usually be called
// except during testing.
auto unregister_command(command *cmd) noexcept -> void
{
auto ®istry = get_registry();
auto it = std::ranges::find_if(registry, [=] (auto c) -> bool {
return c.get() == cmd;
});
if (it != std::ranges::end(registry))
registry.erase(it);
}
} // namespace nihil
|