PageRenderTime 25ms CodeModel.GetById 14ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/gme/blargg_source.h

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