11
Command, undo, and queues
A warehouse editor whose operations are command objects, with undo/redo and a FIFO command queue.
A warehouse inventory editor whose operations are command objects:
- Command objects.
AddStockCommandandMoveItemCommandundo with a clean arithmetic inverse;AdjustQuantityCommandandCancelLineCommandundo from a snapshot captured insideexecute()(not the constructor, since a queued command runs later than it is built). CommandHistoryis the undo/redo stack; a fresh execute clears the redo branch.CommandQueueis a FIFO batch processor that can keep a batch undoable.RestockLogFnis thestd::functionshortcut for a queued action with nothing to undo.
The C++23 codetabs path uses std::move_only_function so the command slot can
hold a move-only capture; the C++20 path uses std::function.
Build, run, observe
./scripts/build.sh cpp23
./scripts/verify.sh cpp23 # 20/20 tests pass
./examples/11-command/demo.sh cpp23
The demo executes a mix of commands, undoes and redoes them, drains a FIFO
batch, and writes restock-log entries. Chapter 11 walks through the two undo
strategies and when a plain callable beats a full command object. Builds
standalone and under the cpp20 preset (20 tests).