expect.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #if defined( __clang__ ) || defined( __GNUC__ )
  60. # define FUNCTION STRING(__PRETTY_FUNCTION__)
  61. #elif defined( _MSC_VER )
  62. # define FUNCTION STRING(__FUNCTION__)
  63. #else
  64. # define FUNCTION "???"
  65. #endif
  66. #define LOCATION_MSG ". in " FUNCTION "(" __FILE__ ":" STRING( __LINE__ ) ")"
  67. #define DEF_MSG( header, expr ) \
  68. header " failed: " STRING(expr) LOCATION_MSG
  69. #define SUB_MSG( header, bool_expr, rval_expr ) \
  70. header " failed: " STRING(bool_expr) " [ with _ = " STRING(rval_expr) " ]" LOCATION_MSG
  71. /*
  72. * Usage:
  73. * expects( bool-expr )
  74. * expects( bool-expr, custom_msg )
  75. * expects( bool-expr, error_type, custom_msg )
  76. */
  77. #define expects( ... ) \
  78. contract::_contract_impl<EXCEPT_T( __VA_ARGS__, std::logic_error)>( \
  79. FIRST(__VA_ARGS__), \
  80. EXCEPT_MSG(__VA_ARGS__, \
  81. DEF_MSG("precondition", FIRST(__VA_ARGS__))) \
  82. )
  83. /*
  84. * Usage:
  85. * expects_graceful( bool-expr[, rval] )
  86. *
  87. * rval - The value to return if the expression is false.
  88. * Skip rval for a function returning void.
  89. */
  90. #define expects_graceful( expr, ... ) \
  91. if ( ! bool(expr) ) { return __VA_ARGS__; }
  92. /*
  93. * Usage:
  94. * ensures( bool-expr )
  95. * ensures( bool-expr, custom_msg )
  96. * ensures( bool-expr, error_type, custom_msg )
  97. */
  98. #define ensures( ... ) \
  99. contract::ensure_t<EXCEPT_T(__VA_ARGS__, std::runtime_error)>\
  100. CONCAT(contract_ensures_, __LINE__)( \
  101. [&]() { return FIRST(__VA_ARGS__); }, \
  102. EXCEPT_MSG(__VA_ARGS__ , \
  103. DEF_MSG("postcondition", FIRST(__VA_ARGS__))) \
  104. )
  105. /**
  106. * RVO works if expr is an rvalue, but not if it is an
  107. * lvalue. e.g:
  108. * return ensures( expression, condition );
  109. * will rvo the object (if expression is rvo-able)
  110. * but
  111. * return ensures( var, condition );
  112. * will not, instead copies
  113. * By making _ an argument, both options will move
  114. *
  115. * Usage:
  116. * return_ensures( rval-expr, ensure-expr )
  117. * return_ensures( rval-expr, ensure-expr, custom_msg )
  118. * return_ensures( rval-expr, ensure-expr, error_type, custom_msg )
  119. *
  120. * rval-expr - An expression that can be cast to the return type
  121. * of the invoking function. Bound to the token '_'.
  122. * ensure-expr - A special boolean expression using the token '_'.
  123. */
  124. #if __cplusplus < 201300
  125. #define return_ensures( expr, ... ) \
  126. [ & ]( ) { \
  127. decltype((expr)) _ = expr; \
  128. contract::_contract_impl<EXCEPT_T(__VA_ARGS__, std::runtime_error)>( \
  129. FIRST(__VA_ARGS__), \
  130. EXCEPT_MSG(__VA_ARGS__, \
  131. SUB_MSG("postcondition", FIRST(__VA_ARGS__), expr))); \
  132. return _; \
  133. }( )
  134. #else
  135. #define return_ensures( expr, cond, ... ) \
  136. [ _ = expr ]( ) { \
  137. contract::_contract_impl<EXCEPT_T(-, ##__VA_ARGS__, std::runtime_error)>( \
  138. cond, \
  139. EXCEPT_MSG(cond, ##__VA_ARGS__, \
  140. SUB_MSG("postcondition", cond, expr))); \
  141. return _; \
  142. }( )
  143. #endif