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.0
5 #ifndef BLARGG_COMMON_H
6 #define BLARGG_COMMON_H
8 #include <stddef.h>
9 #include <stdlib.h>
10 #include <assert.h>
11 #include <limits.h>
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
19 // BLARGG_RESTRICT: equivalent to restrict, where supported
20 #if defined (__GNUC__) || _MSC_VER >= 1100
21 #define BLARGG_RESTRICT __restrict
22 #else
23 #define BLARGG_RESTRICT
24 #endif
26 // STATIC_CAST(T,expr): Used in place of static_cast<T> (expr)
27 #ifndef STATIC_CAST
28 #define STATIC_CAST(T,expr) ((T) (expr))
29 #endif
31 // blargg_err_t (0 on success, otherwise error string)
32 #ifndef blargg_err_t
33 typedef const char* blargg_err_t;
34 #endif
36 // 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, ugh
50 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 expanding
54 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 ) const
60 {
61 assert( n <= size_ ); // <= to allow past-the-end value
62 return begin_ [n];
63 }
64 };
66 #ifndef BLARGG_DISABLE_NOTHROW
67 // throw spec mandatory in ISO C++ if operator new can return NULL
68 #if __cplusplus >= 199711 || defined (__GNUC__)
69 #define BLARGG_THROWS( spec ) throw spec
70 #else
71 #define BLARGG_THROWS( spec )
72 #endif
73 #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 new
77 #else
78 #include <new>
79 #define BLARGG_NEW new (std::nothrow)
80 #endif
82 // 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_ASSERT
88 #ifdef _MSC_VER
89 // MSVC6 (_MSC_VER < 1300) fails for use of __LINE__ when /Zl is specified
90 #define BOOST_STATIC_ASSERT( expr ) \
91 void blargg_failed_( int (*arg) [2 / (int) !!(expr) - 1] )
92 #else
93 // Some other compilers fail when declaring same function multiple times in class,
94 // so differentiate them by line
95 #define BOOST_STATIC_ASSERT( expr ) \
96 void blargg_failed_( int (*arg) [2 / !!(expr) - 1] [__LINE__] )
97 #endif
98 #endif
100 // 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_BOOL
103 #if defined (__MWERKS__)
104 #if !__option(bool)
105 #define BLARGG_COMPILER_HAS_BOOL 0
106 #endif
107 #elif defined (_MSC_VER)
108 #if _MSC_VER < 1100
109 #define BLARGG_COMPILER_HAS_BOOL 0
110 #endif
111 #elif defined (__GNUC__)
112 // supports bool
113 #elif __cplusplus < 199711
114 #define BLARGG_COMPILER_HAS_BOOL 0
115 #endif
116 #endif
117 #if defined (BLARGG_COMPILER_HAS_BOOL) && !BLARGG_COMPILER_HAS_BOOL
118 // If you get errors here, modify your blargg_config.h file
119 typedef int bool;
120 const bool true = 1;
121 const bool false = 0;
122 #endif
124 // blargg_long/blargg_ulong = at least 32 bits, int if it's big enough
126 #if INT_MAX < 0x7FFFFFFF || LONG_MAX == 0x7FFFFFFF
127 typedef long blargg_long;
128 #else
129 typedef int blargg_long;
130 #endif
132 #if UINT_MAX < 0xFFFFFFFF || ULONG_MAX == 0xFFFFFFFF
133 typedef unsigned long blargg_ulong;
134 #else
135 typedef unsigned blargg_ulong;
136 #endif
138 // 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 BOOST
145 // HAVE_INTTYPES_H: If defined, use <stdint.h> for int8_t etc.
146 #elif defined (HAVE_INTTYPES_H)
147 #include <inttypes.h>
148 #define BOOST
150 #else
151 struct BOOST
152 {
153 #if UCHAR_MAX == 0xFF && SCHAR_MAX == 0x7F
154 typedef signed char int8_t;
155 typedef unsigned char uint8_t;
156 #else
157 // No suitable 8-bit type available
158 typedef struct see_blargg_common_h int8_t;
159 typedef struct see_blargg_common_h uint8_t;
160 #endif
162 #if USHRT_MAX == 0xFFFF
163 typedef short int16_t;
164 typedef unsigned short uint16_t;
165 #else
166 // No suitable 16-bit type available
167 typedef struct see_blargg_common_h int16_t;
168 typedef struct see_blargg_common_h uint16_t;
169 #endif
171 #if ULONG_MAX == 0xFFFFFFFF
172 typedef long int32_t;
173 typedef unsigned long uint32_t;
174 #elif UINT_MAX == 0xFFFFFFFF
175 typedef int int32_t;
176 typedef unsigned int uint32_t;
177 #else
178 // No suitable 32-bit type available
179 typedef struct see_blargg_common_h int32_t;
180 typedef struct see_blargg_common_h uint32_t;
181 #endif
182 };
183 #endif
185 #endif
186 #endif