MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlScopeGuard.h
Go to the documentation of this file.
1
/*************************************************************************************
2
**
3
** Copyright 2013, MeVis Medical Solutions AG
4
**
5
** The user may use this file in accordance with the license agreement provided with
6
** the Software or, alternatively, in accordance with the terms contained in a
7
** written agreement between the user and MeVis Medical Solutions AG.
8
**
9
** For further information use the contact form at https://www.mevislab.de/contact
10
**
11
**************************************************************************************/
12
13
#pragma once
14
15
#include "
mlTypeDefs.h
"
16
17
#include <utility>
18
24
#define ML_UTILITIES_CONCATENATE_IMPL(s1, s2) s1##s2
25
#define ML_UTILITIES_CONCATENATE(s1, s2) ML_UTILITIES_CONCATENATE_IMPL(s1, s2)
26
27
// Some compilers do not support __COUNTER__ so there is an alternative
28
// implementation with __LINE__
29
#ifdef __COUNTER__
30
#define ML_UTILITIES_ANONYMOUS_VARIABLE(str) ML_UTILITIES_CONCATENATE(str, __COUNTER__)
31
#else
32
#define ML_UTILITIES_ANONYMOUS_VARIABLE(str) ML_UTILITIES_CONCATENATE(str, __LINE__)
33
#endif
34
35
ML_START_NAMESPACE
36
37
namespace
ScopeGuardDetail
38
{
39
template
<
class
Functor>
class
ScopeGuard
40
{
41
public
:
42
explicit
ScopeGuard
(
Functor
fn
)
43
: _f(
std
::
move
(
fn
))
44
, _active(
true
)
45
{}
46
47
ScopeGuard
(
ScopeGuard
&&
rhs
)
48
: _f(
std
::
move
(
rhs
._f))
49
, _active(
rhs
._active)
50
{
51
rhs
._active =
false
;
52
}
53
54
~ScopeGuard
()
55
{
56
if
(_active) {
57
_f();
58
}
59
}
60
61
void
dismiss
()
62
{
63
_active =
false
;
64
}
65
66
private
:
67
Functor
_f;
68
bool
_active;
69
70
ScopeGuard
() =
delete
;
71
ScopeGuard
(
const
ScopeGuard
&) =
delete
;
72
ScopeGuard
& operator=(
const
ScopeGuard
&) =
delete
;
73
};
74
75
enum class
ScopeGuardOnExit
{
76
};
77
78
template
<
class
Functor>
ScopeGuard<Functor>
operator+
(
ScopeGuardOnExit
,
Functor
&&
fn
)
79
{
80
return
ScopeGuard<Functor>
(std::forward<Functor>(
fn
));
81
}
82
}
// namespace ScopeGuardDetail
83
84
ML_END_NAMESPACE
85
92
#define SCOPE_EXIT \
93
auto ML_UTILITIES_ANONYMOUS_VARIABLE(SCOPE_EXIT_STATE) = ::ML_NAMESPACE::ScopeGuardDetail::ScopeGuardOnExit() + [&]()
94
95
111
#define EXPLICIT_SCOPE_EXIT ::ML_NAMESPACE::ScopeGuardDetail::ScopeGuardOnExit() + [&]()
ml::ScopeGuardDetail::ScopeGuard
Definition
mlScopeGuard.h:40
ml::ScopeGuardDetail::ScopeGuard::~ScopeGuard
~ScopeGuard()
Definition
mlScopeGuard.h:54
ml::ScopeGuardDetail::ScopeGuard::ScopeGuard
ScopeGuard(Functor fn)
Definition
mlScopeGuard.h:42
ml::ScopeGuardDetail::ScopeGuard::ScopeGuard
ScopeGuard(ScopeGuard &&rhs)
Definition
mlScopeGuard.h:47
ml::ScopeGuardDetail::ScopeGuard::dismiss
void dismiss()
Definition
mlScopeGuard.h:61
mlrange_cast
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
Definition
mlRangeCasts.h:305
mlTypeDefs.h
ml::ScopeGuardDetail::operator+
ScopeGuard< Functor > operator+(ScopeGuardOnExit, Functor &&fn)
Definition
mlScopeGuard.h:78
ml::ScopeGuardDetail::ScopeGuardOnExit
ScopeGuardOnExit
Definition
mlScopeGuard.h:75
std
STL namespace.
MeVis
Foundation
Sources
MLUtilities
mlScopeGuard.h
Generated by
1.10.0