Mercurial > spc_convert
diff snes_spc/blargg_source.h @ 0:e38dacceb958
initial import
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 21 Oct 2011 05:53:11 -0700 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/snes_spc/blargg_source.h Fri Oct 21 05:53:11 2011 -0700 1.3 @@ -0,0 +1,100 @@ 1.4 +/* Included at the beginning of library source files, after all other #include lines. 1.5 +Sets up helpful macros and services used in my source code. They don't need 1.6 +module an annoying module prefix on their names since they are defined after 1.7 +all other #include lines. */ 1.8 + 1.9 +// snes_spc 0.9.0 1.10 +#ifndef BLARGG_SOURCE_H 1.11 +#define BLARGG_SOURCE_H 1.12 + 1.13 +// If debugging is enabled, abort program if expr is false. Meant for checking 1.14 +// internal state and consistency. A failed assertion indicates a bug in the module. 1.15 +// void assert( bool expr ); 1.16 +#include <assert.h> 1.17 + 1.18 +// If debugging is enabled and expr is false, abort program. Meant for checking 1.19 +// caller-supplied parameters and operations that are outside the control of the 1.20 +// module. A failed requirement indicates a bug outside the module. 1.21 +// void require( bool expr ); 1.22 +#undef require 1.23 +#define require( expr ) assert( expr ) 1.24 + 1.25 +// Like printf() except output goes to debug log file. Might be defined to do 1.26 +// nothing (not even evaluate its arguments). 1.27 +// void dprintf( const char* format, ... ); 1.28 +static inline void blargg_dprintf_( const char*, ... ) { } 1.29 +#undef dprintf 1.30 +#define dprintf (1) ? (void) 0 : blargg_dprintf_ 1.31 + 1.32 +// If enabled, evaluate expr and if false, make debug log entry with source file 1.33 +// and line. Meant for finding situations that should be examined further, but that 1.34 +// don't indicate a problem. In all cases, execution continues normally. 1.35 +#undef check 1.36 +#define check( expr ) ((void) 0) 1.37 + 1.38 +// If expr yields error string, return it from current function, otherwise continue. 1.39 +#undef RETURN_ERR 1.40 +#define RETURN_ERR( expr ) do { \ 1.41 + blargg_err_t blargg_return_err_ = (expr); \ 1.42 + if ( blargg_return_err_ ) return blargg_return_err_; \ 1.43 + } while ( 0 ) 1.44 + 1.45 +// If ptr is 0, return out of memory error string. 1.46 +#undef CHECK_ALLOC 1.47 +#define CHECK_ALLOC( ptr ) do { if ( (ptr) == 0 ) return "Out of memory"; } while ( 0 ) 1.48 + 1.49 +// Avoid any macros which evaluate their arguments multiple times 1.50 +#undef min 1.51 +#undef max 1.52 + 1.53 +#define DEF_MIN_MAX( type ) \ 1.54 + static inline type min( type x, type y ) { if ( x < y ) return x; return y; }\ 1.55 + static inline type max( type x, type y ) { if ( y < x ) return x; return y; } 1.56 + 1.57 +DEF_MIN_MAX( int ) 1.58 +DEF_MIN_MAX( unsigned ) 1.59 +DEF_MIN_MAX( long ) 1.60 +DEF_MIN_MAX( unsigned long ) 1.61 +DEF_MIN_MAX( float ) 1.62 +DEF_MIN_MAX( double ) 1.63 + 1.64 +#undef DEF_MIN_MAX 1.65 + 1.66 +/* 1.67 +// using const references generates crappy code, and I am currenly only using these 1.68 +// for built-in types, so they take arguments by value 1.69 + 1.70 +// TODO: remove 1.71 +inline int min( int x, int y ) 1.72 +template<class T> 1.73 +inline T min( T x, T y ) 1.74 +{ 1.75 + if ( x < y ) 1.76 + return x; 1.77 + return y; 1.78 +} 1.79 + 1.80 +template<class T> 1.81 +inline T max( T x, T y ) 1.82 +{ 1.83 + if ( x < y ) 1.84 + return y; 1.85 + return x; 1.86 +} 1.87 +*/ 1.88 + 1.89 +// TODO: good idea? bad idea? 1.90 +#undef byte 1.91 +#define byte byte_ 1.92 +typedef unsigned char byte; 1.93 + 1.94 +// deprecated 1.95 +#define BLARGG_CHECK_ALLOC CHECK_ALLOC 1.96 +#define BLARGG_RETURN_ERR RETURN_ERR 1.97 + 1.98 +// BLARGG_SOURCE_BEGIN: If defined, #included, allowing redefition of dprintf and check 1.99 +#ifdef BLARGG_SOURCE_BEGIN 1.100 + #include BLARGG_SOURCE_BEGIN 1.101 +#endif 1.102 + 1.103 +#endif