Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/quicknes/fex/blargg_common.h
2 views
1
// Sets up common environment for Shay Green's libraries.
2
// To change configuration options, modify blargg_config.h, not this file.
3
4
// File_Extractor 1.0.0
5
#ifndef BLARGG_COMMON_H
6
#define BLARGG_COMMON_H
7
8
#include <stdlib.h>
9
#include <stdint.h>
10
#include <assert.h>
11
#include <limits.h>
12
13
typedef const char* blargg_err_t; // 0 on success, otherwise error string
14
15
#ifdef _WIN32
16
typedef wchar_t blargg_wchar_t;
17
#else
18
typedef uint16_t blargg_wchar_t;
19
#endif
20
21
inline size_t blargg_wcslen( const blargg_wchar_t* str )
22
{
23
size_t length = 0;
24
while ( *str++ ) length++;
25
return length;
26
}
27
28
// Success; no error
29
blargg_err_t const blargg_ok = 0;
30
31
// BLARGG_RESTRICT: equivalent to C99's restrict, where supported
32
#if __GNUC__ >= 3 || _MSC_VER >= 1100
33
#define BLARGG_RESTRICT __restrict
34
#else
35
#define BLARGG_RESTRICT
36
#endif
37
38
#if __cplusplus >= 199711
39
#define BLARGG_MUTABLE mutable
40
#else
41
#define BLARGG_MUTABLE
42
#endif
43
44
/* BLARGG_4CHAR('a','b','c','d') = 'abcd' (four character integer constant).
45
I don't just use 'abcd' because that's implementation-dependent. */
46
#define BLARGG_4CHAR( a, b, c, d ) \
47
((a&0xFF)*0x1000000 + (b&0xFF)*0x10000 + (c&0xFF)*0x100 + (d&0xFF))
48
49
/* BLARGG_STATIC_ASSERT( expr ): Generates compile error if expr is 0.
50
Can be used at file, function, or class scope. */
51
#ifdef _MSC_VER
52
// MSVC6 (_MSC_VER < 1300) __LINE__ fails when /Zl is specified
53
#define BLARGG_STATIC_ASSERT( expr ) \
54
void blargg_failed_( int (*arg) [2 / (int) !!(expr) - 1] )
55
#else
56
// Others fail when declaring same function multiple times in class,
57
// so differentiate them by line
58
#define BLARGG_STATIC_ASSERT( expr ) \
59
void blargg_failed_( int (*arg) [2 / !!(expr) - 1] [__LINE__] )
60
#endif
61
62
/* Pure virtual functions cause a vtable entry to a "called pure virtual"
63
error handler, requiring linkage to the C++ runtime library. This macro is
64
used in place of the "= 0", and simply expands to its argument. During
65
development, it expands to "= 0", allowing detection of missing overrides. */
66
#define BLARGG_PURE( def ) def
67
68
/* My code depends on ASCII anywhere a character or string constant is
69
compared with data read from a file, and anywhere file data is read and
70
treated as a string. */
71
#if '\n'!=0x0A || ' '!=0x20 || '0'!=0x30 || 'A'!=0x41 || 'a'!=0x61
72
#error "ASCII character set required"
73
#endif
74
75
/* My code depends on int being at least 32 bits. Almost everything these days
76
uses at least 32-bit ints, so it's hard to even find a system with 16-bit ints
77
to test with. The issue can't be gotten around by using a suitable blargg_int
78
everywhere either, because int is often converted to implicitly when doing
79
arithmetic on smaller types. */
80
#if UINT_MAX < 0xFFFFFFFF
81
#error "int must be at least 32 bits"
82
#endif
83
84
// In case compiler doesn't support these properly. Used rarely.
85
#define STATIC_CAST(T,expr) static_cast<T> (expr)
86
#define CONST_CAST( T,expr) const_cast<T> (expr)
87
88
// User configuration can override the above macros if necessary
89
#include "blargg_config.h"
90
91
/* BLARGG_DEPRECATED [_TEXT] for any declarations/text to be removed in a
92
future version. In GCC, we can let the compiler warn. In other compilers,
93
we strip it out unless BLARGG_LEGACY is true. */
94
#if BLARGG_LEGACY
95
// Allow old client code to work without warnings
96
#define BLARGG_DEPRECATED_TEXT( text ) text
97
#define BLARGG_DEPRECATED( text ) text
98
#elif __GNUC__ >= 4
99
// In GCC, we can mark declarations and let the compiler warn
100
#define BLARGG_DEPRECATED_TEXT( text ) text
101
#define BLARGG_DEPRECATED( text ) __attribute__ ((deprecated)) text
102
#else
103
// By default, deprecated items are removed, to avoid use in new code
104
#define BLARGG_DEPRECATED_TEXT( text )
105
#define BLARGG_DEPRECATED( text )
106
#endif
107
108
/* BOOST::int8_t, BOOST::int32_t, etc.
109
I used BOOST since I originally was going to allow use of the boost library
110
for prividing the definitions. If I'm defining them, they must be scoped or
111
else they could conflict with the standard ones at global scope. Even if
112
HAVE_STDINT_H isn't defined, I can't assume the typedefs won't exist at
113
global scope already. */
114
#if defined (HAVE_STDINT_H) || \
115
UCHAR_MAX != 0xFF || USHRT_MAX != 0xFFFF || UINT_MAX != 0xFFFFFFFF
116
#include <stdint.h>
117
#define BOOST
118
#else
119
struct BOOST
120
{
121
typedef signed char int8_t;
122
typedef unsigned char uint8_t;
123
typedef short int16_t;
124
typedef unsigned short uint16_t;
125
typedef int int32_t;
126
typedef unsigned int uint32_t;
127
typedef __int64 int64_t;
128
typedef unsigned __int64 uint64_t;
129
};
130
#endif
131
132
/* My code is not written with exceptions in mind, so either uses new (nothrow)
133
OR overrides operator new in my classes. The former is best since clients
134
creating objects will get standard exceptions on failure, but that causes it
135
to require the standard C++ library. So, when the client is using the C
136
interface, I override operator new to use malloc. */
137
138
// BLARGG_DISABLE_NOTHROW is put inside classes
139
#ifndef BLARGG_DISABLE_NOTHROW
140
// throw spec mandatory in ISO C++ if NULL can be returned
141
#if __cplusplus >= 199711 || __GNUC__ >= 3 || _MSC_VER >= 1300
142
#define BLARGG_THROWS_NOTHING throw ()
143
#else
144
#define BLARGG_THROWS_NOTHING
145
#endif
146
147
#define BLARGG_DISABLE_NOTHROW \
148
void* operator new ( size_t s ) BLARGG_THROWS_NOTHING { return malloc( s ); }\
149
void operator delete( void* p ) BLARGG_THROWS_NOTHING { free( p ); }
150
151
#define BLARGG_NEW new
152
#else
153
// BLARGG_NEW is used in place of new in library code
154
#include <new>
155
#define BLARGG_NEW new (std::nothrow)
156
#endif
157
158
class blargg_vector_ {
159
protected:
160
void* begin_;
161
size_t size_;
162
void init();
163
blargg_err_t resize_( size_t n, size_t elem_size );
164
public:
165
size_t size() const { return size_; }
166
void clear();
167
};
168
169
// Very lightweight vector for POD types (no constructor/destructor)
170
template<class T>
171
class blargg_vector : public blargg_vector_ {
172
union T_must_be_pod { T t; }; // fails if T is not POD
173
public:
174
blargg_vector() { init(); }
175
~blargg_vector() { clear(); }
176
177
blargg_err_t resize( size_t n ) { return resize_( n, sizeof (T) ); }
178
179
T* begin() { return static_cast<T*> (begin_); }
180
const T* begin() const { return static_cast<T*> (begin_); }
181
182
T* end() { return static_cast<T*> (begin_) + size_; }
183
const T* end() const { return static_cast<T*> (begin_) + size_; }
184
185
T& operator [] ( size_t n )
186
{
187
assert( n < size_ );
188
return static_cast<T*> (begin_) [n];
189
}
190
191
const T& operator [] ( size_t n ) const
192
{
193
assert( n < size_ );
194
return static_cast<T*> (begin_) [n];
195
}
196
};
197
198
// Callback function with user data.
199
// blargg_callback<T> set_callback; // for user, this acts like...
200
// void set_callback( T func, void* user_data = NULL ); // ...this
201
// To call function, do set_callback.f( .. set_callback.data ... );
202
template<class T>
203
struct blargg_callback
204
{
205
T f;
206
void* data;
207
blargg_callback() { f = NULL; }
208
void operator () ( T callback, void* user_data = NULL ) { f = callback; data = user_data; }
209
};
210
211
BLARGG_DEPRECATED( typedef signed int blargg_long; )
212
BLARGG_DEPRECATED( typedef unsigned int blargg_ulong; )
213
#if BLARGG_LEGACY
214
#define BOOST_STATIC_ASSERT BLARGG_STATIC_ASSERT
215
#endif
216
217
#endif
218
219