1. Си / Говнокод #24517

    −3

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 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

    raii

    A simple library to provide RAII in standard-compliant C99, using raii_with(resource, initializer, destructor) { ... }-syntax:

    Запостил: j123123, 19 Июля 2018

    Комментарии (242) RSS

    Добавить комментарий