Move 'round()' / 'trunc()' etc so that they won't conflict with any versions already...
[ardour.git] / msvc_extra_headers / ardourext / misc.h.input
1 /*
2     Copyright (C) 2009 John Emmas
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __ardour_msvc_extensions_h__
21 #define __ardour_msvc_extensions_h__
22
23 #ifndef _WIN32_WINNT
24 #define _WIN32_WINNT 0x0500
25 #endif
26 #ifndef _CPP_VECTOR
27 #define _CPP_VECTOR  1
28 #endif
29
30 #ifdef __cplusplus
31 #include <vector>
32 #endif
33
34 #include <math.h>
35 #include <float.h>
36 #include <direct.h>
37 #include <boost/regex.h>
38 #include <glib.h>
39 #include <ardourext/float_cast.h>
40
41 // 'std::isnan()' is not available in MSVC. Assume '_isnan(double)'
42 #define isnan(val)  _isnan((double)val)
43
44 // 'std::isinf()' is not available in MSVC. Assume '!_finite(double)'
45 #define isinf(val) !_finite((double)val)
46
47 // 'INFINITY' is not defined in MSVC. Assume 'HUGE_VAL'
48 #ifndef INFINITY
49 #define INFINITY    HUGE_VAL
50 #endif
51
52 #ifndef NAN
53 #define NAN        _NANCODE
54 #endif
55
56 // File access modes copied from unistd.h
57 #define F_OK 0
58 #define R_OK 4
59 #define W_OK 2
60 #define X_OK 1
61
62 // Miscellaneous #defines
63 #define __attribute__(x)
64 #define llabs      _abs64
65 #define atoll      _atoi64
66 #define access     _access
67 #define getcwd     _getcwd
68 #define getpid     _getpid
69 #define snprintf   _snprintf
70 #define random      rand
71 #define link        ntfs_link
72 #define unlink      ntfs_unlink
73 #define strcasecmp  stricmp
74 #define strncasecmp strnicmp
75 #define strtok_r( _s, _sep, _lasts ) \
76         ( *(_lasts) = strtok( (_s), (_sep) ) )
77
78 #ifndef PATH_MAX
79 #define PATH_MAX _MAX_PATH
80 #endif
81
82 // Types missing from Win32 'stat.h' (hopefully Windows
83 // will either act sensibly or ignore most of them).
84 #define _S_IFBLK                0x3000
85 #define S_IRWXU                 _S_IRWXU
86 #define S_IXUSR                 _S_IXUSR
87 #define S_IWUSR                 _S_IWUSR
88 #define S_IRUSR                 _S_IRUSR
89 #define S_IXGRP                 _S_IXGRP
90 #define S_IWGRP                 _S_IWGRP
91 #define S_IRGRP                 _S_IRGRP
92 #define S_IXOTH                 _S_IXOTH
93 #define S_IWOTH                 _S_IWOTH
94 #define S_IROTH                 _S_IROTH
95
96 #define _S_IRWXU                (_S_IREAD | _S_IWRITE | _S_IEXEC)
97 #define _S_IXUSR                _S_IEXEC
98 #define _S_IWUSR                _S_IWRITE
99 #define _S_IRUSR                _S_IREAD
100 #define _S_IXGRP                _S_IEXEC
101 #define _S_IWGRP                _S_IWRITE
102 #define _S_IRGRP                _S_IREAD
103 #define _S_IXOTH                _S_IEXEC
104 #define _S_IWOTH                _S_IWRITE
105 #define _S_IROTH                _S_IREAD
106
107 #define S_ISFIFO(m)             _S_ISFIFO(m)
108 #define S_ISDIR(m)              _S_ISDIR(m)
109 #define S_ISCHR(m)              _S_ISCHR(m)
110 #define S_ISBLK(m)              _S_ISBLK(m)
111 #define S_ISREG(m)              _S_ISREG(m)
112
113 #define _S_ISFIFO(m)    (((m) & _S_IFMT) == _S_IFIFO)
114 #define _S_ISDIR(m)             (((m) & _S_IFMT) == _S_IFDIR)
115 #define _S_ISCHR(m)             (((m) & _S_IFMT) == _S_IFCHR)
116 #define _S_ISBLK(m)             (((m) & _S_IFMT) == _S_IFBLK)
117 #define _S_ISREG(m)             (((m) & _S_IFMT) == _S_IFREG)
118
119
120 #if defined(__USE_BSD) || defined(_BSD_SOURCE)
121 /* Convenience macros for operations on timevals.
122    NOTE: `timercmp' does not work for >= or <=.
123    Note also that 'timerset', 'timerclear' and
124    'timercmp' are (perhaps strangely) already
125    defined, along with various other 'time'
126    functions in WinSock.h  */
127 # define timeradd(a, b, result)                                               \
128   do {                                                                        \
129     (result)->tv_sec = (a)->tv_sec + (b)->tv_sec;                             \
130     (result)->tv_usec = (a)->tv_usec + (b)->tv_usec;                          \
131     if ((result)->tv_usec >= 1000000)                                         \
132       {                                                                       \
133     ++(result)->tv_sec;                                               \
134     (result)->tv_usec -= 1000000;                                             \
135       }                                                                       \
136   } while (0)
137 # define timersub(a, b, result)                                               \
138   do {                                                                        \
139     (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;                             \
140     (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;                          \
141     if ((result)->tv_usec < 0) {                                              \
142       --(result)->tv_sec;                                                     \
143       (result)->tv_usec += 1000000;                                           \
144     }                                                                         \
145   } while (0)
146 #endif  /* BSD */
147
148 #if !defined(__BIT_TYPES_DEFINED) || !defined(__BIT_TYPES_DEFINED__)
149 #define __BIT_TYPES_DEFINED__ 1
150 // Doesn't yet define all 'bit types'. Only those
151 // needed by Ardour. More can be added as needed.
152 #ifndef __int8_t_defined
153 #define __int8_t_defined
154 typedef unsigned char      u_int8_t;
155 typedef unsigned short int u_int16_t;
156 typedef unsigned int       u_int32_t;
157 typedef unsigned __int64   u_int64_t;
158
159 typedef signed char        int8_t;
160 typedef unsigned char      uint8_t;
161 typedef short              int16_t;
162 typedef unsigned short     uint16_t;
163 typedef int                int32_t;
164 typedef unsigned           uint32_t;
165 typedef long long          int64_t;
166 typedef unsigned long long uint64_t;
167 #endif  // __int8_t
168
169 #ifndef __register_t_defined
170 #define __register_t_defined
171 typedef int register_t;
172 #endif  // __register_t
173 #endif  // __BIT_TYPESD
174
175 // throw()
176 #ifndef __THROW
177 #ifdef __cplusplus
178 #define __THROW  throw()
179 #else
180 #define __THROW
181 #endif
182 #endif
183
184 // System V compatibility
185 typedef unsigned short ushort;
186 typedef unsigned int   uint;
187
188 // mode_t
189 #ifndef _MODE_T_
190 #define _MODE_T_
191 typedef unsigned short _mode_t;
192
193 #ifndef NO_OLDNAMES
194 typedef _mode_t mode_t;
195 #endif /* NO_OLDNAMES */
196 #endif /* _MODE_T_ */
197
198 // int64 abs()
199 #ifdef __cplusplus // Normal 'C' doesn't permit over-ridden functions !!
200 inline int64_t abs(int64_t val) throw()
201 {
202         if (val < 0)
203                 return val * (-1);
204         else
205                 return val;
206 }
207 #endif
208
209 // fmin() and fmax()
210 #define fmin(a, b) min((double)a, (double)b)
211 #define fmax(a, b) max((double)a, (double)b)
212
213 // approximate POSIX pipe()
214 #define pipe(handles) _pipe(handles, 4096, _O_BINARY)
215
216 // Windows mkdir() doesn't care about access privileges
217 #define mkdir(path, mode) _mkdir(path)
218
219 // Redefine 'ftruncate()' to use the glib-win32 version
220 #define ftruncate(fd, len) g_win32_ftruncate((gint)fd, (guint)len)
221
222
223 // #include the main headers for Ardour MSVC
224 #ifdef __cplusplus
225 #if defined(LIBPBD_DLL) || defined(PBD_IS_IN_WIN_STATIC_LIB)
226 #include <pbd/msvc_pbd.h>
227
228 #ifdef LIBPBD_DLL
229 #define DEFAULT_COMPARISONS_DEFINED
230 #define DECLARE_DEFAULT_COMPARISONS(Type) \
231         LIBPBD_API bool operator >  (const Type& lhs, const Type& rhs); \
232         LIBPBD_API bool operator <  (const Type& lhs, const Type& rhs); \
233         LIBPBD_API bool operator != (const Type& lhs, const Type& rhs); \
234         LIBPBD_API bool operator == (const Type& lhs, const Type& rhs);
235 #endif
236 #endif
237 #if defined(BUILDING_LIBARDOUR) || defined(LIBARDOUR_IS_IN_WIN_STATIC_LIB)
238 #include <ardour/msvc_libardour.h>
239 #endif
240 #if defined(BUILDING_RUBBERBAND) || defined(RUBBERBAND_IS_IN_WIN_STATIC_LIB)
241 #include <rubberband/msvc_rubberband.h>
242 #endif
243 #endif // __cplusplus
244
245 #ifndef DEFAULT_COMPARISONS_DEFINED
246 #define DEFAULT_COMPARISONS_DEFINED
247 #define DECLARE_DEFAULT_COMPARISONS(Type) \
248         extern bool operator >  (const Type& lhs, const Type& rhs); \
249         extern bool operator <  (const Type& lhs, const Type& rhs); \
250         extern bool operator != (const Type& lhs, const Type& rhs); \
251         extern bool operator == (const Type& lhs, const Type& rhs);
252 #endif
253
254 #if !defined(LIBPBD_API) || defined(PBD_IS_IN_WIN_STATIC_LIB)
255 // Emulate some C99 math functions which MSVC itself didn't
256 // implement until later in life.
257 #if defined(_MSC_VER) && (_MSC_VER < 1800)
258 extern  double expm1(double x);
259 extern  double log1p(double x);
260 extern  double round(double x);
261 extern  float roundf(float x);
262 #endif
263 #if defined(_MSC_VER) && (_MSC_VER < 1900)
264 extern  double log2 (double x);
265 extern  double trunc(double x);
266 #endif
267 #endif
268
269 #endif /* __ardour_msvc_extensions_h__ */