Belle II Software light-2406-ragdoll
|
Simple ScopeGuard to execute a function at the end of the object lifetime. More...
#include <ScopeGuard.h>
Public Member Functions | |
template<class Callable > | |
ScopeGuard (Callable &&f) | |
Construct a object with a callable function to be called on destruction. | |
ScopeGuard (const ScopeGuard &)=delete | |
No copies. | |
ScopeGuard (ScopeGuard &&sg)=delete | |
No move construction. | |
ScopeGuard & | operator= (const ScopeGuard &)=delete |
No assignment. | |
ScopeGuard & | operator= (ScopeGuard &&)=delete |
No move assignment. | |
~ScopeGuard () | |
Call function on destruct unless released has been called. | |
void | release () |
Release the guard without calling the cleanup function. | |
Static Public Member Functions | |
template<class T > | |
static ScopeGuard | guardValue (T &reference) |
Create a ScopeGuard for a value: The content of reference will be copied and reset when the returned object goes out of scope. | |
template<class T , class V > | |
static ScopeGuard | guardValue (T &reference, const V &newValue) |
Create a ScopeGuard for a value: The content of reference will be copied and reset when the returned object goes out of scope. | |
template<class Getter , class Setter > | |
static ScopeGuard | guardGetterSetter (Getter getter, Setter setter) |
Create a ScopeGuard from a getter and setter: On construction the getter object is called to obtain a value which is handed to the setter when the guard object goes out of scope. | |
template<class Getter , class Setter > | |
static ScopeGuard | guardGetterSetter (const Getter &getter, Setter setter, const typename std::result_of< Getter()>::type newValue) |
Create a ScopeGuard from a getter and setter: On construction first the getter is called to get the original value, then the setter is called to set the new value and when the guard object is destructed the setter is called with the original value again. | |
template<class Functor > | |
static ScopeGuard | guardFunctor (Functor functor) |
Create a ScopeGuard from a functor object with appropriately overloaded operator() calls to get and set the values. | |
template<class Functor > | |
static ScopeGuard | guardFunctor (Functor functor, const typename std::result_of< Functor()>::type &newValue) |
Create a ScopeGuard from a functor object with appropriately overloaded operator() calls to get and set the values. | |
template<class T > | |
static ScopeGuard | guardDeletion (T *&pointer) |
Create a ScopeGuard to delete a raw pointer at the end of the scope. | |
template<class CharT , class Traits = typename std::char_traits<CharT>> | |
static ScopeGuard | guardStreamState (std::basic_ios< CharT, Traits > &stream) |
Create a ScopeGuard for the state of a stream to reset all the formatting at the end of the object lifetime. | |
static ScopeGuard | guardWorkingDirectory () |
Create a ScopeGuard of the current working directory. | |
static ScopeGuard | guardWorkingDirectory (const std::string &newDirectory) |
Create a ScopeGuard of the current working directory and change into a new directory. | |
static ScopeGuard | guardBatchMode (bool batchMode=true) |
Create a ScopeGuard to turn ROOT into batch mode and restore the initial batch mode status after the guard object is destroyed. | |
Private Attributes | |
std::function< void()> | m_exitfunc |
Function to be called on exit. | |
bool | m_engaged {true} |
Indicate whether function should be called. | |
Simple ScopeGuard to execute a function at the end of the object lifetime.
This should be used when you want to make sure some cleanup is performed at the end of the current scope even if you might have multiple possible ways to exit the scope (return statements, exceptions).
Optionally you can call release()
to indicate that cleanup will not be needed after all but the state should remain as it is.
For many common use cases there are factory functions to create the correct ScopeGuard object automatically
Definition at line 36 of file ScopeGuard.h.
|
inlineexplicit |
Construct a object with a callable function to be called on destruction.
Definition at line 44 of file ScopeGuard.h.
|
inline |
Call function on destruct unless released has been called.
Definition at line 54 of file ScopeGuard.h.
|
inlinestatic |
Create a ScopeGuard to turn ROOT into batch mode and restore the initial batch mode status after the guard object is destroyed.
Restoring the initial status is important if your code e..g should run with and without the display module.
Definition at line 352 of file ScopeGuard.h.
|
inlinestatic |
Create a ScopeGuard to delete a raw pointer at the end of the scope.
Sometimes with ROOT we cannot use std::unique_ptr
as ROOT wants the address of the pointer to modify it and allocate objects but ROOT still gives us ownership :/.
This class is thus a very simplified scope guard which will just delete the pointer on destruction but still allows using a raw pointer so that ROOT can do whatever it does while this guard is alive.
std::unique_ptr
instead. Only use this if you know what you are doing.pointer | reference to a raw pointer to be deleted when the returned guard object goes out of scope |
Definition at line 239 of file ScopeGuard.h.
|
inlinestatic |
Create a ScopeGuard from a functor object with appropriately overloaded operator()
calls to get and set the values.
Otherwise it behaves exactly like guardGetterSetter()
functor | An object which has a suitable overloaded operator() to be able to
|
Definition at line 188 of file ScopeGuard.h.
|
inlinestatic |
Create a ScopeGuard from a functor object with appropriately overloaded operator()
calls to get and set the values.
Otherwise it behaves exactly like guardGetterSetter()
functor | An object which has a suitable overloaded operator() to be able to
|
newValue | a new value to pass to the functor when this function is called |
Definition at line 210 of file ScopeGuard.h.
|
inlinestatic |
Create a ScopeGuard from a getter and setter: On construction first the getter is called to get the original value, then the setter is called to set the new value and when the guard object is destructed the setter is called with the original value again.
getter | a callable object to return a value |
setter | a callable object to set a value of the same type |
newValue | a new value to pass to the setter when this function is called |
Definition at line 168 of file ScopeGuard.h.
|
inlinestatic |
Create a ScopeGuard from a getter and setter: On construction the getter object is called to obtain a value which is handed to the setter when the guard object goes out of scope.
getter | a callable object to return a value |
setter | a callable object to set a value of the same type |
Definition at line 137 of file ScopeGuard.h.
|
inlinestatic |
Create a ScopeGuard for the state of a stream to reset all the formatting at the end of the object lifetime.
stream | stream object to reset |
Definition at line 262 of file ScopeGuard.h.
|
inlinestatic |
Create a ScopeGuard for a value: The content of reference will be copied and reset when the returned object goes out of scope.
reference | a reference to a value to be guarded |
Definition at line 76 of file ScopeGuard.h.
|
inlinestatic |
Create a ScopeGuard for a value: The content of reference will be copied and reset when the returned object goes out of scope.
Will also immediately set a new value.
reference | a reference to a value to be guarded |
newValue | the new value to set the reference to. Has to be assignable to reference |
Definition at line 108 of file ScopeGuard.h.
|
inlinestatic |
Create a ScopeGuard of the current working directory.
This makes sure that whatever happens to the current working directory we return to the original working directory when the ScopeGuard is destroyed.
Definition at line 296 of file ScopeGuard.h.
|
inlinestatic |
Create a ScopeGuard of the current working directory and change into a new directory.
This makes sure that whatever happens to the current working directory we return to the original working directory when the ScopeGuard is destroyed.
Definition at line 323 of file ScopeGuard.h.
|
inline |
|
private |
Indicate whether function should be called.
Definition at line 41 of file ScopeGuard.h.
|
private |
Function to be called on exit.
Definition at line 39 of file ScopeGuard.h.