staticlib_support
1.2.1
Main Page
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Groups
staticlib
support
defer.hpp
Go to the documentation of this file.
1
/*
2
* Copyright 2017, alex at staticlibs.net
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
17
/*
18
* File: defer.hpp
19
* Author: alex
20
*
21
* Created on January 27, 2017, 12:07 PM
22
*/
23
24
#ifndef STATICLIB_SUPPORT_DEFER_HPP
25
#define STATICLIB_SUPPORT_DEFER_HPP
26
27
#include <utility>
28
29
#include "staticlib/config.hpp"
30
31
namespace
staticlib {
32
namespace
support {
33
34
namespace
detail_defer {
35
36
// http://stackoverflow.com/a/17356259/314015
41
template
<
typename
T>
42
class
defer_guard
{
43
T func;
44
bool
moved_out =
false
;
45
46
public
:
52
explicit
defer_guard
(T func) :
53
func(std::move(func)) { }
54
58
defer_guard
(
const
defer_guard
&) =
delete
;
59
63
defer_guard
&
operator=
(
const
defer_guard
&) =
delete
;
64
70
defer_guard
(
defer_guard
&& other) STATICLIB_NOEXCEPT :
71
func(std::move(other.func)) {
72
other.moved_out =
true
;
73
}
74
78
defer_guard
&
operator=
(
defer_guard
&&) =
delete
;
79
83
~defer_guard
() STATICLIB_NOEXCEPT {
84
#ifdef STATICLIB_NOEXCEPT_SUPPORTED
85
static_assert(noexcept(func()),
86
"Please check that the defer block cannot throw, "
87
"and mark the lambda as 'noexcept'."
);
88
#endif
89
if
(!moved_out) {
90
func();
91
}
92
}
93
};
94
95
}
// namespace
96
106
template
<
typename
T>
107
detail_defer::defer_guard<T>
defer
(T func) {
108
return
detail_defer::defer_guard<T>
(std::move(func));
109
}
110
111
}
// namespace
112
}
113
114
#endif
/* STATICLIB_SUPPORT_DEFER_HPP */
115
Generated on Thu Apr 4 2019 22:52:24 for staticlib_support by
1.8.1.2