expect.hpp 838 B

1234567891011121314151617181920212223242526272829303132
  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 "macro.h"
  10. inline namespace precondition {
  11. template <typename except>
  12. void _expect(bool expr, char const * message) {
  13. if (!expr) throw except{message};
  14. }
  15. }
  16. #if defined( __clang__ ) || defined( __GNUC__ )
  17. # define LOCATION_INFO ". in " STRING(__PRETTY_FUNCTION__) "(" __FILE__ ":" STRING(__LINE__) ")"
  18. #elif defined( _MSC_VER )
  19. # define LOCATION_INFO ". in " __FUNCTION__ "(" __FILE__ ":" STRING(__LINE__) ")"
  20. #else
  21. # define LOCATION_INFO ". in " __FILE__ ":" STRING(__LINE__)
  22. #endif
  23. #define error_msg(expr, msg) msg ": " #expr LOCATION_INFO
  24. #define expects(expr, msg) expects_e(expr, std::logic_error, msg)
  25. #define expects_e(expr, except, msg) precondition::_expect<except>( expr, error_msg(expr, msg) )