gmock-pp.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #ifndef GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_PP_H_
  2. #define GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_PP_H_
  3. // Expands and concatenates the arguments. Constructed macros reevaluate.
  4. #define GMOCK_PP_CAT(_1, _2) GMOCK_PP_INTERNAL_CAT(_1, _2)
  5. // Expands and stringifies the only argument.
  6. #define GMOCK_PP_STRINGIZE(...) GMOCK_PP_INTERNAL_STRINGIZE(__VA_ARGS__)
  7. // Returns empty. Given a variadic number of arguments.
  8. #define GMOCK_PP_EMPTY(...)
  9. // Returns a comma. Given a variadic number of arguments.
  10. #define GMOCK_PP_COMMA(...) ,
  11. // Returns the only argument.
  12. #define GMOCK_PP_IDENTITY(_1) _1
  13. // Evaluates to the number of arguments after expansion.
  14. //
  15. // #define PAIR x, y
  16. //
  17. // GMOCK_PP_NARG() => 1
  18. // GMOCK_PP_NARG(x) => 1
  19. // GMOCK_PP_NARG(x, y) => 2
  20. // GMOCK_PP_NARG(PAIR) => 2
  21. //
  22. // Requires: the number of arguments after expansion is at most 15.
  23. #define GMOCK_PP_NARG(...) \
  24. GMOCK_PP_INTERNAL_16TH( \
  25. (__VA_ARGS__, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0))
  26. // Returns 1 if the expansion of arguments has an unprotected comma. Otherwise
  27. // returns 0. Requires no more than 15 unprotected commas.
  28. #define GMOCK_PP_HAS_COMMA(...) \
  29. GMOCK_PP_INTERNAL_16TH( \
  30. (__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0))
  31. // Returns the first argument.
  32. #define GMOCK_PP_HEAD(...) GMOCK_PP_INTERNAL_HEAD((__VA_ARGS__, unusedArg))
  33. // Returns the tail. A variadic list of all arguments minus the first. Requires
  34. // at least one argument.
  35. #define GMOCK_PP_TAIL(...) GMOCK_PP_INTERNAL_TAIL((__VA_ARGS__))
  36. // Calls CAT(_Macro, NARG(__VA_ARGS__))(__VA_ARGS__)
  37. #define GMOCK_PP_VARIADIC_CALL(_Macro, ...) \
  38. GMOCK_PP_IDENTITY( \
  39. GMOCK_PP_CAT(_Macro, GMOCK_PP_NARG(__VA_ARGS__))(__VA_ARGS__))
  40. // If the arguments after expansion have no tokens, evaluates to `1`. Otherwise
  41. // evaluates to `0`.
  42. //
  43. // Requires: * the number of arguments after expansion is at most 15.
  44. // * If the argument is a macro, it must be able to be called with one
  45. // argument.
  46. //
  47. // Implementation details:
  48. //
  49. // There is one case when it generates a compile error: if the argument is macro
  50. // that cannot be called with one argument.
  51. //
  52. // #define M(a, b) // it doesn't matter what it expands to
  53. //
  54. // // Expected: expands to `0`.
  55. // // Actual: compile error.
  56. // GMOCK_PP_IS_EMPTY(M)
  57. //
  58. // There are 4 cases tested:
  59. //
  60. // * __VA_ARGS__ possible expansion has no unparen'd commas. Expected 0.
  61. // * __VA_ARGS__ possible expansion is not enclosed in parenthesis. Expected 0.
  62. // * __VA_ARGS__ possible expansion is not a macro that ()-evaluates to a comma.
  63. // Expected 0
  64. // * __VA_ARGS__ is empty, or has unparen'd commas, or is enclosed in
  65. // parenthesis, or is a macro that ()-evaluates to comma. Expected 1.
  66. //
  67. // We trigger detection on '0001', i.e. on empty.
  68. #define GMOCK_PP_IS_EMPTY(...) \
  69. GMOCK_PP_INTERNAL_IS_EMPTY(GMOCK_PP_HAS_COMMA(__VA_ARGS__), \
  70. GMOCK_PP_HAS_COMMA(GMOCK_PP_COMMA __VA_ARGS__), \
  71. GMOCK_PP_HAS_COMMA(__VA_ARGS__()), \
  72. GMOCK_PP_HAS_COMMA(GMOCK_PP_COMMA __VA_ARGS__()))
  73. // Evaluates to _Then if _Cond is 1 and _Else if _Cond is 0.
  74. #define GMOCK_PP_IF(_Cond, _Then, _Else) \
  75. GMOCK_PP_CAT(GMOCK_PP_INTERNAL_IF_, _Cond)(_Then, _Else)
  76. // Similar to GMOCK_PP_IF but takes _Then and _Else in parentheses.
  77. //
  78. // GMOCK_PP_GENERIC_IF(1, (a, b, c), (d, e, f)) => a, b, c
  79. // GMOCK_PP_GENERIC_IF(0, (a, b, c), (d, e, f)) => d, e, f
  80. //
  81. #define GMOCK_PP_GENERIC_IF(_Cond, _Then, _Else) \
  82. GMOCK_PP_REMOVE_PARENS(GMOCK_PP_IF(_Cond, _Then, _Else))
  83. // Evaluates to the number of arguments after expansion. Identifies 'empty' as
  84. // 0.
  85. //
  86. // #define PAIR x, y
  87. //
  88. // GMOCK_PP_NARG0() => 0
  89. // GMOCK_PP_NARG0(x) => 1
  90. // GMOCK_PP_NARG0(x, y) => 2
  91. // GMOCK_PP_NARG0(PAIR) => 2
  92. //
  93. // Requires: * the number of arguments after expansion is at most 15.
  94. // * If the argument is a macro, it must be able to be called with one
  95. // argument.
  96. #define GMOCK_PP_NARG0(...) \
  97. GMOCK_PP_IF(GMOCK_PP_IS_EMPTY(__VA_ARGS__), 0, GMOCK_PP_NARG(__VA_ARGS__))
  98. // Expands to 1 if the first argument starts with something in parentheses,
  99. // otherwise to 0.
  100. #define GMOCK_PP_IS_BEGIN_PARENS(...) \
  101. GMOCK_PP_HEAD(GMOCK_PP_CAT(GMOCK_PP_INTERNAL_IBP_IS_VARIADIC_R_, \
  102. GMOCK_PP_INTERNAL_IBP_IS_VARIADIC_C __VA_ARGS__))
  103. // Expands to 1 is there is only one argument and it is enclosed in parentheses.
  104. #define GMOCK_PP_IS_ENCLOSED_PARENS(...) \
  105. GMOCK_PP_IF(GMOCK_PP_IS_BEGIN_PARENS(__VA_ARGS__), \
  106. GMOCK_PP_IS_EMPTY(GMOCK_PP_EMPTY __VA_ARGS__), 0)
  107. // Remove the parens, requires GMOCK_PP_IS_ENCLOSED_PARENS(args) => 1.
  108. #define GMOCK_PP_REMOVE_PARENS(...) GMOCK_PP_INTERNAL_REMOVE_PARENS __VA_ARGS__
  109. // Expands to _Macro(0, _Data, e1) _Macro(1, _Data, e2) ... _Macro(K -1, _Data,
  110. // eK) as many of GMOCK_INTERNAL_NARG0 _Tuple.
  111. // Requires: * |_Macro| can be called with 3 arguments.
  112. // * |_Tuple| expansion has no more than 15 elements.
  113. #define GMOCK_PP_FOR_EACH(_Macro, _Data, _Tuple) \
  114. GMOCK_PP_CAT(GMOCK_PP_INTERNAL_FOR_EACH_IMPL_, GMOCK_PP_NARG0 _Tuple) \
  115. (0, _Macro, _Data, _Tuple)
  116. // Expands to _Macro(0, _Data, ) _Macro(1, _Data, ) ... _Macro(K - 1, _Data, )
  117. // Empty if _K = 0.
  118. // Requires: * |_Macro| can be called with 3 arguments.
  119. // * |_K| literal between 0 and 15
  120. #define GMOCK_PP_REPEAT(_Macro, _Data, _N) \
  121. GMOCK_PP_CAT(GMOCK_PP_INTERNAL_FOR_EACH_IMPL_, _N) \
  122. (0, _Macro, _Data, GMOCK_PP_INTENRAL_EMPTY_TUPLE)
  123. // Increments the argument, requires the argument to be between 0 and 15.
  124. #define GMOCK_PP_INC(_i) GMOCK_PP_CAT(GMOCK_PP_INTERNAL_INC_, _i)
  125. // Returns comma if _i != 0. Requires _i to be between 0 and 15.
  126. #define GMOCK_PP_COMMA_IF(_i) GMOCK_PP_CAT(GMOCK_PP_INTERNAL_COMMA_IF_, _i)
  127. // Internal details follow. Do not use any of these symbols outside of this
  128. // file or we will break your code.
  129. #define GMOCK_PP_INTENRAL_EMPTY_TUPLE (, , , , , , , , , , , , , , , )
  130. #define GMOCK_PP_INTERNAL_CAT(_1, _2) _1##_2
  131. #define GMOCK_PP_INTERNAL_STRINGIZE(...) #__VA_ARGS__
  132. #define GMOCK_PP_INTERNAL_CAT_5(_1, _2, _3, _4, _5) _1##_2##_3##_4##_5
  133. #define GMOCK_PP_INTERNAL_IS_EMPTY(_1, _2, _3, _4) \
  134. GMOCK_PP_HAS_COMMA(GMOCK_PP_INTERNAL_CAT_5(GMOCK_PP_INTERNAL_IS_EMPTY_CASE_, \
  135. _1, _2, _3, _4))
  136. #define GMOCK_PP_INTERNAL_IS_EMPTY_CASE_0001 ,
  137. #define GMOCK_PP_INTERNAL_IF_1(_Then, _Else) _Then
  138. #define GMOCK_PP_INTERNAL_IF_0(_Then, _Else) _Else
  139. // Because of MSVC treating a token with a comma in it as a single token when
  140. // passed to another macro, we need to force it to evaluate it as multiple
  141. // tokens. We do that by using a "IDENTITY(MACRO PARENTHESIZED_ARGS)" macro. We
  142. // define one per possible macro that relies on this behavior. Note "_Args" must
  143. // be parenthesized.
  144. #define GMOCK_PP_INTERNAL_INTERNAL_16TH(_1, _2, _3, _4, _5, _6, _7, _8, _9, \
  145. _10, _11, _12, _13, _14, _15, _16, \
  146. ...) \
  147. _16
  148. #define GMOCK_PP_INTERNAL_16TH(_Args) \
  149. GMOCK_PP_IDENTITY(GMOCK_PP_INTERNAL_INTERNAL_16TH _Args)
  150. #define GMOCK_PP_INTERNAL_INTERNAL_HEAD(_1, ...) _1
  151. #define GMOCK_PP_INTERNAL_HEAD(_Args) \
  152. GMOCK_PP_IDENTITY(GMOCK_PP_INTERNAL_INTERNAL_HEAD _Args)
  153. #define GMOCK_PP_INTERNAL_INTERNAL_TAIL(_1, ...) __VA_ARGS__
  154. #define GMOCK_PP_INTERNAL_TAIL(_Args) \
  155. GMOCK_PP_IDENTITY(GMOCK_PP_INTERNAL_INTERNAL_TAIL _Args)
  156. #define GMOCK_PP_INTERNAL_IBP_IS_VARIADIC_C(...) 1 _
  157. #define GMOCK_PP_INTERNAL_IBP_IS_VARIADIC_R_1 1,
  158. #define GMOCK_PP_INTERNAL_IBP_IS_VARIADIC_R_GMOCK_PP_INTERNAL_IBP_IS_VARIADIC_C \
  159. 0,
  160. #define GMOCK_PP_INTERNAL_REMOVE_PARENS(...) __VA_ARGS__
  161. #define GMOCK_PP_INTERNAL_INC_0 1
  162. #define GMOCK_PP_INTERNAL_INC_1 2
  163. #define GMOCK_PP_INTERNAL_INC_2 3
  164. #define GMOCK_PP_INTERNAL_INC_3 4
  165. #define GMOCK_PP_INTERNAL_INC_4 5
  166. #define GMOCK_PP_INTERNAL_INC_5 6
  167. #define GMOCK_PP_INTERNAL_INC_6 7
  168. #define GMOCK_PP_INTERNAL_INC_7 8
  169. #define GMOCK_PP_INTERNAL_INC_8 9
  170. #define GMOCK_PP_INTERNAL_INC_9 10
  171. #define GMOCK_PP_INTERNAL_INC_10 11
  172. #define GMOCK_PP_INTERNAL_INC_11 12
  173. #define GMOCK_PP_INTERNAL_INC_12 13
  174. #define GMOCK_PP_INTERNAL_INC_13 14
  175. #define GMOCK_PP_INTERNAL_INC_14 15
  176. #define GMOCK_PP_INTERNAL_INC_15 16
  177. #define GMOCK_PP_INTERNAL_COMMA_IF_0
  178. #define GMOCK_PP_INTERNAL_COMMA_IF_1 ,
  179. #define GMOCK_PP_INTERNAL_COMMA_IF_2 ,
  180. #define GMOCK_PP_INTERNAL_COMMA_IF_3 ,
  181. #define GMOCK_PP_INTERNAL_COMMA_IF_4 ,
  182. #define GMOCK_PP_INTERNAL_COMMA_IF_5 ,
  183. #define GMOCK_PP_INTERNAL_COMMA_IF_6 ,
  184. #define GMOCK_PP_INTERNAL_COMMA_IF_7 ,
  185. #define GMOCK_PP_INTERNAL_COMMA_IF_8 ,
  186. #define GMOCK_PP_INTERNAL_COMMA_IF_9 ,
  187. #define GMOCK_PP_INTERNAL_COMMA_IF_10 ,
  188. #define GMOCK_PP_INTERNAL_COMMA_IF_11 ,
  189. #define GMOCK_PP_INTERNAL_COMMA_IF_12 ,
  190. #define GMOCK_PP_INTERNAL_COMMA_IF_13 ,
  191. #define GMOCK_PP_INTERNAL_COMMA_IF_14 ,
  192. #define GMOCK_PP_INTERNAL_COMMA_IF_15 ,
  193. #define GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, _element) \
  194. _Macro(_i, _Data, _element)
  195. #define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_0(_i, _Macro, _Data, _Tuple)
  196. #define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_1(_i, _Macro, _Data, _Tuple) \
  197. GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple)
  198. #define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_2(_i, _Macro, _Data, _Tuple) \
  199. GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
  200. GMOCK_PP_INTERNAL_FOR_EACH_IMPL_1(GMOCK_PP_INC(_i), _Macro, _Data, \
  201. (GMOCK_PP_TAIL _Tuple))
  202. #define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_3(_i, _Macro, _Data, _Tuple) \
  203. GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
  204. GMOCK_PP_INTERNAL_FOR_EACH_IMPL_2(GMOCK_PP_INC(_i), _Macro, _Data, \
  205. (GMOCK_PP_TAIL _Tuple))
  206. #define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_4(_i, _Macro, _Data, _Tuple) \
  207. GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
  208. GMOCK_PP_INTERNAL_FOR_EACH_IMPL_3(GMOCK_PP_INC(_i), _Macro, _Data, \
  209. (GMOCK_PP_TAIL _Tuple))
  210. #define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_5(_i, _Macro, _Data, _Tuple) \
  211. GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
  212. GMOCK_PP_INTERNAL_FOR_EACH_IMPL_4(GMOCK_PP_INC(_i), _Macro, _Data, \
  213. (GMOCK_PP_TAIL _Tuple))
  214. #define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_6(_i, _Macro, _Data, _Tuple) \
  215. GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
  216. GMOCK_PP_INTERNAL_FOR_EACH_IMPL_5(GMOCK_PP_INC(_i), _Macro, _Data, \
  217. (GMOCK_PP_TAIL _Tuple))
  218. #define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_7(_i, _Macro, _Data, _Tuple) \
  219. GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
  220. GMOCK_PP_INTERNAL_FOR_EACH_IMPL_6(GMOCK_PP_INC(_i), _Macro, _Data, \
  221. (GMOCK_PP_TAIL _Tuple))
  222. #define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_8(_i, _Macro, _Data, _Tuple) \
  223. GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
  224. GMOCK_PP_INTERNAL_FOR_EACH_IMPL_7(GMOCK_PP_INC(_i), _Macro, _Data, \
  225. (GMOCK_PP_TAIL _Tuple))
  226. #define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_9(_i, _Macro, _Data, _Tuple) \
  227. GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
  228. GMOCK_PP_INTERNAL_FOR_EACH_IMPL_8(GMOCK_PP_INC(_i), _Macro, _Data, \
  229. (GMOCK_PP_TAIL _Tuple))
  230. #define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_10(_i, _Macro, _Data, _Tuple) \
  231. GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
  232. GMOCK_PP_INTERNAL_FOR_EACH_IMPL_9(GMOCK_PP_INC(_i), _Macro, _Data, \
  233. (GMOCK_PP_TAIL _Tuple))
  234. #define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_11(_i, _Macro, _Data, _Tuple) \
  235. GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
  236. GMOCK_PP_INTERNAL_FOR_EACH_IMPL_10(GMOCK_PP_INC(_i), _Macro, _Data, \
  237. (GMOCK_PP_TAIL _Tuple))
  238. #define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_12(_i, _Macro, _Data, _Tuple) \
  239. GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
  240. GMOCK_PP_INTERNAL_FOR_EACH_IMPL_11(GMOCK_PP_INC(_i), _Macro, _Data, \
  241. (GMOCK_PP_TAIL _Tuple))
  242. #define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_13(_i, _Macro, _Data, _Tuple) \
  243. GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
  244. GMOCK_PP_INTERNAL_FOR_EACH_IMPL_12(GMOCK_PP_INC(_i), _Macro, _Data, \
  245. (GMOCK_PP_TAIL _Tuple))
  246. #define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_14(_i, _Macro, _Data, _Tuple) \
  247. GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
  248. GMOCK_PP_INTERNAL_FOR_EACH_IMPL_13(GMOCK_PP_INC(_i), _Macro, _Data, \
  249. (GMOCK_PP_TAIL _Tuple))
  250. #define GMOCK_PP_INTERNAL_FOR_EACH_IMPL_15(_i, _Macro, _Data, _Tuple) \
  251. GMOCK_PP_INTERNAL_CALL_MACRO(_Macro, _i, _Data, GMOCK_PP_HEAD _Tuple) \
  252. GMOCK_PP_INTERNAL_FOR_EACH_IMPL_14(GMOCK_PP_INC(_i), _Macro, _Data, \
  253. (GMOCK_PP_TAIL _Tuple))
  254. #endif // GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_PP_H_