Caffe2 - C++ API
A deep learning, cross platform ML framework
Public Member Functions | Static Protected Member Functions | Protected Attributes
caffe2::ScopeGuardImplBase Class Reference

ScopeGuard is a general implementation of the "Initialization is Resource Acquisition" idiom. More...

#include <scope_guard.h>

Inheritance diagram for caffe2::ScopeGuardImplBase:
caffe2::ScopeGuardImpl< FunctionType >

Public Member Functions

void dismiss () noexcept
 

Static Protected Member Functions

static ScopeGuardImplBase makeEmptyScopeGuard () noexcept
 
template<typename T >
static const T & asConst (const T &t) noexcept
 

Protected Attributes

bool dismissed_
 

Detailed Description

ScopeGuard is a general implementation of the "Initialization is Resource Acquisition" idiom.

Basically, it guarantees that a function is executed upon leaving the currrent scope unless otherwise told.

The MakeGuard() function is used to create a new ScopeGuard object. It can be instantiated with a lambda function, a std::function<void()>, a functor, or a void(*)() function pointer.

Usage example: Add a friend to memory iff it is also added to the db.

void User::addFriend(User& newFriend) { // add the friend to memory friends_.push_back(&newFriend);

// If the db insertion that follows fails, we should // remove it from memory. // (You could also declare this as "auto guard = MakeGuard(...)") ScopeGuard guard = MakeGuard([&] { friends_.pop_back(); });

// this will throw an exception upon error, which // makes the ScopeGuard execute UserCont::pop_back() // once the Guard's destructor is called. db_->addFriend(GetName(), newFriend.GetName());

// an exception was not thrown, so don't execute // the Guard. guard.dismiss(); }

Examine ScopeGuardTest.cpp for some more sample usage.

Stolen from: Andrei's and Petru Marginean's CUJ article: http://drdobbs.com/184403758 and the loki library: http://loki-lib.sourceforge.net/index.php?n=Idioms.ScopeGuardPointer and triendl.kj article: http://www.codeproject.com/KB/cpp/scope_guard.aspx

Definition at line 59 of file scope_guard.h.


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