Skip to main content

Command Core Types & Factory

Core type definitions and factory function for the command architecture

Overview

The command core types and factory provide the foundational infrastructure for the Command Architecture. These are the building blocks that all concrete commands and command consumers depend on.

Source files:

  • src/Commands/Core/CommandResult.hpp — Result type for command execution
  • src/Commands/Core/CommandContext.hpp — Runtime context provided to commands
  • src/Commands/Core/CommandDescriptor.hpp — JSON-serializable command descriptors
  • src/Commands/Core/ICommand.hpp — Abstract base class for all commands
  • src/Commands/Core/CommandFactory.hpp / .cpp — Thin wrappers delegating to CommandRegistry
  • src/Commands/Core/CommandRegistry.hpp / .cpp — Singleton registry and registerTypedCommand<>

All types live in the commands namespace and have no Qt dependency.

Core Types

CommandResult

Returned by every command’s execute() and undo() methods:

struct CommandResult {
    bool success = false;
    std::string error_message;
    std::vector<std::string> affected_keys;

    static CommandResult ok(std::vector<std::string> keys = {});
    static CommandResult error(std::string msg);
};

CommandContext

Runtime environment passed to commands. Not serialized — constructed at execution time:

struct CommandContext {
    std::shared_ptr<DataManager> data_manager;
    std::map<std::string, std::string> runtime_variables;
    std::function<void(float)> on_progress;
};

CommandDescriptor

JSON-serializable description of a single command invocation. Uses reflect-cpp for serialization:

struct CommandDescriptor {
    std::string command_name;
    std::optional<rfl::Generic> parameters;
    std::optional<std::string> description;
};

CommandSequenceDescriptor

A sequence of commands with optional template variables for ${variable} substitution:

struct CommandSequenceDescriptor {
    std::optional<std::string> name;
    std::optional<std::string> version;
    std::optional<std::map<std::string, std::string>> variables;
    std::vector<CommandDescriptor> commands;
};

ICommand Base Class

All commands inherit from ICommand. The interface provides:

  • execute() — pure virtual, performs the command’s action
  • commandName() — pure virtual, returns a human-readable name
  • toJson() — pure virtual, serializes parameters to JSON
  • isUndoable() — opt-in, defaults to false
  • undo() — opt-in, defaults to returning an error
  • mergeId() / mergeWith() — for undo coalescing (Phase 5)

There is no separate IUndoableCommand hierarchy. Commands opt into undo support by overriding isUndoable() and undo().

Command Factory & Registry

CommandRegistry is the runtime lookup table. Libraries register commands at startup via registerTypedCommand<> in explicit dispatcher functions (see Command Registry).

CommandFactory exposes backward-compatible free functions that delegate to the registry:

std::unique_ptr<ICommand> createCommand(
    std::string const & name,
    rfl::Generic const & params);

std::vector<CommandInfo> getAvailableCommands();
std::optional<CommandInfo> getCommandInfo(std::string const & name);

createCommand() returns nullptr if the command name is unknown or parameter deserialization fails.

Tests

Test file: src/Commands/Core/CommandFactory.test.cpp

Tests cover:

  • CommandResult factory methods (ok, error)
  • CommandDescriptor and CommandSequenceDescriptor JSON round-trip serialization
  • Factory returns nullptr for unknown and empty command names
  • ICommand default behavior (not undoable, undo returns error, merge disabled)
  • Concrete StubCommand execution and name reporting

See Also