Visual Studio 2012 Support
[xy_vsfilter.git] / src / thirdparty / VirtualDub / h / vd2 / system / vdtypes.h
blobe5e196cad21876c82d8cb485cf0c3e9286763e48
1 // VirtualDub - Video processing and capture application
2 // System library component
3 // Copyright (C) 1998-2007 Avery Lee, All Rights Reserved.
4 //
5 // Beginning with 1.6.0, the VirtualDub system library is licensed
6 // differently than the remainder of VirtualDub. This particular file is
7 // thus licensed as follows (the "zlib" license):
8 //
9 // This software is provided 'as-is', without any express or implied
10 // warranty. In no event will the authors be held liable for any
11 // damages arising from the use of this software.
13 // Permission is granted to anyone to use this software for any purpose,
14 // including commercial applications, and to alter it and redistribute it
15 // freely, subject to the following restrictions:
17 // 1. The origin of this software must not be misrepresented; you must
18 // not claim that you wrote the original software. If you use this
19 // software in a product, an acknowledgment in the product
20 // documentation would be appreciated but is not required.
21 // 2. Altered source versions must be plainly marked as such, and must
22 // not be misrepresented as being the original software.
23 // 3. This notice may not be removed or altered from any source
24 // distribution.
26 #ifndef f_VD2_SYSTEM_VDTYPES_H
27 #define f_VD2_SYSTEM_VDTYPES_H
29 #ifdef _MSC_VER
30 #pragma once
31 #endif
33 #include <algorithm>
34 #include <stdio.h>
35 #include <stdarg.h>
36 #include <new>
38 #ifndef NULL
39 #define NULL 0
40 #endif
42 ///////////////////////////////////////////////////////////////////////////
44 // compiler detection
46 ///////////////////////////////////////////////////////////////////////////
48 #ifndef VD_COMPILER_DETECTED
49 #define VD_COMPILER_DETECTED
51 #if defined(_MSC_VER)
52 #define VD_COMPILER_MSVC _MSC_VER
54 #if _MSC_VER >= 1400
55 #define VD_COMPILER_MSVC_VC8 1
56 #define VD_COMPILER_MSVC_VC8_OR_LATER 1
58 #if _MSC_FULL_VER == 140040310
59 #define VD_COMPILER_MSVC_VC8_PSDK 1
60 #elif _MSC_FULL_VER == 14002207
61 #define VD_COMPILER_MSVC_VC8_DDK 1
62 #endif
64 #elif _MSC_VER >= 1310
65 #define VD_COMPILER_MSVC_VC71 1
66 #elif _MSC_VER >= 1300
67 #define VD_COMPILER_MSVC_VC7 1
68 #elif _MSC_VER >= 1200
69 #define VD_COMPILER_MSVC_VC6 1
70 #endif
71 #elif defined(__GNUC__)
72 #define VD_COMPILER_GCC
73 #if defined(__MINGW32__) || defined(__MINGW64__)
74 #define VD_COMPILER_GCC_MINGW
75 #endif
76 #endif
77 #endif
79 #ifndef VD_CPU_DETECTED
80 #define VD_CPU_DETECTED
82 #if defined(_M_AMD64)
83 #define VD_CPU_AMD64 1
84 #elif defined(_M_IX86) || defined(__i386__)
85 #define VD_CPU_X86 1
86 #elif defined(_M_ARM)
87 #define VD_CPU_ARM
88 #endif
89 #endif
91 ///////////////////////////////////////////////////////////////////////////
93 // types
95 ///////////////////////////////////////////////////////////////////////////
97 #ifndef VD_STANDARD_TYPES_DECLARED
98 #if defined(_MSC_VER)
99 typedef signed __int64 sint64;
100 typedef unsigned __int64 uint64;
101 #elif defined(__GNUC__)
102 typedef signed long long sint64;
103 typedef unsigned long long uint64;
104 #endif
105 typedef signed int sint32;
106 typedef unsigned int uint32;
107 typedef signed short sint16;
108 typedef unsigned short uint16;
109 typedef signed char sint8;
110 typedef unsigned char uint8;
112 typedef sint64 int64;
113 typedef sint32 int32;
114 typedef sint16 int16;
115 typedef sint8 int8;
117 #ifdef _M_AMD64
118 typedef sint64 sintptr;
119 typedef uint64 uintptr;
120 #else
121 #if _MSC_VER >= 1310
122 typedef __w64 sint32 sintptr;
123 typedef __w64 uint32 uintptr;
124 #else
125 typedef sint32 sintptr;
126 typedef uint32 uintptr;
127 #endif
128 #endif
129 #endif
131 #if defined(_MSC_VER)
132 #define VD64(x) x##i64
133 #elif defined(__GNUC__)
134 #define VD64(x) x##ll
135 #else
136 #error Please add an entry for your compiler for 64-bit constant literals.
137 #endif
140 #define VDAPIENTRY __cdecl
142 typedef int64 VDTime;
143 typedef int64 VDPosition;
144 typedef struct __VDGUIHandle *VDGUIHandle;
146 // enforce wchar_t under Visual C++
148 #if defined(_MSC_VER) && !defined(_WCHAR_T_DEFINED)
149 #include <ctype.h>
150 #endif
152 ///////////////////////////////////////////////////////////////////////////
154 // allocation
156 ///////////////////////////////////////////////////////////////////////////
158 #if defined(VD_COMPILER_MSVC) && (VD_COMPILER_MSVC < 1300 || (defined(VD_COMPILER_MSVC_VC8_PSDK) || defined(VD_COMPILER_MSVC_VC8_DDK)))
159 #define new_nothrow new
160 #else
161 #define new_nothrow new(std::nothrow)
162 #endif
164 ///////////////////////////////////////////////////////////////////////////
166 // STL fixes
168 ///////////////////////////////////////////////////////////////////////////
170 #if defined(VD_COMPILER_MSVC_VC6) || defined(VD_COMPILER_MSVC_VC8_DDK) || defined(VD_COMPILER_MSVC_VC8_PSDK)
171 // The VC6 STL was deliberately borked to avoid conflicting with
172 // Windows min/max macros. We work around this bogosity here. Note
173 // that NOMINMAX must be defined for these to compile properly. Also,
174 // there is a bug in the VC6 compiler that sometimes causes long
175 // lvalues to "promote" to int, causing ambiguous override errors.
176 // To avoid this, always explicitly declare which type you are using,
177 // i.e. min<int>(x,0). None of this is a problem with VC7 or later.
178 namespace std {
179 template<class T>
180 inline const T& min(const T& x, const T& y) {
181 return _cpp_min(x, y);
184 template<class T>
185 inline const T& max(const T& x, const T& y) {
186 return _cpp_max(x, y);
189 #endif
191 ///////////////////////////////////////////////////////////////////////////
193 // compiler fixes
195 ///////////////////////////////////////////////////////////////////////////
197 #if defined(VD_COMPILER_MSVC) && (VD_COMPILER_MSVC < 1400 || (defined(VD_COMPILER_MSVC_VC8_PSDK) || defined(VD_COMPILER_MSVC_VC8_DDK)))
198 inline int vswprintf(wchar_t *dst, size_t bufsize, const wchar_t *format, va_list val) {
199 return _vsnwprintf(dst, bufsize, format, val);
202 inline int swprintf(wchar_t *dst, size_t bufsize, const wchar_t *format, ...) {
203 va_list val;
205 va_start(val, format);
206 int r = vswprintf(dst, bufsize, format, val);
207 va_end(val);
209 return r;
212 #define _strdup strdup
213 #define _stricmp stricmp
214 #define _strnicmp strnicmp
215 #define _wcsdup wcsdup
216 #define _wcsicmp wcsicmp
217 #define _wcsnicmp wcsnicmp
218 #endif
220 #if defined(VD_COMPILER_MSVC) && VD_COMPILER_MSVC < 1400
221 #define vdfor if(0);else for
222 #else
223 #define vdfor for
224 #endif
226 ///////////////////////////////////////////////////////////////////////////
228 // attribute support
230 ///////////////////////////////////////////////////////////////////////////
232 #if defined(VD_COMPILER_MSVC)
233 #define VDINTERFACE __declspec(novtable)
234 #define VDNORETURN __declspec(noreturn)
235 #define VDPUREFUNC
237 #if VD_COMPILER_MSVC >= 1400
238 #define VDRESTRICT __restrict
239 #else
240 #define VDRESTRICT
241 #endif
243 #define VDNOINLINE __declspec(noinline)
244 #define VDFORCEINLINE __forceinline
245 #define VDALIGN(alignment) __declspec(align(alignment))
246 #elif defined(VD_COMPILER_GCC)
247 #define VDINTERFACE
248 #define VDNORETURN __attribute__((noreturn))
249 #define VDPUREFUNC __attribute__((pure))
250 #define VDRESTRICT __restrict
251 #define VDNOINLINE __attribute__((noinline))
252 #define VDFORCEINLINE inline __attribute__((always_inline))
253 #define VDALIGN(alignment) __attribute__((aligned(alignment)))
254 #else
255 #define VDINTERFACE
256 #define VDNORETURN
257 #define VDPUREFUNC
258 #define VDRESTRICT
259 #define VDFORCEINLINE
260 #define VDALIGN(alignment)
261 #endif
263 ///////////////////////////////////////////////////////////////////////////
265 // debug support
267 ///////////////////////////////////////////////////////////////////////////
269 enum VDAssertResult {
270 kVDAssertBreak,
271 kVDAssertContinue,
272 kVDAssertIgnore
275 extern VDAssertResult VDAssert(const char *exp, const char *file, int line);
276 extern VDAssertResult VDAssertPtr(const char *exp, const char *file, int line);
277 extern void VDDebugPrint(const char *format, ...);
279 #if defined(_MSC_VER)
280 #if _MSC_VER >= 1300
281 #define VDBREAK __debugbreak()
282 #else
283 #define VDBREAK __asm { int 3 }
284 #endif
285 #elif defined(__GNUC__)
286 #define VDBREAK __asm__ volatile ("int3" : : )
287 #else
288 #define VDBREAK *(volatile char *)0 = *(volatile char *)0
289 #endif
292 #ifdef _DEBUG
294 namespace {
295 template<int line>
296 struct VDAssertHelper {
297 VDAssertHelper(const char *exp, const char *file) {
298 if (!sbAssertDisabled)
299 switch(VDAssert(exp, file, line)) {
300 case kVDAssertBreak:
301 VDBREAK;
302 break;
303 case kVDAssertIgnore:
304 sbAssertDisabled = true;
305 break;
309 static bool sbAssertDisabled;
312 template<int lineno>
313 bool VDAssertHelper<lineno>::sbAssertDisabled;
315 template<int lineno>
316 struct VDAssertHelper2 { static bool sDisabled; };
318 template<int lineno>
319 bool VDAssertHelper2<lineno>::sDisabled;
322 // MPC-HC custom code start
323 // We use the old code since the new templated code isn't compatible with MSVC "Edit and continue" feature
324 // because __LINE__ can't be known at compile time.
325 #if !defined(VD_COMPILER_MSVC) || defined(__INTEL_COMPILER)
326 #define VDASSERT(exp) if (!VDAssertHelper2<__LINE__>::sDisabled) if (exp); else switch(VDAssert (#exp, __FILE__, __LINE__)) { case kVDAssertBreak: VDBREAK; break; case kVDAssertIgnore: VDAssertHelper2<__LINE__>::sDisabled = true; } else ((void)0)
327 #define VDASSERTPTR(exp) if (!VDAssertHelper2<__LINE__>::sDisabled) if (exp); else switch(VDAssertPtr(#exp, __FILE__, __LINE__)) { case kVDAssertBreak: VDBREAK; break; case kVDAssertIgnore: VDAssertHelper2<__LINE__>::sDisabled = true; } else ((void)0)
328 #define VDVERIFY(exp) if (exp); else if (!VDAssertHelper2<__LINE__>::sDisabled) switch(VDAssert (#exp, __FILE__, __LINE__)) { case kVDAssertBreak: VDBREAK; break; case kVDAssertIgnore: VDAssertHelper2<__LINE__>::sDisabled = true; } else ((void)0)
329 #define VDVERIFYPTR(exp) if (exp); else if (!VDAssertHelper2<__LINE__>::sDisabled) switch(VDAssertPtr(#exp, __FILE__, __LINE__)) { case kVDAssertBreak: VDBREAK; break; case kVDAssertIgnore: VDAssertHelper2<__LINE__>::sDisabled = true; } else ((void)0)
330 #else
331 #define VDASSERT(exp) if (static bool active = true) if (exp); else switch(VDAssert (#exp, __FILE__, __LINE__)) { case kVDAssertBreak: VDBREAK; break; case kVDAssertIgnore: active = false; } else ((void)0)
332 #define VDASSERTPTR(exp) if (static bool active = true) if (exp); else switch(VDAssertPtr(#exp, __FILE__, __LINE__)) { case kVDAssertBreak: VDBREAK; break; case kVDAssertIgnore: active = false; } else ((void)0)
333 #define VDVERIFY(exp) if (exp); else if (static bool active = true) switch(VDAssert (#exp, __FILE__, __LINE__)) { case kVDAssertBreak: VDBREAK; break; case kVDAssertIgnore: active = false; } else ((void)0)
334 #define VDVERIFYPTR(exp) if (exp); else if (static bool active = true) switch(VDAssertPtr(#exp, __FILE__, __LINE__)) { case kVDAssertBreak: VDBREAK; break; case kVDAssertIgnore: active = false; } else ((void)0)
335 #endif
336 // MPC-HC custom code end
337 #define VDASSERTCT(exp) (void)sizeof(int[(exp)?1:-1])
339 #define VDINLINEASSERT(exp) ((exp)||(VDAssertHelper<__LINE__>(#exp, __FILE__),false))
340 #define VDINLINEASSERTFALSE(exp) ((exp)&&(VDAssertHelper<__LINE__>("!("#exp")", __FILE__),true))
342 #define NEVER_HERE do { if (VDAssert( "[never here]", __FILE__, __LINE__ )) VDBREAK; __assume(false); } while(false)
343 #define VDNEVERHERE do { if (VDAssert( "[never here]", __FILE__, __LINE__ )) VDBREAK; __assume(false); } while(false)
345 #define VDDEBUG VDDebugPrint
347 #else
349 #if defined(_MSC_VER)
350 #ifndef _M_AMD64
351 #define VDASSERT(exp) __assume(!!(exp))
352 #define VDASSERTPTR(exp) __assume(!!(exp))
353 #else
354 #define VDASSERT(exp) __noop(exp)
355 #define VDASSERTPTR(exp) __noop(exp)
356 #endif
357 #elif defined(__GNUC__)
358 #define VDASSERT(exp) __builtin_expect(0 != (exp), 1)
359 #define VDASSERTPTR(exp) __builtin_expect(0 != (exp), 1)
360 #endif
362 #define VDVERIFY(exp) (exp)
363 #define VDVERIFYPTR(exp) (exp)
364 #define VDASSERTCT(exp)
366 #define VDINLINEASSERT(exp) (exp)
367 #define VDINLINEASSERTFALSE(exp) (exp)
369 #if defined(VD_COMPILER_MSVC)
370 #define NEVER_HERE __assume(false)
371 #define VDNEVERHERE __assume(false)
372 #else
373 #define NEVER_HERE VDASSERT(false)
374 #define VDNEVERHERE VDASSERT(false)
375 #endif
377 extern int VDDEBUG_Helper(const char *, ...);
378 #define VDDEBUG (void)sizeof VDDEBUG_Helper
380 #endif
382 #define VDDEBUG2 VDDebugPrint
384 // TODO macros
386 // These produce a diagnostic during compilation that indicate a TODO for
387 // later:
389 // #pragma message(__TODO__ "Fix this.)
390 // #vdpragma_TODO("Fix this.")
392 #define vdpragma_TODO2(x) #x
393 #define vdpragma_TODO1(x) vdpragma_TODO2(x)
394 #define vdpragma_TODO0 __FILE__ "(" vdpragma_TODO1(__LINE__) ") : TODO: "
396 #ifdef _MSC_VER
397 #define vdpragma_TODO(x) message(vdpragma_TODO0 x)
398 #else
399 #define vdpragma_TODO(x)
400 #endif
402 // BS macros
404 // These tag code that is not meant to go into a final build.
406 #define vdpragma_BS2(x) #x
407 #define vdpragma_BS1(x) vdpragma_BS2(x)
408 #define vdpragma_BS0 __FILE__ "(" vdpragma_BS1(__LINE__) ") : BS: "
410 #ifdef _MSC_VER
411 #define vdpragma_BS(x) message(vdpragma_BS0 x)
412 #else
413 #define vdpragma_BS(x)
414 #endif
416 ///////////////////////////////////////////////////////////////////////////
418 // Object scope macros
420 // vdobjectscope() allows you to define a construct where an object is
421 // constructed and live only within the controlled statement. This is
422 // used for vdsynchronized (thread.h) and protected scopes below.
423 // It relies on a strange quirk of C++ regarding initialized objects
424 // in the condition of a selection statement and also horribly abuses
425 // the switch statement, generating rather good code in release builds.
426 // The catch is that the controlled object must implement a conversion to
427 // bool returning false and must only be initialized with one argument (C
428 // syntax).
430 // Unfortunately, handy as this macro is, it is also damned good at
431 // breaking compilers. For a start, declaring an object with a non-
432 // trivial destructor in a switch() kills both VC6 and VC7 with a C1001.
433 // The bug is fixed in VC8 (MSC 14.00).
435 // A somewhat safer alternative is the for() statement, along the lines
436 // of:
438 // switch(bool v=false) case 0: default: for(object_def; !v; v=true)
440 // This avoids the conversion operator but unfortunately usually generates
441 // an actual loop in the output.
443 #if defined(VD_COMPILER_MSVC) && (VD_COMPILER_MSVC < 1400 || defined(VD_COMPILER_MSVC_VC8_DDK))
444 #define vdobjectscope(object_def) if(object_def) VDNEVERHERE; else
445 #else
446 #define vdobjectscope(object_def) switch(object_def) case 0: default:
447 #endif
449 #endif