acme
Foundational cross-platform layer of the ca2 framework
Loading...
Searching...
No Matches
auto.h
1#pragma once
2
3// arthur o'dwyer and stefanus du toit => AUTO Macro -> also may be a good idea to use proper case for template typename parameters
4// for not to be confused with MACROs
5// https://github.com/CppCon/CppCon2014/blob/master/Presentations/C%2B%2B11%20in%20the%20Wild%20-%20Techniques%20from%20a%20Real%20Codebase/auto.h
6
7template < class Lambda > class at_scope_exit
8{
9
10 Lambda & m_lambdaAtScopeExit;
11
12public:
13
14 at_scope_exit(Lambda & action_at_scope_exit): m_lambdaAtScopeExit(action_at_scope_exit)
15 {
16 }
17 ~at_scope_exit()
18 {
19 m_lambdaAtScopeExit();
20 }
21};
22
23
24#define AUTO_INTERNAL1(lambda_function_name, at_scope_exit_var_name, ...) \
25 \
26auto lambda_function_name = [&]() { __VA_ARGS__; }; \
27 \
28at_scope_exit < decltype(lambda_function_name) > at_scope_exit_var_name(lambda_function_name);
29
30
31#define AUTO_INTERNAL2(counter, ...) AUTO_INTERNAL1(TOKEN_CONCATENATE(auto_lambda_function_, counter), TOKEN_CONCATENATE(auto_lambda_instance_, counter), __VA_ARGS__)
32
33
34// put a function/member call even with args... this will be called on exit, regardless premature returns/exceptions
35// e.g.: AUTO(m_readdb.reopen(this, m_strPath, false, false));
36#define AUTO(...) AUTO_INTERNAL2(__COUNTER__, __VA_ARGS__) // at_scope_exit instantiator
37
38
39
40