expect.hpp 4.5 KB

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