summaryrefslogtreecommitdiff
path: root/source/kit/defer.h
blob: bb78b69f55b8ab122d26d3436d1f0523938959b5 (plain)
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
//  Requres GCC or Clang with `-fblocks` option available.
//

#ifndef KIT_DEFER_H
#define KIT_DEFER_H

#if !defined(__clang__) && !defined(__APPLE__)
#  error C blocks support required
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if defined(__GNUC__) || defined(__clang__)
#  pragma GCC diagnostic push
#  pragma GCC diagnostic ignored "-Wunused-function"
#  pragma GCC diagnostic ignored "-Wunknown-pragmas"
#endif

static void kit_defer_cleanup_(void (^*b)(void)) { (*b)(); }

#define kit_defer_merge_(a,b) a##b
#define kit_defer_varname_(a) \
   kit_defer_merge_(kit_defer_scopevar_,a)
#define defer \
   __attribute__((unused, cleanup(kit_defer_cleanup_))) \
 void (^kit_defer_varname_(__COUNTER__))(void) = ^
#define defer_ref __block

#if defined(__GNUC__) || defined(__clang__)
#  pragma GCC diagnostic pop
#endif

#ifdef __cplusplus
}
#endif

#endif