Mercurial > spc_convert
view snes_spc/blargg_common.h @ 9:477c36226481 tip
added old scripts for historical interest.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 21 Oct 2011 07:46:18 -0700 |
parents | e38dacceb958 |
children |
line wrap: on
line source
1 // Sets up common environment for Shay Green's libraries.2 // To change configuration options, modify blargg_config.h, not this file.4 // snes_spc 0.9.05 #ifndef BLARGG_COMMON_H6 #define BLARGG_COMMON_H8 #include <stddef.h>9 #include <stdlib.h>10 #include <assert.h>11 #include <limits.h>13 #undef BLARGG_COMMON_H14 // allow blargg_config.h to #include blargg_common.h15 #include "blargg_config.h"16 #ifndef BLARGG_COMMON_H17 #define BLARGG_COMMON_H19 // BLARGG_RESTRICT: equivalent to restrict, where supported20 #if defined (__GNUC__) || _MSC_VER >= 110021 #define BLARGG_RESTRICT __restrict22 #else23 #define BLARGG_RESTRICT24 #endif26 // STATIC_CAST(T,expr): Used in place of static_cast<T> (expr)27 #ifndef STATIC_CAST28 #define STATIC_CAST(T,expr) ((T) (expr))29 #endif31 // blargg_err_t (0 on success, otherwise error string)32 #ifndef blargg_err_t33 typedef const char* blargg_err_t;34 #endif36 // blargg_vector - very lightweight vector of POD types (no constructor/destructor)37 template<class T>38 class blargg_vector {39 T* begin_;40 size_t size_;41 public:42 blargg_vector() : begin_( 0 ), size_( 0 ) { }43 ~blargg_vector() { free( begin_ ); }44 size_t size() const { return size_; }45 T* begin() const { return begin_; }46 T* end() const { return begin_ + size_; }47 blargg_err_t resize( size_t n )48 {49 // TODO: blargg_common.cpp to hold this as an outline function, ugh50 void* p = realloc( begin_, n * sizeof (T) );51 if ( p )52 begin_ = (T*) p;53 else if ( n > size_ ) // realloc failure only a problem if expanding54 return "Out of memory";55 size_ = n;56 return 0;57 }58 void clear() { void* p = begin_; begin_ = 0; size_ = 0; free( p ); }59 T& operator [] ( size_t n ) const60 {61 assert( n <= size_ ); // <= to allow past-the-end value62 return begin_ [n];63 }64 };66 #ifndef BLARGG_DISABLE_NOTHROW67 // throw spec mandatory in ISO C++ if operator new can return NULL68 #if __cplusplus >= 199711 || defined (__GNUC__)69 #define BLARGG_THROWS( spec ) throw spec70 #else71 #define BLARGG_THROWS( spec )72 #endif73 #define BLARGG_DISABLE_NOTHROW \74 void* operator new ( size_t s ) BLARGG_THROWS(()) { return malloc( s ); }\75 void operator delete ( void* p ) { free( p ); }76 #define BLARGG_NEW new77 #else78 #include <new>79 #define BLARGG_NEW new (std::nothrow)80 #endif82 // BLARGG_4CHAR('a','b','c','d') = 'abcd' (four character integer constant)83 #define BLARGG_4CHAR( a, b, c, d ) \84 ((a&0xFF)*0x1000000L + (b&0xFF)*0x10000L + (c&0xFF)*0x100L + (d&0xFF))86 // BOOST_STATIC_ASSERT( expr ): Generates compile error if expr is 0.87 #ifndef BOOST_STATIC_ASSERT88 #ifdef _MSC_VER89 // MSVC6 (_MSC_VER < 1300) fails for use of __LINE__ when /Zl is specified90 #define BOOST_STATIC_ASSERT( expr ) \91 void blargg_failed_( int (*arg) [2 / (int) !!(expr) - 1] )92 #else93 // Some other compilers fail when declaring same function multiple times in class,94 // so differentiate them by line95 #define BOOST_STATIC_ASSERT( expr ) \96 void blargg_failed_( int (*arg) [2 / !!(expr) - 1] [__LINE__] )97 #endif98 #endif100 // BLARGG_COMPILER_HAS_BOOL: If 0, provides bool support for old compiler. If 1,101 // compiler is assumed to support bool. If undefined, availability is determined.102 #ifndef BLARGG_COMPILER_HAS_BOOL103 #if defined (__MWERKS__)104 #if !__option(bool)105 #define BLARGG_COMPILER_HAS_BOOL 0106 #endif107 #elif defined (_MSC_VER)108 #if _MSC_VER < 1100109 #define BLARGG_COMPILER_HAS_BOOL 0110 #endif111 #elif defined (__GNUC__)112 // supports bool113 #elif __cplusplus < 199711114 #define BLARGG_COMPILER_HAS_BOOL 0115 #endif116 #endif117 #if defined (BLARGG_COMPILER_HAS_BOOL) && !BLARGG_COMPILER_HAS_BOOL118 // If you get errors here, modify your blargg_config.h file119 typedef int bool;120 const bool true = 1;121 const bool false = 0;122 #endif124 // blargg_long/blargg_ulong = at least 32 bits, int if it's big enough126 #if INT_MAX < 0x7FFFFFFF || LONG_MAX == 0x7FFFFFFF127 typedef long blargg_long;128 #else129 typedef int blargg_long;130 #endif132 #if UINT_MAX < 0xFFFFFFFF || ULONG_MAX == 0xFFFFFFFF133 typedef unsigned long blargg_ulong;134 #else135 typedef unsigned blargg_ulong;136 #endif138 // BOOST::int8_t etc.140 // HAVE_STDINT_H: If defined, use <stdint.h> for int8_t etc.141 #if defined (HAVE_STDINT_H)142 #include <stdint.h>143 #define BOOST145 // HAVE_INTTYPES_H: If defined, use <stdint.h> for int8_t etc.146 #elif defined (HAVE_INTTYPES_H)147 #include <inttypes.h>148 #define BOOST150 #else151 struct BOOST152 {153 #if UCHAR_MAX == 0xFF && SCHAR_MAX == 0x7F154 typedef signed char int8_t;155 typedef unsigned char uint8_t;156 #else157 // No suitable 8-bit type available158 typedef struct see_blargg_common_h int8_t;159 typedef struct see_blargg_common_h uint8_t;160 #endif162 #if USHRT_MAX == 0xFFFF163 typedef short int16_t;164 typedef unsigned short uint16_t;165 #else166 // No suitable 16-bit type available167 typedef struct see_blargg_common_h int16_t;168 typedef struct see_blargg_common_h uint16_t;169 #endif171 #if ULONG_MAX == 0xFFFFFFFF172 typedef long int32_t;173 typedef unsigned long uint32_t;174 #elif UINT_MAX == 0xFFFFFFFF175 typedef int int32_t;176 typedef unsigned int uint32_t;177 #else178 // No suitable 32-bit type available179 typedef struct see_blargg_common_h int32_t;180 typedef struct see_blargg_common_h uint32_t;181 #endif182 };183 #endif185 #endif186 #endif