comparison Plugins/Input/console/blargg_common.h @ 493:c04dff121e1d trunk

[svn] hostile merge, phase 2: reimport based on new plugin code
author nenolod
date Tue, 24 Jan 2006 20:19:01 -0800
parents
children
comparison
equal deleted inserted replaced
492:ccb68bad47b2 493:c04dff121e1d
1
2 // Sets up common environment for Shay Green's libraries.
3 //
4 // To change configuration options, modify blargg_config.h, not this file.
5
6 #ifndef BLARGG_COMMON_H
7 #define BLARGG_COMMON_H
8
9 // HAVE_CONFIG_H: If defined, include user's "config.h" first (which *can*
10 // re-include blargg_common.h if it needs to)
11 #ifdef HAVE_CONFIG_H
12 #undef BLARGG_COMMON_H
13 #include "config.h"
14 #define BLARGG_COMMON_H
15 #endif
16
17 // BLARGG_NONPORTABLE: If defined to 1, platform-specific (and possibly non-portable)
18 // optimizations are used. Defaults to off. Report any problems that occur only when
19 // this is enabled.
20 #ifndef BLARGG_NONPORTABLE
21 #define BLARGG_NONPORTABLE 0
22 #endif
23
24 // BLARGG_BIG_ENDIAN, BLARGG_LITTLE_ENDIAN: Determined automatically, otherwise only
25 // one must be #defined to 1. Only needed if something actually depends on byte order.
26 #if !defined (BLARGG_BIG_ENDIAN) && !defined (BLARGG_LITTLE_ENDIAN)
27 #if defined (MSB_FIRST) || defined (__powerc) || defined (macintosh) || \
28 defined (WORDS_BIGENDIAN) || defined (__BIG_ENDIAN__)
29 #define BLARGG_BIG_ENDIAN 1
30 #else
31 #define BLARGG_LITTLE_ENDIAN 1
32 #endif
33 #endif
34
35 // Determine compiler's language support
36
37 // Metrowerks CodeWarrior
38 #if defined (__MWERKS__)
39 #define BLARGG_COMPILER_HAS_NAMESPACE 1
40 #if !__option(bool)
41 #define BLARGG_COMPILER_HAS_BOOL 0
42 #endif
43 #define STATIC_CAST(T,expr) static_cast< T > (expr)
44
45 // Microsoft Visual C++
46 #elif defined (_MSC_VER)
47 #if _MSC_VER < 1100
48 #define BLARGG_COMPILER_HAS_BOOL 0
49 #endif
50
51 // GNU C++
52 #elif defined (__GNUC__)
53 #if __GNUC__ > 2
54 #define BLARGG_COMPILER_HAS_NAMESPACE 1
55 #endif
56
57 // Mingw
58 #elif defined (__MINGW32__)
59 // empty
60
61 // Pre-ISO C++ compiler
62 #elif __cplusplus < 199711
63 #ifndef BLARGG_COMPILER_HAS_BOOL
64 #define BLARGG_COMPILER_HAS_BOOL 0
65 #endif
66
67 #endif
68
69 /* BLARGG_COMPILER_HAS_BOOL: If 0, provides bool support for old compilers.
70 If errors occur here, add the following line to your config.h file:
71 #define BLARGG_COMPILER_HAS_BOOL 0
72 */
73 #if defined (BLARGG_COMPILER_HAS_BOOL) && !BLARGG_COMPILER_HAS_BOOL
74 typedef int bool;
75 const bool true = 1;
76 const bool false = 0;
77 #endif
78
79 // BLARGG_USE_NAMESPACE: If 1, use <cxxx> headers rather than <xxxx.h>
80 #if BLARGG_USE_NAMESPACE || (!defined (BLARGG_USE_NAMESPACE) && BLARGG_COMPILER_HAS_NAMESPACE)
81 #include <cstddef>
82 #include <cstdlib>
83 #include <cassert>
84 #include <climits>
85 #define STD std
86 #else
87 #include <stddef.h>
88 #include <stdlib.h>
89 #include <assert.h>
90 #include <limits.h>
91 #define STD
92 #endif
93
94 // BLARGG_NEW is used in place of 'new' to create objects. By default, plain new is used.
95 // To prevent an exception if out of memory, #define BLARGG_NEW new (std::nothrow)
96 #ifndef BLARGG_NEW
97 #define BLARGG_NEW new
98 #endif
99
100 // BOOST::int8_t etc.
101
102 // HAVE_STDINT_H: If defined, use <stdint.h> for int8_t etc.
103 #if defined (HAVE_STDINT_H)
104 #include <stdint.h>
105 #define BOOST
106
107 // HAVE_INTTYPES_H: If defined, use <stdint.h> for int8_t etc.
108 #elif defined (HAVE_INTTYPES_H)
109 #include <inttypes.h>
110 #define BOOST
111
112 #else
113 struct BOOST
114 {
115 #if UCHAR_MAX == 0xFF && SCHAR_MAX == 0x7F
116 typedef signed char int8_t;
117 typedef unsigned char uint8_t;
118 #else
119 // No suitable 8-bit type available
120 typedef struct see_blargg_common_h int8_t;
121 typedef struct see_blargg_common_h uint8_t;
122 #endif
123
124 #if USHRT_MAX == 0xFFFF
125 typedef short int16_t;
126 typedef unsigned short uint16_t;
127 #else
128 // No suitable 16-bit type available
129 typedef struct see_blargg_common_h int16_t;
130 typedef struct see_blargg_common_h uint16_t;
131 #endif
132
133 #if ULONG_MAX == 0xFFFFFFFF
134 typedef long int32_t;
135 typedef unsigned long uint32_t;
136 #elif UINT_MAX == 0xFFFFFFFF
137 typedef int int32_t;
138 typedef unsigned int uint32_t;
139 #else
140 // No suitable 32-bit type available
141 typedef struct see_blargg_common_h int32_t;
142 typedef struct see_blargg_common_h uint32_t;
143 #endif
144 };
145 #endif
146
147 // BLARGG_SOURCE_BEGIN: Library sources #include this after other #includes.
148 #ifndef BLARGG_SOURCE_BEGIN
149 #define BLARGG_SOURCE_BEGIN "blargg_source.h"
150 #endif
151
152 // BLARGG_ENABLE_OPTIMIZER: Library sources #include this for speed-critical code
153 #ifndef BLARGG_ENABLE_OPTIMIZER
154 #define BLARGG_ENABLE_OPTIMIZER "blargg_common.h"
155 #endif
156
157 // BLARGG_CPU_*: Used to select between some optimizations
158 #if !defined (BLARGG_CPU_POWERPC) && !defined (BLARGG_CPU_X86)
159 #if defined (__powerc)
160 #define BLARGG_CPU_POWERPC 1
161 #elif defined (_MSC_VER) && defined (_M_IX86)
162 #define BLARGG_CPU_X86 1
163 #endif
164 #endif
165
166 // BOOST_STATIC_ASSERT( expr ): Generates compile error if expr is 0.
167 #ifndef BOOST_STATIC_ASSERT
168 #ifdef _MSC_VER
169 // MSVC6 (_MSC_VER < 1300) fails for use of __LINE__ when /Zl is specified
170 #define BOOST_STATIC_ASSERT( expr ) \
171 void blargg_failed_( int (*arg) [2 / ((expr) ? 1 : 0) - 1] )
172 #else
173 // Some other compilers fail when declaring same function multiple times in class,
174 // so differentiate them by line
175 #define BOOST_STATIC_ASSERT( expr ) \
176 void blargg_failed_( int (*arg) [2 / ((expr) ? 1 : 0) - 1] [__LINE__] )
177 #endif
178 #endif
179
180 // STATIC_CAST(T,expr): Used in place of static_cast<T> (expr)
181 #ifndef STATIC_CAST
182 #define STATIC_CAST(T,expr) ((T) (expr))
183 #endif
184
185 // blargg_err_t (NULL on success, otherwise error string)
186 #ifndef blargg_err_t
187 typedef const char* blargg_err_t;
188 #endif
189 const char* const blargg_success = 0;
190
191 // blargg_vector: Simple array that does *not* work for types with a constructor (non-POD).
192 template<class T>
193 class blargg_vector {
194 T* begin_;
195 STD::size_t size_;
196 public:
197 blargg_vector() : begin_( 0 ), size_( 0 ) { }
198 ~blargg_vector() { STD::free( begin_ ); }
199
200 typedef STD::size_t size_type;
201
202 blargg_err_t resize( size_type n )
203 {
204 void* p = STD::realloc( begin_, n * sizeof (T) );
205 if ( !p && n )
206 return "Out of memory";
207 begin_ = (T*) p;
208 size_ = n;
209 return 0;
210 }
211
212 void clear()
213 {
214 void* p = begin_;
215 begin_ = 0;
216 size_ = 0;
217 STD::free( p );
218 }
219
220 size_type size() const { return size_; }
221
222 T* begin() { return begin_; }
223 T* end() { return begin_ + size_; }
224
225 const T* begin() const { return begin_; }
226 const T* end() const { return begin_ + size_; }
227
228 T& operator [] ( size_type n )
229 {
230 assert( n <= size_ ); // allow for past-the-end value
231 return begin_ [n];
232 }
233
234 const T& operator [] ( size_type n ) const
235 {
236 assert( n <= size_ ); // allow for past-the-end value
237 return begin_ [n];
238 }
239 };
240
241 #endif
242