Jamba C++ API  5.1.1
SelfContainedViewListener Class Reference

The purpose of this class is to implement a view listener that is "self-contained": More...

#include <SelfContainedViewListener.h>

Inherits ViewListenerAdapter, and enable_shared_from_this< SelfContainedViewListener >.

Inherited by GlobalKeyboardHook.

Public Member Functions

virtual std::shared_ptr< SelfContainedViewListenerregisterView (CView *iView)
 Registers this class as a view listener. More...
 
 SelfContainedViewListener ()=default
 The constructor which unfortunately has to be declared public for the purpose of a creating a shared instance. More...
 
virtual void unregister ()
 You can call this method at anytime to unregister this class as the listener to the view that was previously registered in SelfContainedViewListener::create(). More...
 

Static Public Member Functions

template<typename T , typename ... Args>
static std::shared_ptr< T > create (CView *iView, Args &&...iArgs)
 This is the main method that should be used to create an instance of this class. More...
 

Protected Member Functions

void viewWillDelete (CView *iView) override
 Called by the view itself when it is going to be deleted. More...
 

Protected Attributes

std::shared_ptr< SelfContainedViewListenerfThis {}
 
CView * fView {}
 

Detailed Description

The purpose of this class is to implement a view listener that is "self-contained":

  • this class MUST live at least as long as it is registered with the view
  • this class offers an easy way to unregister early (if the client code desires to do so) while making sure that the instance is still valid.

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.

Note
Although this class is not marked internal and you can use it in your own code, it is used by the framework and you should rarely need to to use it.

Constructor & Destructor Documentation

◆ SelfContainedViewListener()

The constructor which unfortunately has to be declared public for the purpose of a creating a shared instance.

Do not instantiate directly.

Member Function Documentation

◆ create()

static std::shared_ptr<T> create ( CView *  iView,
Args &&...  iArgs 
)
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.

Template Parameters
Tthe actual type (must be a subtype of SelfContainedViewListener)
Argsoptional arguments templates for the T constructor
Parameters
iViewthe view on which this class will be registered as a listener (must not be nullptr)
iArgsoptional arguments for the T constructor
Returns
the shared pointer which may be kept around (scenario 2) but is NOT required

◆ registerView()

virtual std::shared_ptr<SelfContainedViewListener> registerView ( CView *  iView)
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 GlobalKeyboardHook.

◆ unregister()

virtual void unregister ( )
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 explicitly as it will be automatically unregistered when the view gets deleted.

Reimplemented in GlobalKeyboardHook.

◆ viewWillDelete()

void viewWillDelete ( CView *  iView)
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)

Member Data Documentation

◆ fThis

std::shared_ptr<SelfContainedViewListener> fThis {}
protected

◆ fView

CView* fView {}
protected

The documentation for this class was generated from the following file: