PCL
pcl::AutoViewLock Class Reference

Automatic view lock/unlock. More...

#include <AutoViewLock.h>

+ Inheritance diagram for pcl::AutoViewLock:

Public Member Functions

 AutoViewLock (const AutoViewLock &)=delete
 
 AutoViewLock (View &view, bool lock=true)
 
 ~AutoViewLock ()
 
void Lock ()
 
void LockForWrite ()
 
AutoViewLockoperator= (const AutoViewLock &)=delete
 
void RelockForRead ()
 
void Unlock ()
 
void UnlockForRead ()
 

Detailed Description

AutoViewLock simplifies locking and unlocking View objects accessed from process execution routines.

An AutoViewLock object locks a View upon construction and unlocks it upon destruction. This ensures that a view will never be left locked, even in critical situations involving multiple function return points and exceptions.

AutoViewLock works for View just as AutoLock does for Mutex. The main difference is that a view can be locked and unlocked for read and write operations separately, while a mutex cannot.

Typically AutoViewLock is used within a reimplementation of the ProcessImplementation::ExecuteOn() virtual member function for a process instance class. Consider the following example:

bool FooProcessInstance::ExecuteOn( View& view )
{
try
{
view.Lock();
ImageVariant image = view.Image();
if ( image.IsComplexSample() )
{
view.Unlock();
return false;
}
Console().EnableAbort();
StandardStatus status;
image->SetStatusCallback( &status );
FooEngine::Apply( image, *this );
view.Unlock();
return true;
}
catch ( ... )
{
view.Unlock();
throw;
}
}
void Apply(FI i, FI j, F f) noexcept(noexcept(f))
Definition: Utility.h:249

With AutoViewLock the above code can be simplified considerably:

bool FooProcessInstance::ExecuteOn( View& view )
{
AutoViewLock lock( view );
ImageVariant image = view.Image();
if ( image.IsComplexSample() )
return false;
Console().EnableAbort();
StandardStatus status;
image->SetStatusCallback( &status );
FooEngine::Apply( image, *this );
return true;
}
AutoViewLock(View &view, bool lock=true)
Definition: AutoViewLock.h:187

Note that the try-catch blocks are now unnecessary. As soon as the lock variable gets out of scope, the view object will be unlocked automatically, including both normal function returns and uncaught exceptions within the ExecuteOn() function. Keep in mind that all exceptions will always be caught by internal PCL routines.

See also
View

Definition at line 152 of file AutoViewLock.h.

Constructor & Destructor Documentation

◆ AutoViewLock() [1/2]

pcl::AutoViewLock::AutoViewLock ( View view,
bool  lock = true 
)
inlineexplicit

Constructs an AutoViewLock object to monitor the specified view.

Parameters
viewA View object that will be monitored by this AutoViewLock instance. The object must remain valid for the whole lifetime of this AutoViewLock instance.
lockWhether the specified view should be locked for read and write operations by this constructor. The default value is true.

If the lock argument is true, the specified view will be locked for read and write operations immediately by this constructor. It will be unlocked automatically when this AutoViewLock object gets out of scope, or if it is destroyed explicitly.

By specifying lock=false, you can create an AutoViewLock object to monitor a view and lock it for write operations exclusively:

void foo( View& view )
{
AutoViewLock lock( view, false ); // do not lock for read-write ops.
lock.LockForWrite();
...
}

See AutoViewWriteLock for a convenience class to implements this functionality in a cleaner way.

Definition at line 187 of file AutoViewLock.h.

◆ ~AutoViewLock()

pcl::AutoViewLock::~AutoViewLock ( )
inline

Destroys this AutoViewLock object.

If the monitored view (that was specified in the constructor) is locked, it is unlocked by this destructor.

Definition at line 202 of file AutoViewLock.h.

References pcl::View::Null().

◆ AutoViewLock() [2/2]

pcl::AutoViewLock::AutoViewLock ( const AutoViewLock )
delete

Copy constructor. This constructor is disabled because views and view locks are unique objects.

Member Function Documentation

◆ Lock()

void pcl::AutoViewLock::Lock ( )
inline

Locks the monitored view for read and write operations, if it has not been previously locked.

For more information, see the documentation for View::Lock().

Definition at line 226 of file AutoViewLock.h.

◆ LockForWrite()

void pcl::AutoViewLock::LockForWrite ( )
inline

Locks the monitored view for write operations, if it has not already been write-locked.

For more information, see the documentation for View::LockForWrite().

Definition at line 266 of file AutoViewLock.h.

◆ operator=()

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

Copy assignment. This operator is disabled because views and view locks are unique objects.

◆ RelockForRead()

void pcl::AutoViewLock::RelockForRead ( )
inline

Unlocks the monitored view for read operations only, if it has not already been unlocked.

For more information, see the documentation for View::RelockForRead().

Definition at line 292 of file AutoViewLock.h.

◆ Unlock()

void pcl::AutoViewLock::Unlock ( )
inline

Unlocks the monitored view for read and write operations, if it has been previously locked.

For more information, see the documentation for View::Unlock().

Definition at line 246 of file AutoViewLock.h.

◆ UnlockForRead()

void pcl::AutoViewLock::UnlockForRead ( )
inline

Unlocks the monitored view for read operations, if it has already been read-locked.

For more information, see the documentation for View::UnlockForRead().

Definition at line 279 of file AutoViewLock.h.


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