Skip to content

core: add shared_ptr overload for Bridge::switchBackend - #32

Open
Yaraslaut wants to merge 1 commit into
masterfrom
feature/22-switchbackend-shared-ptr
Open

core: add shared_ptr overload for Bridge::switchBackend#32
Yaraslaut wants to merge 1 commit into
masterfrom
feature/22-switchbackend-shared-ptr

Conversation

@Yaraslaut

Copy link
Copy Markdown
Member

Summary

  • Bridge::switchBackend only took a unique_ptr, so it could not express re-installing a backend instance the caller still owns (e.g. toggling between a long-lived remote backend and a local fallback as connectivity comes and goes).
  • Adds switchBackend(shared_ptr<IBackend>). The unique_ptr overload is now a template on the concrete backend type that converts and delegates to the shared_ptr overload — templating it (rather than taking unique_ptr<IBackend> directly) is required to avoid an overload-resolution ambiguity: a plain unique_ptr<IBackend> overload ties with the new shared_ptr<IBackend> overload for calls like switchBackend(std::make_unique<LocalBackend>(...)), since both require one equally-ranked user-defined conversion. The template version is an exact match instead.
  • Purely additive; existing unique_ptr call sites are unaffected (verified — the full suite already relies on this pattern in several tests).

Test plan

  • Added two tests to tests/test_switch_backend.cpp: one re-installs the same caller-owned shared_ptr backend after switching away (use_count() > 1 throughout, no ownership transfer, subsequent execute still works), one confirms the templated unique_ptr overload still works and delegates correctly.
  • Full suite: ./build/tests/morph_tests — all 813 test cases / 8289 assertions pass.

Closes #22

🤖 Generated with Claude Code

switchBackend previously only took a unique_ptr, so it could not express
re-installing a backend instance the caller still owns -- e.g. an app that
holds one long-lived remote backend for the process lifetime and toggles the
bridge between it and a local fallback as connectivity comes and goes had to
either give up ownership on the first switch (with nothing to switch back
to) or reconstruct the remote backend on every transition, discarding its
connection state.

Add switchBackend(shared_ptr<IBackend>); the unique_ptr overload now
converts and delegates to it. That overload is templated on the concrete
backend type rather than taking unique_ptr<IBackend> directly: a
non-template unique_ptr<IBackend> overload would tie with the new
shared_ptr<IBackend> overload for a unique_ptr<Concrete> argument (both are
one equally-ranked user-defined conversion), making every existing call
site -- e.g. switchBackend(std::make_unique<LocalBackend>(...)) --
ambiguous. The template version is an exact match and wins cleanly.

Closes #22

Signed-off-by: Yaraslau Tamashevich <yaraslau.tamashevich@gmail.com>
@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

switchBackend consumes the backend; no way to re-install a caller-owned instance

1 participant