diff options
| author | Lexi Winter <lexi@le-fay.org> | 2025-06-29 00:42:31 +0100 |
|---|---|---|
| committer | Lexi Winter <lexi@le-fay.org> | 2025-06-29 00:42:31 +0100 |
| commit | d24315268c11d435bb9accbce87b7f46dda6ed3e (patch) | |
| tree | 66589cb6a15fa74d4b09683105c583e4a5c222b4 /nihil.cli/registry.cc | |
| parent | 7741a9698d29f79aca3e47495dcdf87c7a712f42 (diff) | |
| download | nihil-d24315268c11d435bb9accbce87b7f46dda6ed3e.tar.gz nihil-d24315268c11d435bb9accbce87b7f46dda6ed3e.tar.bz2 | |
cli: improve command dispatch a bit
Diffstat (limited to 'nihil.cli/registry.cc')
| -rw-r--r-- | nihil.cli/registry.cc | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/nihil.cli/registry.cc b/nihil.cli/registry.cc new file mode 100644 index 0000000..e35078d --- /dev/null +++ b/nihil.cli/registry.cc @@ -0,0 +1,57 @@ +/* + * This source code is released into the public domain. + */ + +module; + +#include <cstdio> +#include <exception> +#include <memory> +#include <vector> + +module nihil.cli; + +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::printf("%s\n", exc.what()); + std::exit(1); +} catch (...) { + std::printf("get_registered_commands(): unknown error\n"); + std::exit(1); +} + +/* + * 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::printf("%s\n", exc.what()); + std::exit(1); +} catch (...) { + std::printf("get_registered_commands(): unknown error\n"); + std::exit(1); +} + +/* + * Get the list of registered commands. + */ +auto get_registered_commands() -> std::span<std::shared_ptr<command_node>> +{ + return {get_registry()}; +} + +} // namespace nihil |
