Skip to content

Visit the client's roles through one helper - #284

Merged
kahrendt merged 2 commits into
mainfrom
trim-role-boilerplate
Nov 21, 2026
Merged

kahrendt merged 2 commits into
mainfrom
trim-role-boilerplate

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Five sites in client.cpp each spelled out the seven roles under their own #ifdef. One private helper now holds that list, and the sites pass it a visitor. No behavior change. Net -88 lines.

Change

  • for_each_role(): a private member template that calls visit(impl, role) for each added role, in the order player, controller, metadata, color, artwork, visualizer, source. It is the one place in client.cpp that lists the roles for these sites.
  • Converted sites: catch_up_roles, cleanup_connection_state, build_client_hello, apply_role_removals and build_client_state. Each visits the same roles in the same order with the same calls as before. role_removed() folds into apply_role_removals.
  • take_stream_event(): a file-local helper for the generation check and teardown catch-up the four stream-event cases repeated.
  • Public header: client.h gains the one private template declaration.

Reviewer notes

  • Two sites skip roles that lack a member through if constexpr (requires { ... }): end_stream (only the source has one) and build_state_fields (player, artwork, visualizer, source). A rename of either cannot pass unnoticed: end_stream has three other callers that name it on the source role, and the role tests call build_state_fields directly on all four roles.
  • The role drains in drain_inbox keep their own list. Nothing else names needs_drain(), so a probe there would skip a role whose method was renamed instead of failing to compile.
  • stop_role_threads() keeps its own list. It stops the roles in a different order, and converting it would reorder the stops.
  • The seven add_*() bodies are unchanged. A shared helper for them saved about six lines and cost a second template in the public header.
  • The "Role left its connection" log line is instantiated per role now, where one shared function held it before: a small flash cost on ESP, not measured.

Tests

  • No tests added or changed; the existing lifecycle and role tests exercise every converted site.
  • The library alone was built with warnings on with every role off, source only, everything but source, player only, controller only, and Opus off. The first all-off build caught an unused-function warning, fixed here.
  • Not run locally: an ESP build and ThreadSanitizer; CI covers both.

Six sites in client.cpp each spelled out the seven roles under their
own #ifdef. for_each_role() holds that list once and hands each added
role's Impl to a visitor, in the order the sites already used.
take_stream_event() does the same for the generation check the four
stream events repeated.

stop_role_threads() keeps its own list: it stops the roles in a
different order.
@kahrendt
kahrendt marked this pull request as ready for review November 21, 2026 14:34
@kahrendt
kahrendt requested an adaptive review from Copilot November 21, 2026 14:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The change still has three active moderate findings because requires checks allow missing role methods to compile as silent no-ops, risking undetected regressions in draining, state reporting, and stream teardown.

3 open issues
What changed in this PR

This pull request centralizes repeated client-role traversal in SendspinClient, preserving role order and existing behavior while reducing duplicated conditional logic.

Changes:

  • Added for_each_role() to centralize compile-gated visitation of all seven roles.
  • Converted lifecycle, inbox-draining, hello/state construction, cleanup, and role-removal paths to use the shared visitor.
  • Extracted shared stream-event generation and teardown handling into take_stream_event().
File Description
src/​client.cpp Implements centralized role visitation and applies it across client lifecycle, inbox, state, hello, cleanup, removal, and stream-event paths.
include/​sendspin/​client.h Declares and documents the private role-visitation helper template.

Review effort: Lite (adaptive)

Comment thread src/client.cpp Resolved
Comment thread src/client.cpp Resolved
Comment thread src/client.cpp Resolved
for_each_role() reached the role drains through a requires probe, so a
role whose needs_drain() was renamed would be skipped instead of failing
to compile. Nothing else names needs_drain(), so the site lists the six
roles again.
@kahrendt
kahrendt enabled auto-merge (squash) November 21, 2026 15:21
@kahrendt
kahrendt merged commit f49a5bf into main Nov 21, 2026
16 checks passed
@kahrendt
kahrendt deleted the trim-role-boilerplate branch November 21, 2026 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants