PCL
|
Automatic recovery of status monitoring callbacks. More...
#include <AutoStatusCallbackRestorer.h>
Public Member Functions | |
AutoStatusCallbackRestorer (const AutoStatusCallbackRestorer &)=delete | |
AutoStatusCallbackRestorer (StatusMonitor &monitor) | |
~AutoStatusCallbackRestorer () | |
StatusCallback * | Callback () const |
StatusMonitor & | Monitor () const |
AutoStatusCallbackRestorer & | operator= (const AutoStatusCallbackRestorer &)=delete |
void | Restore () |
void | Store () |
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:
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:
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.
Definition at line 137 of file AutoStatusCallbackRestorer.h.
|
inlineexplicit |
Constructs an AutoStatusCallbackRestorer object for the specified client status monitor.
monitor | Reference to the client StatusMonitor instance. This object will store a pointer to the StatusCallback object currently being used by the client monitor. |
Definition at line 153 of file AutoStatusCallbackRestorer.h.
|
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.
|
delete |
Copy constructor. This constructor is disabled.
|
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.
|
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.
|
delete |
Copy assignment. This constructor is disabled.
|
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.
|
inline |
Stores the status callback currently used by the client status monitor.
Definition at line 184 of file AutoStatusCallbackRestorer.h.