Workspace 6.21.5
Classes | Namespaces | Functions
scopeguard.h File Reference

This file contains a class and factory method to easily create a generic scope guard in any given context by taking advantage of type inferencing and lambdas. This simplifies the code needed to use RAII to perform cleanup functionality regardless of how a method is exited. For example, deallocating dynamically allocated objects or closing a connection when a method exits, regardless of whether it exits successfully or due to an error. More...

#include <memory>
#include "Workspace/Application/LanguageUtils/errorchecks.h"
#include "Workspace/Application/logmanager.h"
Include dependency graph for scopeguard.h:
This graph shows which files directly or indirectly include this file:

Classes

class  ScopeGuard< Callable >
 Generic scopeguard to simplify RAII where cleanup is needed. More...
 

Namespaces

namespace  CSIRO
 Top level namespace for all Workspace code.
 
namespace  CSIRO::Application
 Application-level functionality for all workspace applications.
 

Functions

template<typename Callable >
ScopeGuard< Callable > createScopeGuard (Callable rollback)
 

Detailed Description

The ScopeGuard::dismiss() method can be used to dismiss a guard if its rollback method is no longer required.

Sample usage:

QSqlDatabase db(connectionString);
if (!db.isOpen() || !db.open())
{
return false;
}
auto guard = Application::createScopeGuard([&db]() { db.close(); });
if (!process(db))
{
return false;
}
return true;