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
36
37namespace ScopeGuardDetail
38{
39 template <class Functor> class ScopeGuard
40 {
41 public:
43 : _f(std::move(fn))
44 , _active(true)
45 {}
46
48 : _f(std::move(rhs._f))
49 , _active(rhs._active)
50 {
51 rhs._active = false;
52 }
53
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
79 {
80 return ScopeGuard<Functor>(std::forward<Functor>(fn));
81 }
82} // namespace ScopeGuardDetail
83
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() + [&]()
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
ScopeGuard< Functor > operator+(ScopeGuardOnExit, Functor &&fn)
STL namespace.