blargg_source.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Included at the beginning of library source files, after all other #include lines.
  2. Sets up helpful macros and services used in my source code. They don't need
  3. module an annoying module prefix on their names since they are defined after
  4. all other #include lines. */
  5. #ifndef BLARGG_SOURCE_H
  6. #define BLARGG_SOURCE_H
  7. // If debugging is enabled, abort program if expr is false. Meant for checking
  8. // internal state and consistency. A failed assertion indicates a bug in the module.
  9. // void assert( bool expr );
  10. #include <assert.h>
  11. // If debugging is enabled and expr is false, abort program. Meant for checking
  12. // caller-supplied parameters and operations that are outside the control of the
  13. // module. A failed requirement indicates a bug outside the module.
  14. // void require( bool expr );
  15. #undef require
  16. #define require( expr ) assert( expr )
  17. // Like printf() except output goes to debug log file. Might be defined to do
  18. // nothing (not even evaluate its arguments).
  19. // void debug_printf( const char* format, ... );
  20. static inline void blargg_dprintf_( const char*, ... ) { }
  21. #undef debug_printf
  22. #define debug_printf (1) ? (void) 0 : blargg_dprintf_
  23. // If enabled, evaluate expr and if false, make debug log entry with source file
  24. // and line. Meant for finding situations that should be examined further, but that
  25. // don't indicate a problem. In all cases, execution continues normally.
  26. #undef check
  27. #define check( expr ) ((void) 0)
  28. // If expr yields error string, return it from current function, otherwise continue.
  29. #undef RETURN_ERR
  30. #define RETURN_ERR( expr ) do { \
  31. blargg_err_t blargg_return_err_ = (expr); \
  32. if ( blargg_return_err_ ) return blargg_return_err_; \
  33. } while ( 0 )
  34. // If ptr is 0, return out of memory error string.
  35. #undef CHECK_ALLOC
  36. #define CHECK_ALLOC( ptr ) do { if ( (ptr) == 0 ) return "Out of memory"; } while ( 0 )
  37. // Avoid any macros which evaluate their arguments multiple times
  38. #undef min
  39. #undef max
  40. #define DEF_MIN_MAX( type ) \
  41. static inline type min( type x, type y ) { if ( x < y ) return x; return y; }\
  42. static inline type max( type x, type y ) { if ( y < x ) return x; return y; }
  43. DEF_MIN_MAX( int )
  44. DEF_MIN_MAX( unsigned )
  45. DEF_MIN_MAX( long )
  46. DEF_MIN_MAX( unsigned long )
  47. DEF_MIN_MAX( float )
  48. DEF_MIN_MAX( double )
  49. #undef DEF_MIN_MAX
  50. /*
  51. // using const references generates crappy code, and I am currenly only using these
  52. // for built-in types, so they take arguments by value
  53. // TODO: remove
  54. inline int min( int x, int y )
  55. template<class T>
  56. inline T min( T x, T y )
  57. {
  58. if ( x < y )
  59. return x;
  60. return y;
  61. }
  62. template<class T>
  63. inline T max( T x, T y )
  64. {
  65. if ( x < y )
  66. return y;
  67. return x;
  68. }
  69. */
  70. // TODO: good idea? bad idea?
  71. #undef byte
  72. #define byte byte_
  73. typedef unsigned char byte;
  74. // Setup compiler defines useful for exporting required public API symbols in gme.cpp
  75. #ifndef BLARGG_EXPORT
  76. #if defined (_WIN32) && defined(BLARGG_BUILD_DLL)
  77. #define BLARGG_EXPORT __declspec(dllexport)
  78. #elif defined (LIBGME_VISIBILITY)
  79. #define BLARGG_EXPORT __attribute__((visibility ("default")))
  80. #else
  81. #define BLARGG_EXPORT
  82. #endif
  83. #endif
  84. // deprecated
  85. #define BLARGG_CHECK_ALLOC CHECK_ALLOC
  86. #define BLARGG_RETURN_ERR RETURN_ERR
  87. // BLARGG_SOURCE_BEGIN: If defined, #included, allowing redefition of debug_printf and check
  88. #ifdef BLARGG_SOURCE_BEGIN
  89. #include BLARGG_SOURCE_BEGIN
  90. #endif
  91. #endif