cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

blargg_common.h (5801B)


      1// Sets up common environment for Shay Green's libraries.
      2// To change configuration options, modify blargg_config.h, not this file.
      3
      4// Gb_Snd_Emu 0.2.0
      5#ifndef BLARGG_COMMON_H
      6#define BLARGG_COMMON_H
      7
      8#include <stddef.h>
      9#include <stdlib.h>
     10#include <assert.h>
     11#include <limits.h>
     12
     13#undef BLARGG_COMMON_H
     14// allow blargg_config.h to #include blargg_common.h
     15#include "blargg_config.h"
     16#ifndef BLARGG_COMMON_H
     17#define BLARGG_COMMON_H
     18
     19// BLARGG_RESTRICT: equivalent to restrict, where supported
     20#if __GNUC__ >= 3 || _MSC_VER >= 1100
     21	#define BLARGG_RESTRICT __restrict
     22#else
     23	#define BLARGG_RESTRICT
     24#endif
     25
     26// STATIC_CAST(T,expr): Used in place of static_cast<T> (expr)
     27// CONST_CAST( T,expr): Used in place of const_cast<T> (expr)
     28#ifndef STATIC_CAST
     29	#if __GNUC__ >= 4
     30		#define STATIC_CAST(T,expr) static_cast<T> (expr)
     31		#define CONST_CAST( T,expr) const_cast<T> (expr)
     32	#else
     33		#define STATIC_CAST(T,expr) ((T) (expr))
     34		#define CONST_CAST( T,expr) ((T) (expr))
     35	#endif
     36#endif
     37
     38// blargg_err_t (0 on success, otherwise error string)
     39#ifndef blargg_err_t
     40	typedef const char* blargg_err_t;
     41#endif
     42
     43// blargg_vector - very lightweight vector of POD types (no constructor/destructor)
     44template<class T>
     45class blargg_vector {
     46	T* begin_;
     47	size_t size_;
     48public:
     49	blargg_vector() : begin_( 0 ), size_( 0 ) { }
     50	~blargg_vector() { free( begin_ ); }
     51	size_t size() const { return size_; }
     52	T* begin() const { return begin_; }
     53	T* end() const { return begin_ + size_; }
     54	blargg_err_t resize( size_t n )
     55	{
     56		// TODO: blargg_common.cpp to hold this as an outline function, ugh
     57		void* p = realloc( begin_, n * sizeof (T) );
     58		if ( p )
     59			begin_ = (T*) p;
     60		else if ( n > size_ ) // realloc failure only a problem if expanding
     61			return "Out of memory";
     62		size_ = n;
     63		return 0;
     64	}
     65	void clear() { void* p = begin_; begin_ = 0; size_ = 0; free( p ); }
     66	T& operator [] ( size_t n ) const
     67	{
     68		assert( n <= size_ ); // <= to allow past-the-end value
     69		return begin_ [n];
     70	}
     71};
     72
     73#ifndef BLARGG_DISABLE_NOTHROW
     74	// throw spec mandatory in ISO C++ if operator new can return NULL
     75	#if __cplusplus >= 199711 || __GNUC__ >= 3
     76		#define BLARGG_THROWS( spec ) throw spec
     77	#else
     78		#define BLARGG_THROWS( spec )
     79	#endif
     80	#define BLARGG_DISABLE_NOTHROW \
     81		void* operator new ( size_t s ) BLARGG_THROWS(()) { return malloc( s ); }\
     82		void operator delete ( void* p ) { free( p ); }
     83	#define BLARGG_NEW new
     84#else
     85	#include <new>
     86	#define BLARGG_NEW new (std::nothrow)
     87#endif
     88
     89// BLARGG_4CHAR('a','b','c','d') = 'abcd' (four character integer constant)
     90#define BLARGG_4CHAR( a, b, c, d ) \
     91	((a&0xFF)*0x1000000 + (b&0xFF)*0x10000 + (c&0xFF)*0x100 + (d&0xFF))
     92
     93// BOOST_STATIC_ASSERT( expr ): Generates compile error if expr is 0.
     94#ifndef BOOST_STATIC_ASSERT
     95	#ifdef _MSC_VER
     96		// MSVC6 (_MSC_VER < 1300) fails for use of __LINE__ when /Zl is specified
     97		#define BOOST_STATIC_ASSERT( expr ) \
     98			void blargg_failed_( int (*arg) [2 / (int) !!(expr) - 1] )
     99	#else
    100		// Some other compilers fail when declaring same function multiple times in class,
    101		// so differentiate them by line
    102		#define BOOST_STATIC_ASSERT( expr ) \
    103			void blargg_failed_( int (*arg) [2 / !!(expr) - 1] [__LINE__] )
    104	#endif
    105#endif
    106
    107// BLARGG_COMPILER_HAS_BOOL: If 0, provides bool support for old compiler. If 1,
    108// compiler is assumed to support bool. If undefined, availability is determined.
    109#ifndef BLARGG_COMPILER_HAS_BOOL
    110	#if defined (__MWERKS__)
    111		#if !__option(bool)
    112			#define BLARGG_COMPILER_HAS_BOOL 0
    113		#endif
    114	#elif defined (_MSC_VER)
    115		#if _MSC_VER < 1100
    116			#define BLARGG_COMPILER_HAS_BOOL 0
    117		#endif
    118	#elif defined (__GNUC__)
    119		// supports bool
    120	#elif __cplusplus < 199711
    121		#define BLARGG_COMPILER_HAS_BOOL 0
    122	#endif
    123#endif
    124#if defined (BLARGG_COMPILER_HAS_BOOL) && !BLARGG_COMPILER_HAS_BOOL
    125	// If you get errors here, modify your blargg_config.h file
    126	typedef int bool;
    127	const bool true  = 1;
    128	const bool false = 0;
    129#endif
    130
    131// blargg_long/blargg_ulong = at least 32 bits, int if it's big enough
    132
    133#if INT_MAX < 0x7FFFFFFF || LONG_MAX == 0x7FFFFFFF
    134	typedef long blargg_long;
    135#else
    136	typedef int blargg_long;
    137#endif
    138
    139#if UINT_MAX < 0xFFFFFFFF || ULONG_MAX == 0xFFFFFFFF
    140	typedef unsigned long blargg_ulong;
    141#else
    142	typedef unsigned blargg_ulong;
    143#endif
    144
    145// BOOST::int8_t etc.
    146
    147// HAVE_STDINT_H: If defined, use <stdint.h> for int8_t etc.
    148#if defined (HAVE_STDINT_H)
    149	#include <stdint.h>
    150	#define BOOST
    151
    152// HAVE_INTTYPES_H: If defined, use <stdint.h> for int8_t etc.
    153#elif defined (HAVE_INTTYPES_H)
    154	#include <inttypes.h>
    155	#define BOOST
    156
    157#else
    158	struct BOOST
    159	{
    160		#if UCHAR_MAX == 0xFF && SCHAR_MAX == 0x7F
    161			typedef signed char     int8_t;
    162			typedef unsigned char   uint8_t;
    163		#else
    164			// No suitable 8-bit type available
    165			typedef struct see_blargg_common_h int8_t;
    166			typedef struct see_blargg_common_h uint8_t;
    167		#endif
    168
    169		#if USHRT_MAX == 0xFFFF
    170			typedef short           int16_t;
    171			typedef unsigned short  uint16_t;
    172		#else
    173			// No suitable 16-bit type available
    174			typedef struct see_blargg_common_h int16_t;
    175			typedef struct see_blargg_common_h uint16_t;
    176		#endif
    177
    178		#if ULONG_MAX == 0xFFFFFFFF
    179			typedef long            int32_t;
    180			typedef unsigned long   uint32_t;
    181		#elif UINT_MAX == 0xFFFFFFFF
    182			typedef int             int32_t;
    183			typedef unsigned int    uint32_t;
    184		#else
    185			// No suitable 32-bit type available
    186			typedef struct see_blargg_common_h int32_t;
    187			typedef struct see_blargg_common_h uint32_t;
    188		#endif
    189	};
    190#endif
    191
    192#if __GNUC__ >= 3
    193	#define BLARGG_DEPRECATED __attribute__ ((deprecated))
    194#else
    195	#define BLARGG_DEPRECATED
    196#endif
    197
    198// Use in place of "= 0;" for a pure virtual, since these cause calls to std C++ lib.
    199// During development, BLARGG_PURE( x ) expands to = 0;
    200// virtual int func() BLARGG_PURE( { return 0; } )
    201#ifndef BLARGG_PURE
    202	#define BLARGG_PURE( def ) def
    203#endif
    204
    205#endif
    206#endif