|
Jamba
3.1.0
|
#include <SelfContainedViewListener.h>
Inherits IViewListenerAdapter, and enable_shared_from_this< SelfContainedViewListener >.
Inherited by pongasoft::VST::GUI::Views::GlobalKeyboardHook.
Public Member Functions | |
| virtual std::shared_ptr< SelfContainedViewListener > | registerView (CView *iView) |
| SelfContainedViewListener ()=default | |
| virtual void | unregister () |
Static Public Member Functions | |
| template<typename T , typename ... Args> | |
| static std::shared_ptr< T > | create (CView *iView, Args &&...iArgs) |
Protected Member Functions | |
| void | viewWillDelete (CView *iView) override |
Protected Attributes | |
| std::shared_ptr< SelfContainedViewListener > | fThis {} |
| CView * | fView {} |
The purpose of this class is to implement a view listener that is "self-contained":
The typical code usage would be
// Create your own listener
class MyViewListener : public SelfContainedViewListener
{
public:
// override whichever method from IViewListener that you are interested in
// if you override viewWillDelete, make sure to call SelfContainedViewListener::viewWillDelete
}
// Scenario 1: you only care about creating a listener for the duration of the view
// we assume that it is being called from a custom controller
// (but can be called directly from a view using "this")
CView *verifyView(CView *iView, ...)
{
if(iView ....)
{
// create and "forget"... completely self contained, no need to keep a reference to it
SelfContainedViewListener::create<MyViewListener>(iView);
}
}
// Scenario 2: you want to be able to unregister at a later time
std::shared_ptr<MyViewListener> fListener{};
CView *verifyView(CView *iView, ...)
{
if(iView ....)
{
// keep a reference to it which is guaranteed to remain valid
fListener = SelfContainedViewListener::create<MyViewListener>(iView);
}
}
void someCustomCode()
{
if(fListener)
fListener->unregister();
}
Implementation note: internally this class keeps a std::shared_ptr to ifself for as long as this is registered with the view itself, ensuring that this class is not going to be deleted until the view goes away (or it is unregistered). If an outside piece of code also has a reference to this class which is scenario 2, then the class itself won't be deleted until this other reference goes away.
|
default |
The constructor which unfortunately has to be declared public for the purpose of a creating a shared instance. Do not instantiate directly.
|
inlinestatic |
This is the main method that should be used to create an instance of this class. It will automatically register this class as a view listener to iView.
| T | the actual type (should be a subtype of SelfContainedViewListener) |
| Args | optional arguments templates for the T constructor |
| iView | the view on which this class will be registered as a listener (must not be nullptr) |
| args | optional arguments for the T constructor |
|
inlinevirtual |
Registers this class as a view listener. Note that this call requires to be called from a shared pointer. It is called by SelfContainedViewListener::create.
Reimplemented in pongasoft::VST::GUI::Views::GlobalKeyboardHook.
|
inlinevirtual |
You can call this method at anytime to unregister this class as the listener to the view that was previously registered in SelfContainedViewListener::create. It is not necessary to call it explicitely as it will be automatically unregistered when the view gets deleted.
Reimplemented in pongasoft::VST::GUI::Views::GlobalKeyboardHook.
|
inlineoverrideprotected |
Called by the view itself when it is going to be deleted. It automatically unregisters itself and deletes the shared_ptr so that the class can be properly deleted (when all other instances of the shared_ptr are gone)
|
protected |
|
protected |