Path: blob/a-new-beginning/Cherry/Core/include/audio/blargg_common.h
2 views
// Sets up common environment for Shay Green's libraries.1// To change configuration options, modify blargg_config.h, not this file.23// Gb_Snd_Emu 0.2.04#ifndef BLARGG_COMMON_H5#define BLARGG_COMMON_H67#include <stddef.h>8#include <stdlib.h>9#include <assert.h>10#include <limits.h>1112#undef BLARGG_COMMON_H13// allow blargg_config.h to #include blargg_common.h14#include "blargg_config.h"15#ifndef BLARGG_COMMON_H16#define BLARGG_COMMON_H1718// BLARGG_RESTRICT: equivalent to restrict, where supported19#if __GNUC__ >= 3 || _MSC_VER >= 110020#define BLARGG_RESTRICT __restrict21#else22#define BLARGG_RESTRICT23#endif2425// STATIC_CAST(T,expr): Used in place of static_cast<T> (expr)26// CONST_CAST( T,expr): Used in place of const_cast<T> (expr)27#ifndef STATIC_CAST28#if __GNUC__ >= 429#define STATIC_CAST(T,expr) static_cast<T> (expr)30#define CONST_CAST( T,expr) const_cast<T> (expr)31#else32#define STATIC_CAST(T,expr) ((T) (expr))33#define CONST_CAST( T,expr) ((T) (expr))34#endif35#endif3637// blargg_err_t (0 on success, otherwise error string)38#ifndef blargg_err_t39typedef const char* blargg_err_t;40#endif4142// blargg_vector - very lightweight vector of POD types (no constructor/destructor)43template<class T>44class blargg_vector {45T* begin_;46size_t size_;47public:48blargg_vector() : begin_( 0 ), size_( 0 ) { }49~blargg_vector() { free( begin_ ); }50size_t size() const { return size_; }51T* begin() const { return begin_; }52T* end() const { return begin_ + size_; }53blargg_err_t resize( size_t n )54{55// TODO: blargg_common.cpp to hold this as an outline function, ugh56void* p = realloc( begin_, n * sizeof (T) );57if ( p )58begin_ = (T*) p;59else if ( n > size_ ) // realloc failure only a problem if expanding60return "Out of memory";61size_ = n;62return 0;63}64void clear() { void* p = begin_; begin_ = 0; size_ = 0; free( p ); }65T& operator [] ( size_t n ) const66{67assert( n <= size_ ); // <= to allow past-the-end value68return begin_ [n];69}70};7172#ifndef BLARGG_DISABLE_NOTHROW73// throw spec mandatory in ISO C++ if operator new can return NULL74#if __cplusplus >= 199711 || __GNUC__ >= 375#define BLARGG_THROWS( spec ) throw spec76#else77#define BLARGG_THROWS( spec )78#endif79#define BLARGG_DISABLE_NOTHROW \80void* operator new ( size_t s ) BLARGG_THROWS(()) { return malloc( s ); }\81void operator delete ( void* p ) { free( p ); }82#define BLARGG_NEW new83#else84#include <new>85#define BLARGG_NEW new (std::nothrow)86#endif8788// BLARGG_4CHAR('a','b','c','d') = 'abcd' (four character integer constant)89#define BLARGG_4CHAR( a, b, c, d ) \90((a&0xFF)*0x1000000 + (b&0xFF)*0x10000 + (c&0xFF)*0x100 + (d&0xFF))9192// BOOST_STATIC_ASSERT( expr ): Generates compile error if expr is 0.93#ifndef BOOST_STATIC_ASSERT94#ifdef _MSC_VER95// MSVC6 (_MSC_VER < 1300) fails for use of __LINE__ when /Zl is specified96#define BOOST_STATIC_ASSERT( expr ) \97void blargg_failed_( int (*arg) [2 / (int) !!(expr) - 1] )98#else99// Some other compilers fail when declaring same function multiple times in class,100// so differentiate them by line101#define BOOST_STATIC_ASSERT( expr ) \102void blargg_failed_( int (*arg) [2 / !!(expr) - 1] [__LINE__] )103#endif104#endif105106// BLARGG_COMPILER_HAS_BOOL: If 0, provides bool support for old compiler. If 1,107// compiler is assumed to support bool. If undefined, availability is determined.108#ifndef BLARGG_COMPILER_HAS_BOOL109#if defined (__MWERKS__)110#if !__option(bool)111#define BLARGG_COMPILER_HAS_BOOL 0112#endif113#elif defined (_MSC_VER)114#if _MSC_VER < 1100115#define BLARGG_COMPILER_HAS_BOOL 0116#endif117#elif defined (__GNUC__)118// supports bool119#elif __cplusplus < 199711120#define BLARGG_COMPILER_HAS_BOOL 0121#endif122#endif123#if defined (BLARGG_COMPILER_HAS_BOOL) && !BLARGG_COMPILER_HAS_BOOL124// If you get errors here, modify your blargg_config.h file125typedef int bool;126const bool true = 1;127const bool false = 0;128#endif129130// blargg_long/blargg_ulong = at least 32 bits, int if it's big enough131132#if INT_MAX < 0x7FFFFFFF || LONG_MAX == 0x7FFFFFFF133typedef long blargg_long;134#else135typedef int blargg_long;136#endif137138#if UINT_MAX < 0xFFFFFFFF || ULONG_MAX == 0xFFFFFFFF139typedef unsigned long blargg_ulong;140#else141typedef unsigned blargg_ulong;142#endif143144// BOOST::int8_t etc.145146// HAVE_STDINT_H: If defined, use <stdint.h> for int8_t etc.147#if defined (HAVE_STDINT_H)148#include <stdint.h>149#define BOOST150151// HAVE_INTTYPES_H: If defined, use <stdint.h> for int8_t etc.152#elif defined (HAVE_INTTYPES_H)153#include <inttypes.h>154#define BOOST155156#else157struct BOOST158{159#if UCHAR_MAX == 0xFF && SCHAR_MAX == 0x7F160typedef signed char int8_t;161typedef unsigned char uint8_t;162#else163// No suitable 8-bit type available164typedef struct see_blargg_common_h int8_t;165typedef struct see_blargg_common_h uint8_t;166#endif167168#if USHRT_MAX == 0xFFFF169typedef short int16_t;170typedef unsigned short uint16_t;171#else172// No suitable 16-bit type available173typedef struct see_blargg_common_h int16_t;174typedef struct see_blargg_common_h uint16_t;175#endif176177#if ULONG_MAX == 0xFFFFFFFF178typedef long int32_t;179typedef unsigned long uint32_t;180#elif UINT_MAX == 0xFFFFFFFF181typedef int int32_t;182typedef unsigned int uint32_t;183#else184// No suitable 32-bit type available185typedef struct see_blargg_common_h int32_t;186typedef struct see_blargg_common_h uint32_t;187#endif188};189#endif190191#if __GNUC__ >= 3192#define BLARGG_DEPRECATED __attribute__ ((deprecated))193#else194#define BLARGG_DEPRECATED195#endif196197// Use in place of "= 0;" for a pure virtual, since these cause calls to std C++ lib.198// During development, BLARGG_PURE( x ) expands to = 0;199// virtual int func() BLARGG_PURE( { return 0; } )200#ifndef BLARGG_PURE201#define BLARGG_PURE( def ) def202#endif203204#endif205#endif206207208