PCL
pcl::AutoStatusCallbackRestorer Class Reference

Automatic recovery of status monitoring callbacks. More...

#include <AutoStatusCallbackRestorer.h>

Public Member Functions

 AutoStatusCallbackRestorer (const AutoStatusCallbackRestorer &)=delete
 
 AutoStatusCallbackRestorer (StatusMonitor &monitor)
 
 ~AutoStatusCallbackRestorer ()
 
StatusCallbackCallback () const
 
StatusMonitorMonitor () const
 
AutoStatusCallbackRestoreroperator= (const AutoStatusCallbackRestorer &)=delete
 
void Restore ()
 
void Store ()
 

Detailed Description

AutoStatusCallbackRestorer simplifies working with different status monitoring callback objects, including objects allocated as automatic variables (i.e., inside functions without the static qualifier), ensuring that the initial monitoring callback will be recovered when the instance gets out of scope.

An instance of AutoStatusCallbackRestorer stores a pointer to the StatusCallback object being used by a StatusMonitor instance upon construction, and restores it upon destruction. This ensures that the StatusMonitor object will always be left in a valid state, even in critical situations involving multiple function return points and exceptions.

Consider the following example:

void Foo( Image& image )
{
StatusCallback* oldStatus = image.StatusCallback();
StandardStatus status;
image.SetStatusCallback( &status );
image.Status().Initialize( "Performing Foo()", image.NumberOfPixels() );
image.Status().DisableInitialization();
try
{
DoFoo( image );
image.SetStatusCallback( oldStatus );
}
catch ( ... )
{
image.SetStatusCallback( oldStatus );
throw;
}
}
32-bit floating point real image.

Since status is an automatic variable, we cannot return from the Foo() function without removing it from image.Status(), because otherwise the StatusMonitor object would be referencing a dangling pointer. For this reason we have to be careful to restore the oldStatus pointer before returning from Foo(), including any possible returning points and all exceptions thrown.

With AutoStatusCallbackRestorer the above code can be simplified to:

void Foo( Image& image )
{
AutoStatusCallbackRestorer saveStatus( image.Status() );
StandardStatus status;
image.SetStatusCallback( &status );
image.Status().Initialize( "Performing Foo()", image.NumberOfPixels() );
image.Status().DisableInitialization();
DoFoo( image );
}
AutoStatusCallbackRestorer(StatusMonitor &monitor)

Note that the try-catch blocks are now unnecessary. As soon as the saveStatus variable gets out of scope, the image.Status() object will use its previous status callback again, that is, the same status callback that it was using before calling Foo(). This will happen automatically, including both normal function returns and uncaught exceptions within the Foo() function.

See also
StatusMonitor, StatusCallback

Definition at line 137 of file AutoStatusCallbackRestorer.h.

Constructor & Destructor Documentation

◆ AutoStatusCallbackRestorer() [1/2]

pcl::AutoStatusCallbackRestorer::AutoStatusCallbackRestorer ( StatusMonitor monitor)
inlineexplicit

Constructs an AutoStatusCallbackRestorer object for the specified client status monitor.

Parameters
monitorReference to the client StatusMonitor instance. This object will store a pointer to the StatusCallback object currently being used by the client monitor.
Warning
The specified monitor object must remain valid while this object exists. Otherwise undefined behavior will be invoked when this object is destroyed.

Definition at line 153 of file AutoStatusCallbackRestorer.h.

◆ ~AutoStatusCallbackRestorer()

pcl::AutoStatusCallbackRestorer::~AutoStatusCallbackRestorer ( )
inline

Destroys this AutoStatusCallbackRestorer object.

This destructor restores the status callback being used by the client status monitor when this object was constructed, or upon the last call to Store().

Definition at line 166 of file AutoStatusCallbackRestorer.h.

◆ AutoStatusCallbackRestorer() [2/2]

pcl::AutoStatusCallbackRestorer::AutoStatusCallbackRestorer ( const AutoStatusCallbackRestorer )
delete

Copy constructor. This constructor is disabled.

Member Function Documentation

◆ Callback()

StatusCallback* pcl::AutoStatusCallbackRestorer::Callback ( ) const
inline

Returns the address of the status callback object currently stored by this instance. This is a pointer to a StatusCallback object that was acquired when this object was constructed, or in the last call to the Store() member function.

Definition at line 205 of file AutoStatusCallbackRestorer.h.

◆ Monitor()

StatusMonitor& pcl::AutoStatusCallbackRestorer::Monitor ( ) const
inline

Returns a reference to the client status monitor object. The client status monitor has been specified upon construction of this instance and cannot be changed.

Definition at line 194 of file AutoStatusCallbackRestorer.h.

◆ operator=()

AutoStatusCallbackRestorer& pcl::AutoStatusCallbackRestorer::operator= ( const AutoStatusCallbackRestorer )
delete

Copy assignment. This constructor is disabled.

◆ Restore()

void pcl::AutoStatusCallbackRestorer::Restore ( )
inline

Restores the status callback that was being used by the client status monitor when this object was constructed, or upon the last call to Store().

Definition at line 176 of file AutoStatusCallbackRestorer.h.

◆ Store()

void pcl::AutoStatusCallbackRestorer::Store ( )
inline

Stores the status callback currently used by the client status monitor.

Definition at line 184 of file AutoStatusCallbackRestorer.h.


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