blargg_common.h 5.3 KB

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