es_util.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. // See https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms
  41. #ifndef ES_UTIL_H__
  42. #define ES_UTIL_H__
  43. #define BOOL(x) COMPL(NOT(x))
  44. #define IF(c) IIF(BOOL(c))
  45. #define CHECK_N(x, n, ...) n
  46. #define CHECK(...) CHECK_N(__VA_ARGS__, 0,)
  47. #define PROBE(x) x, 1,
  48. #define EAT(...)
  49. #define EXPAND(...) __VA_ARGS__
  50. #define WHEN(c) IF(c)(EXPAND, EAT)
  51. #define NOT(x) CHECK(PRIMITIVE_CAT(NOT_, x))
  52. #define NOT_0 PROBE(~)
  53. #define COMPL(b) PRIMITIVE_CAT(COMPL_, b)
  54. #define COMPL_0 1
  55. #define COMPL_1 0
  56. #define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
  57. #define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
  58. #define IIF(c) PRIMITIVE_CAT(IIF_, c)
  59. #define IIF_0(t, ...) __VA_ARGS__
  60. #define IIF_1(t, ...) t
  61. #define DEC(x) PRIMITIVE_CAT(DEC_, x)
  62. #define DEC_0 0
  63. #define DEC_1 0
  64. #define DEC_2 1
  65. #define DEC_3 2
  66. #define DEC_4 3
  67. #define DEC_5 4
  68. #define DEC_6 5
  69. #define DEC_7 6
  70. #define DEC_8 7
  71. #define DEC_9 8
  72. #define DEC_10 9
  73. #define DEC_11 10
  74. #define DEC_12 11
  75. #define DEC_13 12
  76. #define DEC_14 13
  77. #define DEC_15 14
  78. #define DEC_16 15
  79. #define DEC_17 16
  80. #define DEC_18 17
  81. #define DEC_19 18
  82. #define DEC_20 19
  83. #define DEC_21 20
  84. #define DEC_22 21
  85. #define DEC_23 22
  86. #define DEC_24 23
  87. #define DEC_25 24
  88. #define DEC_26 25
  89. #define DEC_27 26
  90. #define DEC_28 27
  91. #define DEC_29 28
  92. #define DEC_30 29
  93. #define DEC_31 30
  94. #define DEC_32 31
  95. #define EMPTY()
  96. #define DEFER(id) id EMPTY()
  97. #define OBSTRUCT(...) __VA_ARGS__ DEFER(EMPTY)()
  98. #define EXPAND(...) __VA_ARGS__
  99. #define EVAL(...) EVAL1(EVAL1(EVAL1(__VA_ARGS__)))
  100. #define EVAL1(...) EVAL2(EVAL2(EVAL2(__VA_ARGS__)))
  101. #define EVAL2(...) EVAL3(EVAL3(EVAL3(__VA_ARGS__)))
  102. #define EVAL3(...) EVAL4(EVAL4(EVAL4(__VA_ARGS__)))
  103. #define EVAL4(...) EVAL5(EVAL5(EVAL5(__VA_ARGS__)))
  104. #define EVAL5(...) __VA_ARGS__
  105. #define REPEAT(count, macro, ...) \
  106. WHEN(count) \
  107. ( \
  108. OBSTRUCT(REPEAT_INDIRECT) () \
  109. ( \
  110. DEC(count), macro, __VA_ARGS__ \
  111. ) \
  112. OBSTRUCT(macro) \
  113. ( \
  114. DEC(count), __VA_ARGS__ \
  115. ) \
  116. )
  117. #define REPEAT_INDIRECT() REPEAT
  118. /**
  119. * @}
  120. */
  121. #endif // ES_UTIL_H__