LMMS
Loading...
Searching...
No Matches
blargg_common.h
Go to the documentation of this file.
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// BLARGG_RESTRICT: equivalent to restrict, where supported
19#if __GNUC__ >= 3 || _MSC_VER >= 1100
20 #define BLARGG_RESTRICT __restrict
21#else
22 #define BLARGG_RESTRICT
23#endif
24
25// STATIC_CAST(T,expr): Used in place of static_cast<T> (expr)
26#ifndef STATIC_CAST
27 #define STATIC_CAST(T,expr) ((T) (expr))
28#endif
29
30// blargg_err_t (0 on success, otherwise error string)
31#ifndef blargg_err_t
32 typedef const char* blargg_err_t;
33#endif
34
35// blargg_vector - very lightweight vector of POD types (no constructor/destructor)
36template<class T>
37class blargg_vector {
38 T* begin_;
39 size_t size_;
40public:
41 blargg_vector() : begin_( 0 ), size_( 0 ) { }
42 ~blargg_vector() { free( begin_ ); }
43 size_t size() const { return size_; }
44 T* begin() const { return begin_; }
45 T* end() const { return begin_ + size_; }
46 blargg_err_t resize( size_t n )
47 {
48 void* p = realloc( begin_, n * sizeof (T) );
49 if ( !p && n )
50 return "Out of memory";
51 begin_ = (T*) p;
52 size_ = n;
53 return 0;
54 }
55 void clear() { void* p = begin_; begin_ = 0; size_ = 0; free( p ); }
56 T& operator [] ( size_t n ) const
57 {
58 assert( n <= size_ ); // <= to allow past-the-end value
59 return begin_ [n];
60 }
61};
62
63#ifndef BLARGG_DISABLE_NOTHROW
64 // throw spec mandatory in ISO C++ if operator new can return NULL
65 #if __cplusplus >= 199711 || __GNUC__ >= 3
66 #define BLARGG_THROWS( spec ) throw spec
67 #else
68 #define BLARGG_THROWS( spec )
69 #endif
70 #define BLARGG_DISABLE_NOTHROW \
71 void* operator new ( size_t s ) BLARGG_THROWS(()) { return malloc( s ); }\
72 void operator delete ( void* p ) { free( p ); }
73 #define BLARGG_NEW new
74#else
75 #include <new>
76 #define BLARGG_NEW new (std::nothrow)
77#endif
78
79// BLARGG_4CHAR('a','b','c','d') = 'abcd' (four character integer constant)
80#define BLARGG_4CHAR( a, b, c, d ) \
81 ((a&0xFF)*0x1000000L + (b&0xFF)*0x10000L + (c&0xFF)*0x100L + (d&0xFF))
82
83// BOOST_STATIC_ASSERT( expr ): Generates compile error if expr is 0.
84#ifndef BOOST_STATIC_ASSERT
85 #ifdef _MSC_VER
86 // MSVC6 (_MSC_VER < 1300) fails for use of __LINE__ when /Zl is specified
87 #define BOOST_STATIC_ASSERT( expr ) \
88 void blargg_failed_( int (*arg) [2 / (int) !!(expr) - 1] )
89 #else
90 // Some other compilers fail when declaring same function multiple times in class,
91 // so differentiate them by line
92 #define BOOST_STATIC_ASSERT( expr ) \
93 void blargg_failed_( int (*arg) [2 / !!(expr) - 1] [__LINE__] )
94 #endif
95#endif
96
97// BLARGG_COMPILER_HAS_BOOL: If 0, provides bool support for old compiler. If 1,
98// compiler is assumed to support bool. If undefined, availability is determined.
99#ifndef BLARGG_COMPILER_HAS_BOOL
100 #if defined (__MWERKS__)
101 #if !__option(bool)
102 #define BLARGG_COMPILER_HAS_BOOL 0
103 #endif
104 #elif defined (_MSC_VER)
105 #if _MSC_VER < 1100
106 #define BLARGG_COMPILER_HAS_BOOL 0
107 #endif
108 #elif defined (__GNUC__)
109 // supports bool
110 #elif __cplusplus < 199711
111 #define BLARGG_COMPILER_HAS_BOOL 0
112 #endif
113#endif
114#if defined (BLARGG_COMPILER_HAS_BOOL) && !BLARGG_COMPILER_HAS_BOOL
115 // If you get errors here, modify your blargg_config.h file
116 typedef int bool;
117 const bool true = 1;
118 const bool false = 0;
119#endif
120
121// blargg_long/blargg_ulong = at least 32 bits, int if it's big enough
122
123#if INT_MAX < 0x7FFFFFFF || LONG_MAX == 0x7FFFFFFF
124 typedef long blargg_long;
125#else
126 typedef int blargg_long;
127#endif
128
129#if UINT_MAX < 0xFFFFFFFF || ULONG_MAX == 0xFFFFFFFF
130 typedef unsigned long blargg_ulong;
131#else
132 typedef unsigned blargg_ulong;
133#endif
134
135// int8_t etc.
136
137// TODO: Add CMake check for this, although I'd likely just point affected
138// persons to a real compiler...
139#if 1 || defined (HAVE_STDINT_H)
140 #include <stdint.h>
141#endif
142
143#if __GNUC__ >= 3
144 #define BLARGG_DEPRECATED __attribute__ ((deprecated))
145#else
146 #define BLARGG_DEPRECATED
147#endif
148
149// Use in place of "= 0;" for a pure virtual, since these cause calls to std C++ lib.
150// During development, BLARGG_PURE( x ) expands to = 0;
151// virtual int func() BLARGG_PURE( { return 0; } )
152#ifndef BLARGG_PURE
153 #define BLARGG_PURE( def ) def
154#endif
155
156#endif
157#endif
assert(0)
void clear(void *s)
Definition juce_FixedSizeFunction.h:71
RangedDirectoryIterator end(const RangedDirectoryIterator &)
Definition juce_RangedDirectoryIterator.h:184
RangedDirectoryIterator begin(const RangedDirectoryIterator &it)
Definition juce_RangedDirectoryIterator.h:179
int n
Definition crypt.c:458
uch * p
Definition crypt.c:594
ulg size
Definition extract.c:2350