t_1_024.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. http://www.boost.org/
  4. Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
  5. Software License, Version 1.0. (See accompanying file
  6. LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. // Tests more complex macro expansion.
  9. // token-pasting macro
  10. #define CAT(a, b) PRIMITIVE_CAT(a, b)
  11. #define PRIMITIVE_CAT(a, b) a ## b
  12. // splits a value that is about to expand into two parameters and returns
  13. // either the zero-th or one-th element.
  14. #define SPLIT(n, im) PRIMITIVE_CAT(SPLIT_, n)(im)
  15. #define SPLIT_0(a, b) a
  16. #define SPLIT_1(a, b) b
  17. // detects if the parameter is nullary parentheses () or something else.
  18. // passing non-nullary parenthesis is invalid input.
  19. #define IS_NULLARY(expr) \
  20. SPLIT( \
  21. 0, \
  22. CAT(IS_NULLARY_R_, IS_NULLARY_T expr) \
  23. ) \
  24. /**/
  25. #define IS_NULLARY_T() 1
  26. #define IS_NULLARY_R_1 1, ?
  27. #define IS_NULLARY_R_IS_NULLARY_T 0, ?
  28. // expands to a macro that eats an n-element parenthesized expression.
  29. #define EAT(n) PRIMITIVE_CAT(EAT_, n)
  30. #define EAT_0()
  31. #define EAT_1(a)
  32. #define EAT_2(a, b)
  33. #define EAT_3(a, b, c)
  34. // expands to a macro that removes the parentheses from an n-element
  35. // parenthesized expression
  36. #define REM(n) PRIMITIVE_CAT(REM_, n)
  37. #define REM_0()
  38. #define REM_1(a) a
  39. #define REM_2(a, b) a, b
  40. #define REM_3(a, b, c) a, b, c
  41. // expands to nothing
  42. #define NIL
  43. // expands to 1 if x is less than y otherwise, it expands to 0
  44. #define LESS(x, y) \
  45. IS_NULLARY( \
  46. PRIMITIVE_CAT(LESS_, y)( \
  47. EAT(1), PRIMITIVE_CAT(LESS_, x) \
  48. )() \
  49. ) \
  50. /**/
  51. #define LESS_0(a, b) a(EAT(2)) b(REM(1), NIL)
  52. #define LESS_1(a, b) a(LESS_0) b(REM(1), NIL)
  53. #define LESS_2(a, b) a(LESS_1) b(REM(1), NIL)
  54. #define LESS_3(a, b) a(LESS_2) b(REM(1), NIL)
  55. #define LESS_4(a, b) a(LESS_3) b(REM(1), NIL)
  56. #define LESS_5(a, b) a(LESS_4) b(REM(1), NIL)
  57. // expands to the binary one's compliment of a binary input value. i.e. 0 or 1
  58. #define COMPL(n) PRIMITIVE_CAT(COMPL_, n)
  59. #define COMPL_0 1
  60. #define COMPL_1 0
  61. // these do the obvious...
  62. #define GREATER(x, y) LESS(y, x)
  63. #define LESS_EQUAL(x, y) COMPL(LESS(y, x))
  64. #define GREATER_EQUAL(x, y) COMPL(LESS(x, y))
  65. // causes another rescan...
  66. #define SCAN(x) x
  67. // expands to 1 if x is not equal to y. this one contains a workaround...
  68. #define NOT_EQUAL(x, y) \
  69. IS_NULLARY( \
  70. SCAN( \
  71. PRIMITIVE_CAT(LESS_, x)( \
  72. EAT(1), \
  73. PRIMITIVE_CAT(LESS_, y) EAT(2) \
  74. )((), ...) \
  75. ) \
  76. ) \
  77. /**/
  78. #define EQUAL(x, y) COMPL(NOT_EQUAL(x, y))
  79. //R #line 95 "t_1_024.cpp"
  80. LESS(2, 3) //R 1
  81. LESS(3, 2) //R 0
  82. LESS(3, 3) //R 0
  83. GREATER(2, 3) //R 0
  84. GREATER(3, 2) //R 1
  85. GREATER(3, 3) //R 0
  86. LESS_EQUAL(2, 3) //R 1
  87. LESS_EQUAL(3, 2) //R 0
  88. LESS_EQUAL(3, 3) //R 1
  89. GREATER_EQUAL(2, 3) //R 0
  90. GREATER_EQUAL(3, 2) //R 1
  91. GREATER_EQUAL(3, 3) //R 1
  92. NOT_EQUAL(2, 3) //R 1
  93. NOT_EQUAL(3, 2) //R 1
  94. NOT_EQUAL(3, 3) //R 0
  95. EQUAL(2, 3) //R 0
  96. EQUAL(3, 2) //R 0
  97. EQUAL(3, 3) //R 1