expect.hpp 4.5 KB

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