Skip to main content

commands::CommandRegistry

commands::CommandRegistry

Singleton registry mapping command name strings to creator functions and metadata. More…

#include <CommandRegistry.hpp>

Public Functions

Name
CommandRegistry & instance()
Access the singleton CommandRegistry instance.
void registerCommand(std::string name, CommandCreator creator, CommandInfo info)
Register a command with its creator function and metadata.
bool isKnown(std::string const & name) const
Check if a command name has been registered.
std::unique_ptr< ICommand > create(std::string const & name, std::string const & params_json) const
Create a command instance from its name and a normalized JSON parameter string.
std::vector< CommandInfo > availableCommands() const
Return metadata for all registered commands.
std::optional< CommandInfo > commandInfo(std::string const & name) const
Return metadata for a single command by name.
std::size_t size() const
Return the number of registered commands.
void clear()
Remove all registered commands.
CommandRegistry(CommandRegistry const & ) =delete
CommandRegistry & operator=(CommandRegistry const & ) =delete
CommandRegistry(CommandRegistry && ) =delete
CommandRegistry & operator=(CommandRegistry && ) =delete

Detailed Description

class commands::CommandRegistry;

Singleton registry mapping command name strings to creator functions and metadata.

Note: Thread-safety: Registration is expected to happen once at startup, before any concurrent access. Query methods are safe for concurrent reads after registration is complete.

Replaces the hardcoded if-else chain in CommandFactory. Libraries register their commands via explicit dispatcher functions (e.g., register_core_commands()) that call registerCommand() on this singleton.

Public Functions Documentation

function instance

static CommandRegistry & instance()

Access the singleton CommandRegistry instance.

Return: Reference to the process-wide CommandRegistry singleton.

function registerCommand

void registerCommand(
    std::string name,
    CommandCreator creator,
    CommandInfo info
)

Register a command with its creator function and metadata.

Parameters:

  • name Unique command name string (must not be empty).
  • creator Callable that deserializes JSON and constructs the command.
  • info Static metadata for the command.

Precondition:

  • name must not already be registered (asserts in debug builds).
  • name must not be empty (asserts in debug builds).

function isKnown

bool isKnown(
    std::string const & name
) const

Check if a command name has been registered.

Parameters:

  • name Command name to query.

Return: True if name is registered, false otherwise.

function create

std::unique_ptr< ICommand > create(
    std::string const & name,
    std::string const & params_json
) const

Create a command instance from its name and a normalized JSON parameter string.

Parameters:

  • name Registered command name.
  • params_json Normalized JSON parameter string for the command.

Return: The constructed command, or nullptr if name is unknown or params_json fails to parse.

function availableCommands

std::vector< CommandInfo > availableCommands() const

Return metadata for all registered commands.

Return: Vector of CommandInfo for every registered command.

function commandInfo

std::optional< CommandInfo > commandInfo(
    std::string const & name
) const

Return metadata for a single command by name.

Parameters:

  • name Registered command name to look up.

Return: The CommandInfo for name, or std::nullopt if the name is not registered.

function size

std::size_t size() const

Return the number of registered commands.

Return: Count of registered command entries.

function clear

void clear()

Remove all registered commands.

Intended for test isolation only — not for production use.

function CommandRegistry

CommandRegistry(
    CommandRegistry const & 
) =delete

function operator=

CommandRegistry & operator=(
    CommandRegistry const & 
) =delete

function CommandRegistry

CommandRegistry(
    CommandRegistry && 
) =delete

function operator=

CommandRegistry & operator=(
    CommandRegistry && 
) =delete

Updated on 2026-11-13 at 20:25:42 +0000