pp.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*=============================================================================
  2. Copyright (c) 2014 Paul Fultz II
  3. pp.h
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #ifndef BOOST_HOF_GUARD_PP_H
  8. #define BOOST_HOF_GUARD_PP_H
  9. #define BOOST_HOF_PP_CAT(x, y) BOOST_HOF_PP_PRIMITIVE_CAT(x, y)
  10. #define BOOST_HOF_PP_PRIMITIVE_CAT(x, y) x ## y
  11. #define BOOST_HOF_PP_SEQ_ITERATE(...) BOOST_HOF_PP_PRIMITIVE_SEQ_ITERATE(__VA_ARGS__)
  12. #define BOOST_HOF_PP_PRIMITIVE_SEQ_ITERATE(...) __VA_ARGS__ ## _END
  13. //
  14. // BOOST_HOF_PP_NARGS returns the number of args in __VA_ARGS__
  15. //
  16. #define BOOST_HOF_PP_NARGS(...) \
  17. BOOST_HOF_PP_DETAIL_NARG((__VA_ARGS__,BOOST_HOF_PP_DETAIL_RSEQ_N()))
  18. #define BOOST_HOF_PP_DETAIL_NARG(args) \
  19. BOOST_HOF_PP_DETAIL_ARG_N args
  20. #define BOOST_HOF_PP_DETAIL_ARG_N( \
  21. _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
  22. _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
  23. _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
  24. _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
  25. _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
  26. _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
  27. _61,_62,_63,N,...) N
  28. #define BOOST_HOF_PP_DETAIL_RSEQ_N() \
  29. 63,62,61,60, \
  30. 59,58,57,56,55,54,53,52,51,50, \
  31. 49,48,47,46,45,44,43,42,41,40, \
  32. 39,38,37,36,35,34,33,32,31,30, \
  33. 29,28,27,26,25,24,23,22,21,20, \
  34. 19,18,17,16,15,14,13,12,11,10, \
  35. 9,8,7,6,5,4,3,2,1,0
  36. //
  37. // BOOST_HOF_PP_IS_PAREN is used to detect if the first token is a parenthesis.
  38. // It expands to 1 if it is, otherwise it expands to 0.
  39. //
  40. #define BOOST_HOF_PP_IS_PAREN(x) BOOST_HOF_PP_IS_PAREN_CHECK(BOOST_HOF_PP_IS_PAREN_PROBE x)
  41. #define BOOST_HOF_PP_IS_PAREN_CHECK(...) BOOST_HOF_PP_IS_PAREN_CHECK_N(__VA_ARGS__,0)
  42. #define BOOST_HOF_PP_IS_PAREN_PROBE(...) ~, 1,
  43. #ifndef _MSC_VER
  44. #define BOOST_HOF_PP_IS_PAREN_CHECK_N(x, n, ...) n
  45. #else
  46. // MSVC workarounds
  47. #define BOOST_HOF_PP_IS_PAREN_CHECK_RES(x) x
  48. #define BOOST_HOF_PP_IS_PAREN_CHECK_II(x, n, ...) n
  49. #define BOOST_HOF_PP_IS_PAREN_CHECK_I(x) BOOST_HOF_PP_IS_PAREN_CHECK_RES(BOOST_HOF_PP_IS_PAREN_CHECK_II x)
  50. #define BOOST_HOF_PP_IS_PAREN_CHECK_N(...) BOOST_HOF_PP_IS_PAREN_CHECK_I((__VA_ARGS__))
  51. #endif
  52. //
  53. // BOOST_HOF_PP_IS_1 is used to detect if the first token is a 1.
  54. // It expands to 1 if it is, otherwise it expands to 0.
  55. //
  56. #define BOOST_HOF_PP_IS_1(x) BOOST_HOF_PP_IS_PAREN(BOOST_HOF_PP_CAT(BOOST_HOF_PP_IS_1_PROBE_, x))
  57. #define BOOST_HOF_PP_IS_1_PROBE_1 ()
  58. #define BOOST_HOF_PP_ARGS_IS_SINGLE(...) BOOST_HOF_PP_IS_1(BOOST_HOF_PP_NARGS(__VA_ARGS__))
  59. #define BOOST_HOF_PP_EMPTY(...)
  60. #define BOOST_HOF_PP_DEFER(...) __VA_ARGS__ BOOST_HOF_PP_EMPTY()
  61. #define BOOST_HOF_PP_OBSTRUCT(...) __VA_ARGS__ BOOST_HOF_PP_DEFER(BOOST_HOF_PP_EMPTY)()
  62. #define BOOST_HOF_PP_EXPAND(...) __VA_ARGS__
  63. #define BOOST_HOF_PP_IIF(c) BOOST_HOF_PP_PRIMITIVE_CAT(BOOST_HOF_PP_IIF_, c)
  64. #define BOOST_HOF_PP_IIF_0(t, ...) __VA_ARGS__
  65. #define BOOST_HOF_PP_IIF_1(t, ...) t
  66. #define BOOST_HOF_PP_WALL(...) __VA_ARGS__
  67. #define BOOST_HOF_PP_RAIL_IIF(c) BOOST_HOF_PP_PRIMITIVE_CAT(BOOST_HOF_PP_RAIL_IIF_, c)
  68. #define BOOST_HOF_PP_RAIL_IIF_0(t, ...) __VA_ARGS__
  69. #define BOOST_HOF_PP_RAIL_IIF_1(t, ...) t
  70. #define BOOST_HOF_PP_RAIL(macro) \
  71. BOOST_HOF_PP_RAIL_IIF(BOOST_HOF_PP_IS_PAREN(BOOST_HOF_PP_WALL(())))( \
  72. BOOST_HOF_PP_RAIL_ID BOOST_HOF_PP_OBSTRUCT()()(macro), \
  73. macro BOOST_HOF_PP_OBSTRUCT() \
  74. )
  75. #define BOOST_HOF_PP_RAIL_ID() BOOST_HOF_PP_RAIL
  76. #endif