Skip to content

Conversation

@ahigerd
Copy link
Contributor

@ahigerd ahigerd commented Aug 8, 2025

This is the first of a series of patches that intend to refactor the way mGBA handles popup windows.

Second PR: ahigerd#3
Third PR: ahigerd#4
Fourth PR: ahigerd#5

Description

CorePointer is a weak pointer to a boxed shared pointer to CoreController with change callbacks. That is:

  • CorePointerSource mostly-transparently wraps around shared_ptr<CoreController>
  • CorePointer acts as a CoreController smart pointer, but it returns the pointer contained by its associated CorePointerSource.
  • CoreConsumer is an interface type exposing attach and detach callbacks.
  • When CorePointerSource has a new CoreController assigned to it, each associated CorePointer will dispatch callbacks to the CoreConsumer that owns it.
  • When the CorePointerSource is destroyed, all associated CorePointer objects become null pointers.

Motivation

Primarily, I wanted to unify how all of the various popup dialogs that depend on Window to set them up get access to CoreController. This will simplify the next step of the popup refactor by allowing a single code path to handle all such view classes.

Considered alternatives

My previous PR had used some arcane C++ template metaprogramming to avoid needing to introduce an interface class. I know you don't like multiple inheritance, but I think this is a lot more straightforward of an implementation that I expect to be easier to maintain going forward. If you insist, I can switch this back to a metaprogramming-based implementation. (However, it will beget even more awkward metaprogramming in the next phase because having a common base class makes it MUCH easier to detect what APIs a view supports.)

C++20 introduces some new template metaprogramming features that would make it significantly less ugly, but I don't expect you want to make that jump yet.

I could also use lambdas as delegates, with an API something along the lines of m_controller.connectAttach(this, &FooView::onCoreAttached); m_controller.connectDetach(this, &FooView::onCoreDetached);. This wouldn't change the implementation very much but it would increase the boilerplate on every class that uses it. (And I would still need the additional metaprogramming in the next phase.)

@ahigerd ahigerd force-pushed the alh/refactor-popups-part-1 branch 2 times, most recently from 48a992b to 4a8f20b Compare August 8, 2025 22:15
@ahigerd
Copy link
Contributor Author

ahigerd commented Aug 8, 2025

Next PR in the series: ahigerd#3

Comment on lines +583 to +596
namespace {
class RefAdaptor
{
public:
RefAdaptor(CorePointerSource& source) : source(&source) {}

inline operator CorePointerSource&() { return *source; }
inline operator CorePointerSource*() { return source; }
inline operator std::shared_ptr<CoreController>() { return *source; }

CorePointerSource* source;
};
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is temporary and the next PR removes it. It's only here so I don't have to make all of the changes at the same time.

@ahigerd
Copy link
Contributor Author

ahigerd commented Aug 9, 2025

Third PR in the series: ahigerd#4

@ahigerd
Copy link
Contributor Author

ahigerd commented Aug 9, 2025

Fourth PR in the series: ahigerd#5

@endrift endrift added this to the mGBA 0.12.0 milestone Sep 29, 2025
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