forked from zhllxt/asio2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefer.hpp
More file actions
58 lines (46 loc) · 1.19 KB
/
Copy pathdefer.hpp
File metadata and controls
58 lines (46 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* COPYRIGHT (C) 2017-2021, zhllxt
*
* author : zhllxt
* email : 37792738@qq.com
*
* Distributed under the GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
* (See accompanying file LICENSE or see <http://www.gnu.org/licenses/>)
*
* refrenced from https://github.com/loveyacper/ananas
*/
#ifndef __ASIO2_DEFER_HPP__
#define __ASIO2_DEFER_HPP__
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
#pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <functional>
namespace asio2
{
class defer
{
public:
defer() = default;
// movable
defer(defer&&) = default;
defer& operator=(defer&&) = default;
// non copyable
defer(const defer&) = delete;
void operator=(const defer&) = delete;
template <typename Fun, typename... Args>
defer(Fun&& fun, Args&&... args)
{
this->fn_ = std::bind(std::forward<Fun>(fun), std::forward<Args>(args)...);
}
~defer() noexcept
{
if (this->fn_) this->fn_();
}
protected:
std::function<void()> fn_;
};
#define ASIO2_CONCAT(a, b) a##b
#define ASIO2_MAKE_DEFER(line) ::asio2::defer ASIO2_CONCAT(_asio2_defer_, line) = [&]()
#define ASIO2_DEFER ASIO2_MAKE_DEFER(__LINE__)
}
#endif // !__ASIO2_DEFER_HPP__