Skip to content

Latest commit

 

History

History
322 lines (211 loc) · 16.3 KB

command_registry.md

File metadata and controls

322 lines (211 loc) · 16.3 KB

command_registry.hpp

CommandRegistry — the binding from a marker's ID to the code that performs it.

This header declares 3 types (14 members) and 1 constant.

Extracted from include/shulib/runner/command_registry.hpp — this page is that header's documentation, reformatted, so it cannot disagree with the code. Prose about how to think about the API lives in How it works; worked recipes live in Recipes; this page is the complete, mechanical list of what exists.

Contents

kMaxCommands

inline constexpr std::size_t kMaxCommands = 32

The most command ids one registry may hold. A DESIGN constant, on kMaxWaypoints's reasoning (B6): a robot with more than 32 distinct scoring capabilities is not a robot this bound is the problem for. Nothing measured says 32; it is the fixed capacity that keeps the table allocation-free.

constant, declared at include/shulib/runner/command_registry.hpp:81.

struct Handler

struct Handler

One bound action: a plain function pointer plus the opaque context it is called with — the type-erased, allocation-free pair std::function would otherwise be used for (file banner). fn receives the whole config::Marker, not just its args, so a handler can read its own id (one function may serve several related ids) and its typed arguments without a second lookup. It returns a manipulation::MechanismOutcome: anything other than Succeeded stops the route under the runner's error policy, and the vocabulary has no conversion to bool, so a failed grab can never read as success by accident (the T2 guarantee).

struct, declared at include/shulib/runner/command_registry.hpp:91.

Handler::fn

manipulation::MechanismOutcome (*fn)(void* ctx, const config::Marker& marker) = nullptr

The action. Never null in a registered Handler (a precondition of on).

field, declared at include/shulib/runner/command_registry.hpp:93.

Handler::ctx

void* ctx = nullptr

Whatever fn needs — the address of the caller's callable for the template on() overload, or anything at all for the explicit one. NON-OWNING: the pointee must outlive the registry. May be null when fn needs no context.

field, declared at include/shulib/runner/command_registry.hpp:97.

struct CommandEntry

struct CommandEntry

One registration, as the registry stores it: the id, how many typed args a marker carrying it must supply, and the bound action. The id is a fixed-width NUL-terminated buffer rather than a std::string_view into the caller's memory, because a registration commonly reads on("intake_in", …) from a string literal but just as legitimately from a buffer a parser filled — and a table of views into somebody else's parse buffer is a dangling read waiting for G3.

struct, declared at include/shulib/runner/command_registry.hpp:107.

CommandEntry::id

char id[config::kMaxMarkerIdBytes]{}

The id text, NUL-terminated (commandIdValid at registration, so a terminator always fits). Read it through idView() rather than directly.

field, declared at include/shulib/runner/command_registry.hpp:110.

CommandEntry::argCount

std::uint8_t argCount = 0

How many typed args a marker with this id carries — DECLARED at registration, and published in the manifest so a picker can prompt for exactly that many. The library does not enforce it on a marker at run time (an unenforceable claim about someone else's handler); it is the contract the picker and the handler share.

field, declared at include/shulib/runner/command_registry.hpp:115.

CommandEntry::handler

Handler handler{}

The bound action.

field, declared at include/shulib/runner/command_registry.hpp:117.

CommandEntry::doc

const char* doc = nullptr

What the id does and what its args mean, in one line, published in the manifest's doc field (file banner). NON-OWNING — the text must outlive the registry; free of " and \ (the manifest's rule). nullptr = no doc: the manifest's "", as before it existed. (APPENDED at chunk MECH-A4 — F8's "new registry members", API 2.5.)

field, declared at include/shulib/runner/command_registry.hpp:122.

CommandEntry::idView

[[nodiscard]] constexpr std::string_view idView() const noexcept

The id as text.

function, declared at include/shulib/runner/command_registry.hpp:125.

class CommandRegistry

class CommandRegistry

The id → action table a team fills once at startup, and PathRunner reads while a route runs (file banner). Fixed capacity, no heap, no std::function; every registration error is a precondition, because registration is code.

class, declared at include/shulib/runner/command_registry.hpp:133.

CommandRegistry::CommandRegistry

CommandRegistry() = default

An empty registry. The built-in ids (config::kBuiltinCommands) are live from here on without appearing in this table at all — they belong to the runner, not to a team.

function, declared at include/shulib/runner/command_registry.hpp:137.

CommandRegistry::on

void on(std::string_view id, Handler handler, std::uint8_t argCount = 0)

Bind id to handler; argCount is how many typed args a marker with this id carries (published in the manifest; 0 for the common case). Preconditions, every one of them a programming error rather than a datum (file banner): id is config::commandIdValid; it is not one of config::kBuiltinCommands; it is not already registered; handler.fn is not null; argCount <= config::kMaxMarkerArgs; and the table has room (kMaxCommands).

function, declared at include/shulib/runner/command_registry.hpp:146.

CommandRegistry::on (overload 2)

template <typename Callable> void on(std::string_view id, Callable& callable, std::uint8_t argCount = 0)

Bind id to any callable, adapting its return type exactly as Routine::then() does: * void — the action always succeeds, * bool — false fails (reported as MechanismOutcome::Unconfirmed: the handler ran and the task did not happen, which is precisely what that verdict means; claiming TimedOut or Stalled would invent a pathology the bool never stated), * manipulation::MechanismOutcome — passed through unchanged, so only Succeeded continues the route and an Unconfirmed grab can never read as success. A callable returning anything else is a compile error naming the three. (control::ExitReason, which then() also accepts, is deliberately NOT accepted here: mapping a motion verdict onto a mechanism verdict would have to invent a meaning for Running, and a handler that wraps a facade verb can return the outcome it means.) The callable may take the marker or take nothing — on("lift_to_level", liftTo) where liftTo reads marker.args[0], or on("intake_in", intakeIn) where it does not. NON-OWNING: a pointer to callable is stored, so it must outlive the registry. The Callable& parameter is what enforces that — a temporary lambda does not bind to it, so the dangling registration cannot be written (file banner).

function, declared at include/shulib/runner/command_registry.hpp:189.

CommandRegistry::on (overload 3)

void on(std::string_view id, Handler handler, std::uint8_t argCount, const char* doc)

Bind id to handler exactly as the three-argument form does, and give it a one-line doc the manifest publishes (file banner) — how runner::MechanismRoutes registers every derived mechanism id. Every precondition of the three-argument form, and: doc is null or free of " and \ (the manifest's own rule, checked HERE so a bad doc fails at registration, not when the manifest is written). NON-OWNING: doc must outlive the registry. (APPENDED at chunk MECH-A4 — F8's "new registry members", API 2.5.)

function, declared at include/shulib/runner/command_registry.hpp:199.

CommandRegistry::find

[[nodiscard]] const Handler* find(std::string_view id) const noexcept

The handler bound to id, or NULLPTR when nothing is. A null return is the whole error channel on purpose (IRouteSource::route()'s convention): "no handler for this id" is a normal thing for a runner to find, and it answers it with a Warn and a skip. Built-in ids are NOT in this table and this returns nullptr for them; the runner checks the built-ins first.

function, declared at include/shulib/runner/command_registry.hpp:214.

CommandRegistry::count

[[nodiscard]] std::size_t count() const noexcept

How many ids are registered (built-ins not included — they are never in this table).

function, declared at include/shulib/runner/command_registry.hpp:224.

CommandRegistry::ids

[[nodiscard]] std::span<const CommandEntry> ids() const noexcept

Every registration, in REGISTRATION ORDER — which is the order the manifest publishes them in, so a diff of two manifests reads as a diff of two on() sequences.

function, declared at include/shulib/runner/command_registry.hpp:228.

Design commentary, from the header

The header opens with the reasoning behind these shapes. It is reproduced here in full because a reference that only lists signatures teaches nobody why.

The header’s own reasoning — 60 lines, click to expand

 CommandRegistry — the binding from a marker's ID to the code that performs it
 (chunk G2, 2026-09-18; master plan §16.3; docs/vexbuilder-integration.md §4.2).

 ── Why this table is the keystone of the no-code story ─────────────────────────────
 A Route (config/route.hpp) says WHERE the robot goes and WHAT to do on the way, and the
 "what" is an ID — never code. The legacy planner embedded C++ snippets in its rows
 (`code_template: "mech.intakeIn();"`), which is rejected outright (ruling B5): data that
 can execute is a second, unreviewable source file. This table is the other half of that
 decision. A coder writes ONE line per capability,

     runner::CommandRegistry registry;
     registry.on("intake_in", intakeIn);      // intakeIn is a callable that OUTLIVES this

 and from then on a non-coder authoring a path in VexBuilder picks "intake_in" from a list
 the library itself publishes (runner/manifest.hpp). The auton is data a non-coder writes;
 the handlers are code the coders maintain.

 ── No std::function, no heap, no std::string ───────────────────────────────────────
 A V5 brain has 128 KB of user RAM and this table may be built during an autonomous
 period, so every allocation is a risk taken for nothing. A Handler is therefore a plain
 function pointer plus an opaque context pointer — the C idiom, chosen because it is the
 only one that is both type-erased and allocation-free. The template `on()` overload
 builds that pair from any callable and stores a NON-OWNING pointer to it: the callable
 must outlive the registry, the same ownership rule `Routine::then()`'s action and
 `StaticRoutes`'s span already carry. That is enforced by the signature, not by a comment
 — the overload takes `Callable&`, so `on("x", []{ ... })` (a temporary lambda) does not
 compile, and the lifetime bug it would cause cannot be written.

 ── A handler runs SYNCHRONOUSLY, on the caller's task, between legs ────────────────
 PathRunner calls a handler from inside run(), after the leg it hangs off has settled, and
 waits for it to return. A handler that blocks for two seconds blocks the route for two
 seconds. That is the right default for "close the clamp, then drive away" and the wrong
 one for "spin the intake for the rest of the match". Since the mechanism core (chunk
 MECH-A4) a robot's DECLARED mechanisms need no hand-written handler at all: their route
 ids are derived from the robot's list and registered in ONE call
 (runner/mechanism_routes.hpp) — each marker blocks through a Routine step until its
 verdict, and what goes on after it (an arm's hold, a flywheel's spin) is the mechanism
 service's, which ticks every mechanism inside every shulib wait. A step that must start
 mid-leg is a Routine's partway trigger since STEP1-B (chassis/routine.hpp; route markers stay
 blocking, F8 unchanged); for an F1 operation the idiom is unchanged: a handler that STARTS it and a later marker that
 confirms it (manipulation/mechanism_op.hpp).

 ── A doc per id (chunk MECH-A4, F8-additive) ───────────────────────────────────────
 An entry may carry a one-line `doc` — what the id does and what its args mean — which the
 manifest publishes for a picker to show beside the id ("args[0] = volts, args[1] = how
 long, ms" instead of a bare "2 args"). The four-argument on() sets it; an entry without one
 publishes `"doc":""`, so a registry with no docs writes exactly the bytes it wrote before.

 ── What is a programming error here, and what is data ──────────────────────────────
 Registration is code, so everything wrong with a registration is a PRECONDITION: a
 duplicate id (two handlers for one id is not a merge, it is a typo), a built-in id
 (library-owned — ruling R3), an id outside the vocabulary, a null function pointer, more
 than kMaxCommands. An UNKNOWN id at RUN time is the opposite: it is data, the runner logs
 one Warn and carries on (vexbuilder-integration.md's guarantee), and nothing about it
 reaches this class.

 FROZEN: F8 (docs/roadmap.md), pinned by test/f8_signature_pin_test.cpp.

 PROS-free, sim-free, allocation-free. Single-task, like everything it is called from.