aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.cli/registry.cc
blob: 0f0041b8d6681e4b1b7195544ceb7dfc7cedb427 (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
49
// This source code is released into the public domain.
module nihil.cli;

import nihil.std;

namespace nihil {

/*
 * Get the registry storage.  Because this is called from global ctors,
 * it handles exceptions itself.
 */
auto get_registry() noexcept -> std::vector<std::shared_ptr<command_node>> &
try {
	static auto commands = std::vector<std::shared_ptr<command_node>>();
	return commands;
} catch (std::exception const &exc) {
	std::println(std::cerr, "{}", exc.what());
	std::exit(1); // NOLINT
} catch (...) {
	std::println(std::cerr, "get_registered_commands(): unknown error\n");
	std::exit(1); // NOLINT
}

/*
 * Register a new command.
 */
auto register_command(command *cmd) noexcept -> void
try {
	auto null_deleter = [] (command_node const *) -> void {};

	auto &commands = get_registry();
	commands.emplace_back(cmd, null_deleter);
} catch (std::exception const &exc) {
	std::println(std::cerr, "{}", exc.what());
	std::exit(1); // NOLINT
} catch (...) {
	std::println(std::cerr, "get_registered_commands(): unknown error\n");
	std::exit(1); // NOLINT
}

/*
 * Get the list of registered commands.
 */
auto get_registered_commands() -> std::span<std::shared_ptr<command_node>>
{
	return {get_registry()};
}

} // namespace nihil