- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
// https://github.com/Qqwy/raii_with/blob/74e4c66a821fba6a483d62a8c583b3fab06e3443/raii/raii.h#L60
/**
* Custom Control Structure Macro to provide Resource Acquisition Is Initialization (and Resource Relinquishment is Destruction).
*
* Use this to run a block of code with `var_decl` initialized to `init`, where at the end of the block (or at an earlier `safe_return`),
* the passed `destr`-function will automatically be called with the given resource.
*
* Gotcha's:
* 1. Do not use `return` from within `raii_with`, but only `safe_return`, because otherwise the destructors will not be run.
* 2. Do not perform pointer-swaps with `var_decl`; the destructor will still be run on the original structure, because `raii` keeps its own reference to the resource.
*/
#define raii_with(var_decl, init, destr) \
while(1) /* i.c.m. break on l.4, so we can jump past the user-supplied block */ \
if(0) \
raii_glue(__raii_with_finished, __LINE__): \
break; \
else \
/* initialize _tmp lifetime list elem so replacement `raii_lifetime_list` can have previous one as tail. */ \
for(struct raii_lifetime_list_t _tmp = {.elem.resource = init, .elem.destructor = destr, .next = raii_lifetime_list};;) \
/* initialize user-supplied variable name */ \
for(var_decl = _tmp.elem.resource;;) \
if (1) { \
/* Fill `_tmp`'s tail before `raii_lifetime_list` is shadowed */ \
_tmp.next = raii_lifetime_list; \
goto raii_glue(__raii_with_setup, __LINE__); \
} else \
raii_glue(__raii_with_setup, __LINE__): \
/* Shadow `raii_lifetime_list` with inner version */ \
for(struct raii_lifetime_list_t *raii_lifetime_list = &_tmp;;) \
if(1){ \
goto raii_glue(__raii_with_body, __LINE__); \
} else \
while (1) /* so break works as expected */ \
while (1) /*so continue works as expected */ \
if (1){ \
/*after the else-block (or break or continue), destruct and finish */ \
destruct_raii_lifetime(raii_lifetime_list->elem); \
goto raii_glue(__raii_with_finished, __LINE__); \
} else \
raii_glue(__raii_with_body, __LINE__):
#endif // RAII_WITH_H