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 andregisterTypedCommand<>helpersrc/Commands/Core/CommandRegistry.cpp— singleton implementationsrc/Commands/register_core_commands.hpp— declaration of the built-in dispatchersrc/Commands/register_core_commands.cpp— registers core commandssrc/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):
- Each library defines a free function (e.g.,
register_core_commands()) that callsregisterTypedCommand<>for each command it owns. - The application’s composition root (
MainWindowconstructor, pipeline runners, test listeners) calls each library’s registration function once at startup. For example,MainWindowcallsregister_core_commands(),register_datasynthesizer_commands(), andWhiskerToolbox::Transforms::V2::register_transformsv2_commands(). CommandFactory.cppfree functions (isKnownCommandName,createCommandFromJson,getAvailableCommands,getCommandInfo) are thin wrappers delegating toCommandRegistry::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:
- Extracts
parameter_schemafromParamsviaextractParameterSchema<Params>(). - Wraps JSON deserialization (
rfl::json::read<Params, rfl::DefaultIfMissing>) andnormalizeJsonNumbersinto aCommandCreatorlambda. Omitted JSON fields receive the C++ default member initializer fromParams(same pattern as TransformsV2). - Calls
reg.registerCommand(...).
Adding a New Command
- Create the command class (subclass of
ICommand) and itsParamsstruct. - In the library’s registration function, add one call:
registerTypedCommand<MyNewCommand, MyNewCommand::Params>(
reg, "MyNewCommand",
CommandInfo{"MyNewCommand", "Category", "Description"});- If this is a new library that doesn’t yet have a registration function, create one and call it from
MainWindow(or a futureregister_all_commands.cppcomposition 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.