expect.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // require.hpp
  3. // gameutils
  4. //
  5. // Created by Sam Jaffe on 8/19/16.
  6. //
  7. #pragma once
  8. #include <stdexcept>
  9. #undef CONCAT2
  10. #define CONCAT2(A, B) A##B
  11. #undef CONCAT
  12. #define CONCAT(A, B) CONCAT2(A, B)
  13. #undef STRING2
  14. #define STRING2(A) #A
  15. #undef STRING
  16. #define STRING(A) STRING2(A)
  17. /* expands to the first argument */
  18. #define FIRST(...) FIRST_HELPER(__VA_ARGS__, throwaway)
  19. #define FIRST_HELPER(first, ...) first
  20. #define NUM(...) SELECT_5TH(__VA_ARGS__, FOURPLUS, THREE, TWO, ONE, throwaway)
  21. #define SELECT_5TH(a1, a2, a3, a4, a5, ...) a5
  22. namespace contract {
  23. template <typename expect>
  24. class ensure_t {
  25. private:
  26. std::function<bool()> passes_;
  27. std::string message_;
  28. public:
  29. template <typename F>
  30. ensure_t(F && passes, std::string const & msg, char const * = "")
  31. : passes_(passes), message_(msg) {}
  32. ~ensure_t() noexcept(false) {
  33. if (!std::uncaught_exception() && !passes_()) {
  34. throw expect{message_};
  35. }
  36. }
  37. };
  38. template <typename except>
  39. void _contract_impl(bool expr, std::string const & msg, char const * = "") {
  40. if ( ! expr ) throw except{ msg };
  41. }
  42. template <typename except>
  43. void _contract_impl(bool expr, char const * msg, char const * = "") {
  44. if ( ! expr ) throw except{ msg };
  45. }
  46. }
  47. #define EXCEPT_T_TWO(X, Y) Y
  48. #define EXCEPT_T_THREE(X, Y, Z) Z
  49. #define EXCEPT_T_FOURPLUS(X, Y, Z, ...) Y
  50. #define EXCEPT_T(...) EXCEPT_T_HELPER(NUM(__VA_ARGS__), __VA_ARGS__)
  51. #define EXCEPT_T_HELPER(N, ...) EXCEPT_T_HELPER2(N, __VA_ARGS__)
  52. #define EXCEPT_T_HELPER2(N, ...) EXCEPT_T_##N(__VA_ARGS__)
  53. #define EXCEPT_MSG_TWO(X, Y) Y
  54. #define EXCEPT_MSG_THREE(X, Y, Z) Y
  55. #define EXCEPT_MSG_FOURPLUS(X, Y, Z, ...) Z
  56. #define EXCEPT_MSG(...) EXCEPT_MSG_HELPER(NUM(__VA_ARGS__), __VA_ARGS__)
  57. #define EXCEPT_MSG_HELPER(N, ...) EXCEPT_MSG_HELPER2(N, __VA_ARGS__)
  58. #define EXCEPT_MSG_HELPER2(N, ...) EXCEPT_MSG_##N(__VA_ARGS__)
  59. #define LOCATION_MSG ". in " __FILE__ ":" STRING( __LINE__ )
  60. #define DEF_MSG( header, expr ) \
  61. header " failed: " STRING(expr) LOCATION_MSG
  62. #define SUB_MSG( header, bool_expr, rval_expr ) \
  63. header " failed: " STRING(bool_expr) " [ with _ = " STRING(rval_expr) " ]" LOCATION_MSG
  64. /*
  65. * Usage:
  66. * expects( bool-expr )
  67. * expects( bool-expr, custom_msg )
  68. * expects( bool-expr, error_type, custom_msg )
  69. */
  70. #define expects( ... ) \
  71. contract::_contract_impl<EXCEPT_T( __VA_ARGS__, std::logic_error)>( \
  72. FIRST(__VA_ARGS__), \
  73. EXCEPT_MSG(__VA_ARGS__, \
  74. DEF_MSG("precondition", FIRST(__VA_ARGS__))) \
  75. )
  76. /*
  77. * Usage:
  78. * expects_graceful( bool-expr[, rval] )
  79. *
  80. * rval - The value to return if the expression is false.
  81. * Skip rval for a function returning void.
  82. */
  83. #define expects_graceful( expr, ... ) \
  84. if ( ! bool(expr) ) { return __VA_ARGS__; }
  85. /*
  86. * Usage:
  87. * ensures( bool-expr )
  88. * ensures( bool-expr, custom_msg )
  89. * ensures( bool-expr, error_type, custom_msg )
  90. */
  91. #define ensures( ... ) \
  92. contract::ensure_t<EXCEPT_T(__VA_ARGS__, std::runtime_error)>\
  93. CONCAT(contract_ensures_, __LINE__)( \
  94. [&]() { return FIRST(__VA_ARGS__); }, \
  95. EXCEPT_MSG(__VA_ARGS__ , \
  96. DEF_MSG("postcondition", FIRST(__VA_ARGS__))) \
  97. )
  98. /**
  99. * RVO works if expr is an rvalue, but not if it is an
  100. * lvalue. e.g:
  101. * return ensures( expression, condition );
  102. * will rvo the object (if expression is rvo-able)
  103. * but
  104. * return ensures( var, condition );
  105. * will not, instead copies
  106. * By making _ an argument, both options will move
  107. *
  108. * Usage:
  109. * return_ensures( rval-expr, ensure-expr )
  110. * return_ensures( rval-expr, ensure-expr, custom_msg )
  111. * return_ensures( rval-expr, ensure-expr, error_type, custom_msg )
  112. *
  113. * rval-expr - An expression that can be cast to the return type
  114. * of the invoking function. Bound to the token '_'.
  115. * ensure-expr - A special boolean expression using the token '_'.
  116. */
  117. #if __cplusplus < 201300
  118. #define return_ensures( expr, ... ) \
  119. [ & ]( ) { \
  120. decltype((expr)) _ = expr; \
  121. contract::_contract_impl<EXCEPT_T(__VA_ARGS__, std::runtime_error)>( \
  122. FIRST(__VA_ARGS__), \
  123. EXCEPT_MSG(__VA_ARGS__, \
  124. SUB_MSG("postcondition", FIRST(__VA_ARGS__), expr))); \
  125. return _; \
  126. }( )
  127. #else
  128. #define return_ensures( expr, cond, ... ) \
  129. [ _ = expr ]( ) { \
  130. contract::_contract_impl<EXCEPT_T(-, ##__VA_ARGS__, std::runtime_error)>( \
  131. cond, \
  132. EXCEPT_MSG(cond, ##__VA_ARGS__, \
  133. SUB_MSG("postcondition", cond, expr))); \
  134. return _; \
  135. }( )
  136. #endif