COMMS
Template library intended to help with implementation of communication protocols.
Public Member Functions | Related Functions | List of all members
comms::util::ScopeGuard< TFunc > Class Template Reference

#include "comms/util/ScopeGuard.h"

Detailed Description

template<typename TFunc>
class comms::util::ScopeGuard< TFunc >

Implements Scope Guard Idiom.

Scope guard idiom allows to call any function with any number of parameters when the guard is destructed, unless release() method is called prior to the destruction. The scope guard doesn't use any dynamic memory allocation and takes as much space on the stack as needed to bind the provided function with all its arguments. The template parameter must be type of the functor that doesn't receive any parameters and doesn't return any value. In order to properly create such guard use makeScopeGuard() function. For example:

// Binding function with parameters:
auto guard = comms::util::makeScopeGuard(&func, std::ref(arg1), arg2);
// Binding lamda function:
auto guard =
comms::util::makeScopeGuard([&argByRef, argByValue]()
{
...// Some code here
});

Note that all the bound parameters are passed by value, if there is any need to bind function with reference to some object, use "std::ref()" or "std::cref()" for const reference. Also note that the guard doesn't provide copy constructor and assignment operator, it supports only move semantics.

Template Parameters
TFuncFunctor object type.

Public Member Functions

 ScopeGuard (const ScopeGuard &guard)=delete
 No copy is allowed.
 
 ScopeGuard (ScopeGuard &&guard)
 Move constructor. More...
 
 ScopeGuard (TFunc &&func)
 Constructor. More...
 
 ~ScopeGuard () noexcept
 Destructor. More...
 
bool isReleased () const
 Check whether the functor is released. More...
 
ScopeGuardoperator= (const ScopeGuard &guard)=delete
 No copy is allowed.
 
void release ()
 Release the bound functor. More...
 

Related Functions

(Note that these are not member functions.)

template<typename TFunc , typename... TParams>
auto makeScopeGuard (TFunc &&func, TParams... args) -> ScopeGuard< decltype(std::bind(std::forward< TFunc >(func), std::forward< TParams >(args)...))>
 Create scope guard by binding the provided function and all the arguments. More...
 
template<typename TFunctor >
ScopeGuard< TFunctor > makeScopeGuard (TFunctor &&func)
 Create scope guard with provided functor. More...
 

Constructor & Destructor Documentation

◆ ScopeGuard() [1/2]

template<typename TFunc >
comms::util::ScopeGuard< TFunc >::ScopeGuard ( TFunc &&  func)
explicit

Constructor.

Parameters
[in]funcFunctor that will be executed when the scope guard is destructed unless it is "released." Must provide move/copy constructor.

◆ ScopeGuard() [2/2]

template<typename TFunc >
comms::util::ScopeGuard< TFunc >::ScopeGuard ( ScopeGuard< TFunc > &&  guard)

Move constructor.

After the functor is moved, it will be released in the provided guard.

Parameters
[in]guardThe other scope guard of the same type.

◆ ~ScopeGuard()

template<typename TFunc >
comms::util::ScopeGuard< TFunc >::~ScopeGuard ( )
noexcept

Destructor.

Postcondition
The functor is called unless it was released with release() prior to destruction.

Member Function Documentation

◆ isReleased()

template<typename TFunc >
bool comms::util::ScopeGuard< TFunc >::isReleased ( ) const

Check whether the functor is released.

Returns
true in case of being released.

◆ release()

template<typename TFunc >
void comms::util::ScopeGuard< TFunc >::release ( )

Release the bound functor.

Postcondition
The functor won't be called when the scope guard is out of scope.

Friends And Related Function Documentation

◆ makeScopeGuard() [1/2]

template<typename TFunc , typename... TParams>
auto makeScopeGuard ( TFunc &&  func,
TParams...  args 
) -> ScopeGuard<decltype(std::bind(std::forward<TFunc>(func), std::forward<TParams>(args)...))>
related

Create scope guard by binding the provided function and all the arguments.

Use this function to create a scope guard when some function with one or more arguments needs to be called. For example:

// Binding function with parameters:
auto guard = comms::util::makeScopeGuard(&func, std::ref(arg1), arg2);

Note that all the bound parameters are passed by value, if there is any need to bind function with reference to some object, use "std::ref()" or "std::cref()" for const reference. Also note that this function uses variadic template arguments which were introduced in C++11. Please make sure that you compiler supports it.

Template Parameters
TFuncPointer to function type.
TParamsTypes of other arguments.
Parameters
[in]funcFunctor
[in]argsFunction arguments
Returns
Scope guard.

◆ makeScopeGuard() [2/2]

template<typename TFunctor >
ScopeGuard< TFunctor > makeScopeGuard ( TFunctor &&  func)
related

Create scope guard with provided functor.

Use this function to create a scope guard with lambda function. For example:

auto guard =
comms::util::makeScopeGuard([&argByRef, argByValue]()
{
...// Some code here
});
Template Parameters
TFunctorFunctor type, should be deduced automatically based on provided argument.
Parameters
[in]funcFunctor
Returns
Scope guard.

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