Skip to main content

Command Registry

Overview

CommandRegistry is a singleton that maps command name strings to creator functions and CommandInfo metadata. It replaces the former hardcoded if-else chain in CommandFactory.cpp, enabling domain libraries to contribute commands without modifying a central switch statement.

Source files:

  • src/Commands/Core/CommandRegistry.hpp — class declaration and registerTypedCommand<> helper
  • src/Commands/Core/CommandRegistry.cpp — singleton implementation
  • src/Commands/register_core_commands.hpp — declaration of the built-in dispatcher
  • src/Commands/register_core_commands.cpp — registers core commands
  • src/DataSynthesizer/register_datasynthesizer_commands.hpp / .cpp — registers DataSynthesizer-owned commands (e.g., SynthesizeData)
  • src/TransformsV2/register_transformsv2_commands.hpp / .cpp — registers TransformsV2-owned commands (e.g. RunTransformsV2PipelineAtTime; tracked in #267)

Design

The registry follows the Explicit Dispatcher pattern (see Command Architecture):

  1. Each library defines a free function (e.g., register_core_commands()) that calls registerTypedCommand<> for each command it owns.
  2. The application’s composition root (MainWindow constructor, pipeline runners, test listeners) calls each library’s registration function once at startup. For example, MainWindow calls register_core_commands(), register_datasynthesizer_commands(), and WhiskerToolbox::Transforms::V2::register_transformsv2_commands().
  3. CommandFactory.cpp free functions (isKnownCommandName, createCommandFromJson, getAvailableCommands, getCommandInfo) are thin wrappers delegating to CommandRegistry::instance().

This avoids --whole-archive linker flags and keeps the dependency direction clean: domain libraries depend on Commands, and the executable depends on both.

API

CommandRegistry

Method Description
instance() Static accessor for the singleton.
registerCommand(name, creator, info) Register a command. Asserts on empty or duplicate name.
isKnown(name) Check if a command name is registered.
create(name, params_json) Create a command from name + JSON params; returns nullptr on failure.
availableCommands() Return vector<CommandInfo> for all registered commands.
commandInfo(name) Return optional<CommandInfo> for one command.
size() Number of registered commands.
clear() Remove all registrations (test isolation only).

registerTypedCommand<CommandT, Params>

template<typename CommandT, typename Params>
void registerTypedCommand(CommandRegistry & reg, std::string name, CommandInfo info);

Convenience template that:

  1. Extracts parameter_schema from Params via extractParameterSchema<Params>().
  2. Wraps JSON deserialization (rfl::json::read<Params, rfl::DefaultIfMissing>) and normalizeJsonNumbers into a CommandCreator lambda. Omitted JSON fields receive the C++ default member initializer from Params (same pattern as TransformsV2).
  3. Calls reg.registerCommand(...).

Adding a New Command

  1. Create the command class (subclass of ICommand) and its Params struct.
  2. In the library’s registration function, add one call:
registerTypedCommand<MyNewCommand, MyNewCommand::Params>(
    reg, "MyNewCommand",
    CommandInfo{"MyNewCommand", "Category", "Description"});
  1. If this is a new library that doesn’t yet have a registration function, create one and call it from MainWindow (or a future register_all_commands.cpp composition root).

Test Registration

Test binaries that execute commands (via createCommandFromJson, executeSequence, etc.) must register all required command libraries before tests run. A reusable Catch2 event listener is provided at tests/shared/RegisterCommandsListener.cpp (calls register_core_commands(), register_datasynthesizer_commands(), and register_transformsv2_commands()). Test executables using this listener must link DataSynthesizer and TransformsV2. Add the listener to your test executable’s source list in CMake:

add_executable(my_test
    ${CMAKE_SOURCE_DIR}/tests/shared/RegisterCommandsListener.cpp
    # ... test sources ...
)

Thread Safety

Registration is expected to happen once at startup before any concurrent access. After registration is complete, all query methods (isKnown, create, availableCommands, commandInfo, size) are safe for concurrent reads.