Debian Codegraph @

mednafen/include/blargg_common.h

raw symbols SCIP
1 // Sets up common environment for Shay Green's libraries.
2 // To change configuration options, modify blargg_config.h, not this file.
3
4 #ifndef BLARGG_COMMON_H
5 #define BLARGG_COMMON_H
6
7 #include <stddef.h>
8 #include <stdlib.h>
9 #include <assert.h>
10 #include <limits.h>
11
12 #undef BLARGG_COMMON_H
13 // allow blargg_config.h to #include blargg_common.h
14 #include "blargg_config.h"
15 #ifndef BLARGG_COMMON_H
16 #define BLARGG_COMMON_H
17
18 // STATIC_CAST(T,expr): Used in place of static_cast<T> (expr)
19 #ifndef STATIC_CAST
20 #define STATIC_CAST(T,expr) ((T) (expr))
21 #endif
22
23 // blargg_err_t (0 on success, otherwise error string)
24 #ifndef blargg_err_t
25 typedef const char* blargg_err_t;
26 #endif
27
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
56 #ifndef BLARGG_DISABLE_NOTHROW
57 #if __cplusplus < 199711
58 #define BLARGG_THROWS( spec )
59 #else
60 #define BLARGG_THROWS( spec ) throw 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
71 #define BLARGG_4CHAR( a, b, c, d ) \
72 ((a&0xFF)*0x1000000L + (b&0xFF)*0x10000L + (c&0xFF)*0x100L + (d&0xFF))
73
74 // BOOST_STATIC_ASSERT( expr ): Generates compile error if expr is 0.
75 #ifndef BOOST_STATIC_ASSERT
76 #ifdef _MSC_VER
77 // MSVC6 (_MSC_VER < 1300) fails for use of __LINE__ when /Zl is specified
78 #define BOOST_STATIC_ASSERT( expr ) \
79 void blargg_failed_( int (*arg) [2 / (int) !!(expr) - 1] )
80 #else
81 // Some other compilers fail when declaring same function multiple times in class,
82 // so differentiate them by line
83 #define BOOST_STATIC_ASSERT( expr ) \
84 void blargg_failed_( int (*arg) [2 / !!(expr) - 1] [__LINE__] )
85 #endif
86 #endif
87
88 // BLARGG_COMPILER_HAS_BOOL: If 0, provides bool support for old compiler. If 1,
89 // compiler is assumed to support bool. If undefined, availability is determined.
90 #ifndef BLARGG_COMPILER_HAS_BOOL
91 #if defined (__MWERKS__)
92 #if !__option(bool)
93 #define BLARGG_COMPILER_HAS_BOOL 0
94 #endif
95 #elif defined (_MSC_VER)
96 #if _MSC_VER < 1100
97 #define BLARGG_COMPILER_HAS_BOOL 0
98 #endif
99 #elif defined (__GNUC__)
100 // supports bool
101 #elif __cplusplus < 199711
102 #define BLARGG_COMPILER_HAS_BOOL 0
103 #endif
104 #endif
105 #if defined (BLARGG_COMPILER_HAS_BOOL) && !BLARGG_COMPILER_HAS_BOOL
106 // If you get errors here, modify your blargg_config.h file
107 typedef int bool;
108 const bool true = 1;
109 const bool false = 0;
110 #endif
111
112 // blargg_long/blargg_ulong = at least 32 bits, int if it's big enough
113 #include <limits.h>
114
115 #if INT_MAX >= 0x7FFFFFFF
116 typedef int blargg_long;
117 #else
118 typedef long blargg_long;
119 #endif
120
121 #if UINT_MAX >= 0xFFFFFFFF
122 typedef unsigned blargg_ulong;
123 #else
124 typedef unsigned long blargg_ulong;
125 #endif
126
127 // BOOST::int8_t etc.
128
129 // HAVE_STDINT_H: If defined, use <stdint.h> for int8_t etc.
130 #if defined (HAVE_STDINT_H)
131 #include <stdint.h>
132 #define BOOST
133
134 // HAVE_INTTYPES_H: If defined, use <stdint.h> for int8_t etc.
135 #elif defined (HAVE_INTTYPES_H)
136 #include <inttypes.h>
137 #define BOOST
138
139 #else
140 struct BOOST
141 {
142 #if UCHAR_MAX == 0xFF && SCHAR_MAX == 0x7F
143 typedef signed char int8_t;
144 typedef unsigned char uint8_t;
145 #else
146 // No suitable 8-bit type available
147 typedef struct see_blargg_common_h int8_t;
148 typedef struct see_blargg_common_h uint8_t;
149 #endif
150
151 #if USHRT_MAX == 0xFFFF
152 typedef short int16_t;
153 typedef unsigned short uint16_t;
154 #else
155 // No suitable 16-bit type available
156 typedef struct see_blargg_common_h int16_t;
157 typedef struct see_blargg_common_h uint16_t;
158 #endif
159
160 #if ULONG_MAX == 0xFFFFFFFF
161 typedef long int32_t;
162 typedef unsigned long uint32_t;
163 #elif UINT_MAX == 0xFFFFFFFF
164 typedef int int32_t;
165 typedef unsigned int uint32_t;
166 #else
167 // No suitable 32-bit type available
168 typedef struct see_blargg_common_h int32_t;
169 typedef struct see_blargg_common_h uint32_t;
170 #endif
171 };
172 #endif
173
174 #endif
175 #endif