VirtualBox

source: vbox/trunk/include/iprt/cdefs.h@ 5605

Last change on this file since 5605 was 5605, checked in by vboxsync, 17 years ago

BIT => RT_BIT, BIT64 => RT_BIT_64. BIT() is defined in Linux 2.6.24

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.6 KB
Line 
1/** @file
2 * innotek Portable Runtime - Common C and C++ definitions.
3 */
4
5/*
6 * Copyright (C) 2006-2007 innotek GmbH
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License as published by the Free Software Foundation,
12 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
13 * distribution. VirtualBox OSE is distributed in the hope that it will
14 * be useful, but WITHOUT ANY WARRANTY of any kind.
15 */
16
17#ifndef ___iprt_cdefs_h
18#define ___iprt_cdefs_h
19
20
21/** @defgroup grp_rt_cdefs innotek Portable Runtime Common Definitions and Macros
22 * @{
23 */
24
25/*
26 * Include sys/cdefs.h if present, if not define the stuff we need.
27 */
28#ifdef HAVE_SYS_CDEFS_H
29# if defined(RT_ARCH_LINUX) && defined(__KERNEL__)
30# error "oops"
31# endif
32# include <sys/cdefs.h>
33#else
34
35 /** @def __BEGIN_DECLS
36 * Used to start a block of function declarations which are shared
37 * between C and C++ program.
38 */
39
40 /** @def __END_DECLS
41 * Used to end a block of function declarations which are shared
42 * between C and C++ program.
43 */
44
45 #if defined(__cplusplus)
46 # define __BEGIN_DECLS extern "C" {
47 # define __END_DECLS }
48 #else
49 # define __BEGIN_DECLS
50 # define __END_DECLS
51 #endif
52
53#endif
54
55
56/*
57 * Shut up DOXYGEN warnings and guide it properly thru the code.
58 */
59#ifdef __DOXYGEN__
60#define __AMD64__
61#define __X86__
62#define RT_ARCH_AMD64
63#define RT_ARCH_X86
64#define IN_RING0
65#define IN_RING3
66#define IN_GC
67#define IN_RT_GC
68#define IN_RT_R0
69#define IN_RT_R3
70#define RT_STRICT
71#define Breakpoint
72#define RT_NO_DEPRECATED_MACROS
73#define ARCH_BITS
74#define HC_ARCH_BITS
75#define R3_ARCH_BITS
76#define R0_ARCH_BITS
77#define GC_ARCH_BITS
78#endif /* __DOXYGEN__ */
79
80/* A temporary hack. */
81#ifndef RT_OS_LINUX
82# ifdef __LINUX__
83# define RT_OS_LINUX
84# warning "__LINUX__ without RT_OS_LINUX!"
85# endif
86#endif
87
88/** @def RT_ARCH_X86
89 * Indicates that we're compiling for the X86 architecture.
90 */
91
92/** @def RT_ARCH_AMD64
93 * Indicates that we're compiling for the AMD64 architecture.
94 */
95#if !defined(RT_ARCH_X86) && !defined(RT_ARCH_AMD64)
96# if defined(__amd64__) || defined(__x86_64__) || defined(_M_X64) || defined(__AMD64__)
97# define RT_ARCH_AMD64
98# elif defined(__i386__) || defined(_M_IX86) || defined(__X86__)
99# define RT_ARCH_X86
100# else /* PORTME: append test for new archs. */
101# error "Check what predefined stuff your compiler uses to indicate architecture."
102# endif
103#elif defined(RT_ARCH_X86) && defined(RT_ARCH_AMD64) /* PORTME: append new archs. */
104# error "Both RT_ARCH_X86 and RT_ARCH_AMD64 cannot be defined at the same time!"
105#endif
106
107
108/** @def __X86__
109 * Indicates that we're compiling for the X86 architecture.
110 * @deprecated
111 */
112
113/** @def __AMD64__
114 * Indicates that we're compiling for the AMD64 architecture.
115 * @deprecated
116 */
117#if !defined(__X86__) && !defined(__AMD64__)
118# if defined(RT_ARCH_AMD64)
119# define __AMD64__
120# elif defined(RT_ARCH_X86)
121# define __X86__
122# else
123# error "Check what predefined stuff your compiler uses to indicate architecture."
124# endif
125#elif defined(__X86__) && defined(__AMD64__)
126# error "Both __X86__ and __AMD64__ cannot be defined at the same time!"
127#elif defined(__X86__) && !defined(RT_ARCH_X86)
128# error "Both __X86__ without RT_ARCH_X86!"
129#elif defined(__AMD64__) && !defined(RT_ARCH_AMD64)
130# error "Both __AMD64__ without RT_ARCH_AMD64!"
131#endif
132
133/** @def IN_RING0
134 * Used to indicate that we're compiling code which is running
135 * in Ring-0 Host Context.
136 */
137
138/** @def IN_RING3
139 * Used to indicate that we're compiling code which is running
140 * in Ring-3 Host Context.
141 */
142
143/** @def IN_GC
144 * Used to indicate that we're compiling code which is running
145 * in Guest Context (implies R0).
146 */
147#if !defined(IN_RING3) && !defined(IN_RING0) && !defined(IN_GC)
148# error "You must defined which context the compiled code should run in; IN_RING3, IN_RING0 or IN_GC"
149#endif
150#if (defined(IN_RING3) && (defined(IN_RING0) || defined(IN_GC)) ) \
151 || (defined(IN_RING0) && (defined(IN_RING3) || defined(IN_GC)) ) \
152 || (defined(IN_GC) && (defined(IN_RING3) || defined(IN_RING0)) )
153# error "Only one of the IN_RING3, IN_RING0, IN_GC defines should be defined."
154#endif
155
156
157/** @def ARCH_BITS
158 * Defines the bit count of the current context.
159 */
160#ifndef ARCH_BITS
161# if defined(RT_ARCH_AMD64)
162# define ARCH_BITS 64
163# else
164# define ARCH_BITS 32
165# endif
166#endif
167
168/** @def HC_ARCH_BITS
169 * Defines the host architechture bit count.
170 */
171#ifndef HC_ARCH_BITS
172# ifndef IN_GC
173# define HC_ARCH_BITS ARCH_BITS
174# else
175# define HC_ARCH_BITS 32
176# endif
177#endif
178
179/** @def R3_ARCH_BITS
180 * Defines the host ring-3 architechture bit count.
181 */
182#ifndef R3_ARCH_BITS
183# ifdef IN_RING3
184# define R3_ARCH_BITS ARCH_BITS
185# else
186# define R3_ARCH_BITS HC_ARCH_BITS
187# endif
188#endif
189
190/** @def R0_ARCH_BITS
191 * Defines the host ring-0 architechture bit count.
192 */
193#ifndef R0_ARCH_BITS
194# ifdef IN_RING0
195# define R0_ARCH_BITS ARCH_BITS
196# else
197# define R0_ARCH_BITS HC_ARCH_BITS
198# endif
199#endif
200
201/** @def GC_ARCH_BITS
202 * Defines the guest architechture bit count.
203 */
204#ifndef GC_ARCH_BITS
205# ifdef IN_GC
206# define GC_ARCH_BITS ARCH_BITS
207# else
208# define GC_ARCH_BITS 32
209# endif
210#endif
211
212
213/** @def CTXTYPE
214 * Declare a type differently in GC, R3 and R0.
215 *
216 * @param GCType The GC type.
217 * @param R3Type The R3 type.
218 * @param R0Type The R0 type.
219 * @remark For pointers used only in one context use GCPTRTYPE(), R3R0PTRTYPE(), R3PTRTYPE() or R0PTRTYPE().
220 */
221#ifdef IN_GC
222# define CTXTYPE(GCType, R3Type, R0Type) GCType
223#elif defined(IN_RING3)
224# define CTXTYPE(GCType, R3Type, R0Type) R3Type
225#else
226# define CTXTYPE(GCType, R3Type, R0Type) R0Type
227#endif
228
229/** @def GCTYPE
230 * Declare a type differently in GC and HC.
231 *
232 * @param GCType The GC type.
233 * @param HCType The HC type.
234 * @remark For pointers used only in one context use GCPTRTYPE(), R3R0PTRTYPE(), R3PTRTYPE() or R0PTRTYPE().
235 */
236#define GCTYPE(GCType, HCType) CTXTYPE(GCType, HCType, HCType)
237
238/** @def GCPTRTYPE
239 * Declare a pointer which is used in GC but appears in structure(s) used by
240 * both HC and GC. The main purpose is to make sure structures have the same
241 * size when built for different architectures.
242 *
243 * @param GCType The GC type.
244 */
245#define GCPTRTYPE(GCType) CTXTYPE(GCType, RTGCPTR, RTGCPTR)
246
247/** @def R3R0PTRTYPE
248 * Declare a pointer which is used in HC, is explicitely valid in ring 3 and 0,
249 * but appears in structure(s) used by both HC and GC. The main purpose is to
250 * make sure structures have the same size when built for different architectures.
251 *
252 * @param R3R0Type The R3R0 type.
253 * @remarks This used to be called HCPTRTYPE.
254 */
255#define R3R0PTRTYPE(R3R0Type) CTXTYPE(RTHCPTR, R3R0Type, R3R0Type)
256
257/** @def R3PTRTYPE
258 * Declare a pointer which is used in R3 but appears in structure(s) used by
259 * both HC and GC. The main purpose is to make sure structures have the same
260 * size when built for different architectures.
261 *
262 * @param R3Type The R3 type.
263 */
264#define R3PTRTYPE(R3Type) CTXTYPE(RTHCUINTPTR, R3Type, RTHCUINTPTR)
265
266/** @def R0PTRTYPE
267 * Declare a pointer which is used in R0 but appears in structure(s) used by
268 * both HC and GC. The main purpose is to make sure structures have the same
269 * size when built for different architectures.
270 *
271 * @param R0Type The R0 type.
272 */
273#define R0PTRTYPE(R0Type) CTXTYPE(RTHCUINTPTR, RTHCUINTPTR, R0Type)
274
275/** @def CTXSUFF
276 * Adds the suffix of the current context to the passed in
277 * identifier name. The suffix is HC or GC.
278 *
279 * This is macro should only be used in shared code to avoid a forrest of ifdefs.
280 * @param var Identifier name.
281 */
282/** @def OTHERCTXSUFF
283 * Adds the suffix of the other context to the passed in
284 * identifier name. The suffix is HC or GC.
285 *
286 * This is macro should only be used in shared code to avoid a forrest of ifdefs.
287 * @param var Identifier name.
288 */
289#ifdef IN_GC
290# define CTXSUFF(var) var##GC
291# define OTHERCTXSUFF(var) var##HC
292#else
293# define CTXSUFF(var) var##HC
294# define OTHERCTXSUFF(var) var##GC
295#endif
296
297/** @def CTXALLSUFF
298 * Adds the suffix of the current context to the passed in
299 * identifier name. The suffix is R3, R0 or GC.
300 *
301 * This is macro should only be used in shared code to avoid a forrest of ifdefs.
302 * @param var Identifier name.
303 */
304#ifdef IN_GC
305# define CTXALLSUFF(var) var##GC
306#elif defined(IN_RING0)
307# define CTXALLSUFF(var) var##R0
308#else
309# define CTXALLSUFF(var) var##R3
310#endif
311
312/** @def CTXMID
313 * Adds the current context as a middle name of an identifier name
314 * The middle name is HC or GC.
315 *
316 * This is macro should only be used in shared code to avoid a forrest of ifdefs.
317 * @param first First name.
318 * @param last Surname.
319 */
320/** @def OTHERCTXMID
321 * Adds the other context as a middle name of an identifier name
322 * The middle name is HC or GC.
323 *
324 * This is macro should only be used in shared code to avoid a forrest of ifdefs.
325 * @param first First name.
326 * @param last Surname.
327 */
328#ifdef IN_GC
329# define CTXMID(first, last) first##GC##last
330# define OTHERCTXMID(first, last) first##HC##last
331#else
332# define CTXMID(first, last) first##HC##last
333# define OTHERCTXMID(first, last) first##GC##last
334#endif
335
336/** @def CTXALLMID
337 * Adds the current context as a middle name of an identifier name
338 * The middle name is R3, R0 or GC.
339 *
340 * This is macro should only be used in shared code to avoid a forrest of ifdefs.
341 * @param first First name.
342 * @param last Surname.
343 */
344#ifdef IN_GC
345# define CTXALLMID(first, last) first##GC##last
346#elif defined(IN_RING0)
347# define CTXALLMID(first, last) first##R0##last
348#else
349# define CTXALLMID(first, last) first##R3##last
350#endif
351
352
353/** @def R3STRING
354 * A macro which in GC and R0 will return a dummy string while in R3 it will return
355 * the parameter.
356 *
357 * This is typically used to wrap description strings in structures shared
358 * between R3, R0 and/or GC. The intention is to avoid the \#ifdef IN_RING3 mess.
359 *
360 * @param pR3String The R3 string. Only referenced in R3.
361 * @see R0STRING and GCSTRING
362 */
363#ifdef IN_RING3
364# define R3STRING(pR3String) (pR3String)
365#else
366# define R3STRING(pR3String) ("<R3_STRING>")
367#endif
368
369/** @def R0STRING
370 * A macro which in GC and R3 will return a dummy string while in R0 it will return
371 * the parameter.
372 *
373 * This is typically used to wrap description strings in structures shared
374 * between R3, R0 and/or GC. The intention is to avoid the \#ifdef IN_RING0 mess.
375 *
376 * @param pR0String The R0 string. Only referenced in R0.
377 * @see R3STRING and GCSTRING
378 */
379#ifdef IN_RING0
380# define R0STRING(pR0String) (pR0String)
381#else
382# define R0STRING(pR0String) ("<R0_STRING>")
383#endif
384
385/** @def GCSTRING
386 * A macro which in R3 and R0 will return a dummy string while in GC it will return
387 * the parameter.
388 *
389 * This is typically used to wrap description strings in structures shared
390 * between R3, R0 and/or GC. The intention is to avoid the \#ifdef IN_GC mess.
391 *
392 * @param pR0String The GC string. Only referenced in GC.
393 * @see R3STRING, R0STRING
394 */
395#ifdef IN_GC
396# define GCSTRING(pR0String) (pGCString)
397#else
398# define GCSTRING(pR0String) ("<GC_STRING>")
399#endif
400
401/** @def HCSTRING
402 * Macro which in GC will return a dummy string while in HC will return
403 * the parameter.
404 *
405 * This is typically used to wrap description strings in structures shared
406 * between HC and GC. The intention is to avoid the \#ifdef IN_GC kludge.
407 *
408 * @param pHCString The HC string. Only referenced in HC.
409 * @deprecated Use R3STRING or R0STRING instead.
410 */
411#ifdef IN_GC
412# define HCSTRING(pHCString) ("<HC_STRING>")
413#else
414# define HCSTRING(pHCString) (pHCString)
415#endif
416
417
418/** @def RTCALL
419 * The standard calling convention for the Runtime interfaces.
420 */
421#ifdef _MSC_VER
422# define RTCALL __cdecl
423#elif defined(__GNUC__) && defined(IN_RING0) && !(defined(RT_OS_OS2) || defined(RT_ARCH_AMD64)) /* the latter is kernel/gcc */
424# define RTCALL __attribute__((cdecl,regparm(0)))
425#else
426# define RTCALL
427#endif
428
429/** @def DECLEXPORT
430 * How to declare an exported function.
431 * @param type The return type of the function declaration.
432 */
433#if defined(_MSC_VER) || defined(RT_OS_OS2)
434# define DECLEXPORT(type) __declspec(dllexport) type
435#else
436# ifdef VBOX_HAVE_VISIBILITY_HIDDEN
437# define DECLEXPORT(type) __attribute__((visibility("default"))) type
438# else
439# define DECLEXPORT(type) type
440# endif
441#endif
442
443/** @def DECLIMPORT
444 * How to declare an imported function.
445 * @param type The return type of the function declaration.
446 */
447#if defined(_MSC_VER) || (defined(RT_OS_OS2) && !defined(__IBMC__) && !defined(__IBMCPP__))
448# define DECLIMPORT(type) __declspec(dllimport) type
449#else
450# define DECLIMPORT(type) type
451#endif
452
453/** @def DECLASM
454 * How to declare an internal assembly function.
455 * @param type The return type of the function declaration.
456 */
457#ifdef __cplusplus
458# ifdef _MSC_VER
459# define DECLASM(type) extern "C" type __cdecl
460# else
461# define DECLASM(type) extern "C" type
462# endif
463#else
464# ifdef _MSC_VER
465# define DECLASM(type) type __cdecl
466# else
467# define DECLASM(type) type
468# endif
469#endif
470
471/** @def DECLASMTYPE
472 * How to declare an internal assembly function type.
473 * @param type The return type of the function.
474 */
475#ifdef _MSC_VER
476# define DECLASMTYPE(type) type __cdecl
477#else
478# define DECLASMTYPE(type) type
479#endif
480
481/** @def DECLCALLBACK
482 * How to declare an call back function type.
483 * @param type The return type of the function declaration.
484 */
485#define DECLCALLBACK(type) type RTCALL
486
487/** @def DECLCALLBACKPTR
488 * How to declare an call back function pointer.
489 * @param type The return type of the function declaration.
490 * @param name The name of the variable member.
491 */
492#define DECLCALLBACKPTR(type, name) type (RTCALL * name)
493
494/** @def DECLCALLBACKMEMBER
495 * How to declare an call back function pointer member.
496 * @param type The return type of the function declaration.
497 * @param name The name of the struct/union/class member.
498 */
499#define DECLCALLBACKMEMBER(type, name) type (RTCALL * name)
500
501/** @def DECLR3CALLBACKMEMBER
502 * How to declare an call back function pointer member - R3 Ptr.
503 * @param type The return type of the function declaration.
504 * @param name The name of the struct/union/class member.
505 * @param args The argument list enclosed in parentheses.
506 */
507#ifdef IN_RING3
508# define DECLR3CALLBACKMEMBER(type, name, args) type (RTCALL * name) args
509#else
510# define DECLR3CALLBACKMEMBER(type, name, args) RTR3PTR name
511#endif
512
513/** @def DECLGCCALLBACKMEMBER
514 * How to declare an call back function pointer member - GC Ptr.
515 * @param type The return type of the function declaration.
516 * @param name The name of the struct/union/class member.
517 * @param args The argument list enclosed in parentheses.
518 */
519#ifdef IN_GC
520# define DECLGCCALLBACKMEMBER(type, name, args) type (RTCALL * name) args
521#else
522# define DECLGCCALLBACKMEMBER(type, name, args) RTGCPTR name
523#endif
524
525/** @def DECLR0CALLBACKMEMBER
526 * How to declare an call back function pointer member - R0 Ptr.
527 * @param type The return type of the function declaration.
528 * @param name The name of the struct/union/class member.
529 * @param args The argument list enclosed in parentheses.
530 */
531#ifdef IN_RING0
532# define DECLR0CALLBACKMEMBER(type, name, args) type (RTCALL * name) args
533#else
534# define DECLR0CALLBACKMEMBER(type, name, args) RTR0PTR name
535#endif
536
537/** @def DECLINLINE
538 * How to declare a function as inline.
539 * @param type The return type of the function declaration.
540 * @remarks Don't use this macro on C++ methods.
541 */
542#ifdef __GNUC__
543# define DECLINLINE(type) static __inline__ type
544#elif defined(__cplusplus)
545# define DECLINLINE(type) inline type
546#elif defined(_MSC_VER)
547# define DECLINLINE(type) _inline type
548#elif defined(__IBMC__)
549# define DECLINLINE(type) _Inline type
550#else
551# define DECLINLINE(type) inline type
552#endif
553
554
555/** @def IN_RT_R0
556 * Used to indicate whether we're inside the same link module as
557 * the HC Ring-0 Runtime Library.
558 */
559/** @def RTR0DECL(type)
560 * Runtime Library HC Ring-0 export or import declaration.
561 * @param type The return type of the function declaration.
562 */
563#ifdef IN_RT_R0
564# define RTR0DECL(type) DECLEXPORT(type) RTCALL
565#else
566# define RTR0DECL(type) DECLIMPORT(type) RTCALL
567#endif
568
569/** @def IN_RT_R3
570 * Used to indicate whether we're inside the same link module as
571 * the HC Ring-3 Runtime Library.
572 */
573/** @def RTR3DECL(type)
574 * Runtime Library HC Ring-3 export or import declaration.
575 * @param type The return type of the function declaration.
576 */
577#ifdef IN_RT_R3
578# define RTR3DECL(type) DECLEXPORT(type) RTCALL
579#else
580# define RTR3DECL(type) DECLIMPORT(type) RTCALL
581#endif
582
583/** @def IN_RT_GC
584 * Used to indicate whether we're inside the same link module as
585 * the GC Runtime Library.
586 */
587/** @def RTGCDECL(type)
588 * Runtime Library HC Ring-3 export or import declaration.
589 * @param type The return type of the function declaration.
590 */
591#ifdef IN_RT_GC
592# define RTGCDECL(type) DECLEXPORT(type) RTCALL
593#else
594# define RTGCDECL(type) DECLIMPORT(type) RTCALL
595#endif
596
597/** @def RTDECL(type)
598 * Runtime Library export or import declaration.
599 * Functions declared using this macro exists in all contexts.
600 * @param type The return type of the function declaration.
601 */
602#if defined(IN_RT_R3) || defined(IN_RT_GC) || defined(IN_RT_R0)
603# define RTDECL(type) DECLEXPORT(type) RTCALL
604#else
605# define RTDECL(type) DECLIMPORT(type) RTCALL
606#endif
607
608/** @def RTDATADECL(type)
609 * Runtime Library export or import declaration.
610 * Data declared using this macro exists in all contexts.
611 * @param type The return type of the function declaration.
612 */
613#if defined(IN_RT_R3) || defined(IN_RT_GC) || defined(IN_RT_R0)
614# define RTDATADECL(type) DECLEXPORT(type)
615#else
616# define RTDATADECL(type) DECLIMPORT(type)
617#endif
618
619
620/** @def RT_NOCRT
621 * Symbol name wrapper for the No-CRT bits.
622 *
623 * In order to coexist in the same process as other CRTs, we need to
624 * decorate the symbols such that they don't conflict the ones in the
625 * other CRTs. The result of such conflicts / duplicate symbols can
626 * confuse the dynamic loader on unix like systems.
627 *
628 * Define RT_WITHOUT_NOCRT_WRAPPERS to drop the wrapping.
629 */
630/** @def RT_NOCRT_STR
631 * Same as RT_NOCRT only it'll return a double quoted string of the result.
632 */
633#ifndef RT_WITHOUT_NOCRT_WRAPPERS
634# define RT_NOCRT(name) nocrt_ ## name
635# define RT_NOCRT_STR(name) "nocrt_" # name
636#else
637# define RT_NOCRT(name) name
638# define RT_NOCRT_STR(name) #name
639#endif
640
641
642
643/** @def RT_LIKELY
644 * Give the compiler a hint that an expression is very likely to hold true.
645 *
646 * Some compilers support explicit branch prediction so that the CPU backend
647 * can hint the processor and also so that code blocks can be reordered such
648 * that the predicted path sees a more linear flow, thus improving cache
649 * behaviour, etc.
650 *
651 * IPRT provides the macros RT_LIKELY() and RT_UNLIKELY() as a way to utilize
652 * this compiler feature when present.
653 *
654 * A few notes about the usage:
655 *
656 * - Generally, use RT_UNLIKELY() with error condition checks (unless you
657 * have some _strong_ reason to do otherwise, in which case document it),
658 * and/or RT_LIKELY() with success condition checks, assuming you want
659 * to optimize for the success path.
660 *
661 * - Other than that, if you don't know the likelihood of a test succeeding
662 * from empirical or other 'hard' evidence, don't make predictions unless
663 * you happen to be a Dirk Gently.
664 *
665 * - These macros are meant to be used in places that get executed a lot. It
666 * is wasteful to make predictions in code that is executed seldomly (e.g.
667 * at subsystem initialization time) as the basic block reording that this
668 * affecs can often generate larger code.
669 *
670 * - Note that RT_SUCCESS() and RT_FAILURE() already makes use of RT_LIKELY()
671 * and RT_UNLIKELY(). Should you wish for prediction free status checks,
672 * use the RT_SUCCESS_NP() and RT_FAILURE_NP() macros instead.
673 *
674 *
675 * @returns the boolean result of the expression.
676 * @param expr The expression that's very likely to be true.
677 * @see RT_UNLIKELY
678 */
679/** @def RT_UNLIKELY
680 * Give the compiler a hint that an expression is highly unlikely hold true.
681 *
682 * See the usage instructions give in the RT_LIKELY() docs.
683 *
684 * @returns the boolean result of the expression.
685 * @param expr The expression that's very unlikely to be true.
686 * @see RT_LIKELY
687 */
688#if defined(__GNUC__)
689# if __GNUC__ >= 3
690# define RT_LIKELY(expr) __builtin_expect(!!(expr), 1)
691# define RT_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
692# else
693# define RT_LIKELY(expr) (expr)
694# define RT_UNLIKELY(expr) (expr)
695# endif
696#else
697# define RT_LIKELY(expr) (expr)
698# define RT_UNLIKELY(expr) (expr)
699#endif
700
701
702/** @def RT_BIT
703 * Make a bitmask for one integer sized bit.
704 * @param bit Bit number.
705 */
706#define RT_BIT(bit) (1U << (bit))
707
708/** @def RT_BIT_32
709 * Make a 32-bit bitmask for one bit.
710 * @param bit Bit number.
711 */
712#define RT_BIT_32(bit) (UINT32_C(1) << (bit))
713
714/** @def RT_BIT_64
715 * Make a 64-bit bitmask for one bit.
716 * @param bit Bit number.
717 */
718#define RT_BIT_64(bit) (UINT64_C(1) << (bit))
719
720/** @def RT_ALIGN
721 * Align macro.
722 * @param u Value to align.
723 * @param uAlignment The alignment. Power of two!
724 *
725 * @remark Be extremely careful when using this macro with type which sizeof != sizeof int.
726 * When possible use any of the other RT_ALIGN_* macros. And when that's not
727 * possible, make 101% sure that uAlignment is specified with a right sized type.
728 *
729 * Specifying an unsigned 32-bit alignment constant with a 64-bit value will give
730 * you a 32-bit return value!
731 *
732 * In short: Don't use this macro. Use RT_ALIGN_T() instead.
733 */
734#define RT_ALIGN(u, uAlignment) ( ((u) + ((uAlignment) - 1)) & ~((uAlignment) - 1) )
735
736/** @def RT_ALIGN_T
737 * Align macro.
738 * @param u Value to align.
739 * @param uAlignment The alignment. Power of two!
740 * @param type Integer type to use while aligning.
741 * @remark This macro is the prefered alignment macro, it doesn't have any of the pitfalls RT_ALIGN has.
742 */
743#define RT_ALIGN_T(u, uAlignment, type) ( ((type)(u) + ((uAlignment) - 1)) & ~(type)((uAlignment) - 1) )
744
745/** @def RT_ALIGN_32
746 * Align macro for a 32-bit value.
747 * @param u32 Value to align.
748 * @param uAlignment The alignment. Power of two!
749 */
750#define RT_ALIGN_32(u32, uAlignment) RT_ALIGN_T(u32, uAlignment, uint32_t)
751
752/** @def RT_ALIGN_64
753 * Align macro for a 64-bit value.
754 * @param u64 Value to align.
755 * @param uAlignment The alignment. Power of two!
756 */
757#define RT_ALIGN_64(u64, uAlignment) RT_ALIGN_T(u64, uAlignment, uint64_t)
758
759/** @def RT_ALIGN_Z
760 * Align macro for size_t.
761 * @param cb Value to align.
762 * @param uAlignment The alignment. Power of two!
763 */
764#define RT_ALIGN_Z(cb, uAlignment) RT_ALIGN_T(cb, uAlignment, size_t)
765
766/** @def RT_ALIGN_P
767 * Align macro for pointers.
768 * @param pv Value to align.
769 * @param uAlignment The alignment. Power of two!
770 */
771#define RT_ALIGN_P(pv, uAlignment) RT_ALIGN_PT(pv, uAlignment, void *)
772
773/** @def RT_ALIGN_PT
774 * Align macro for pointers with type cast.
775 * @param u Value to align.
776 * @param uAlignment The alignment. Power of two!
777 * @param CastType The type to cast the result to.
778 */
779#define RT_ALIGN_PT(u, uAlignment, CastType) ((CastType)RT_ALIGN_T(u, uAlignment, uintptr_t))
780
781/** @def RT_ALIGN_R3PT
782 * Align macro for ring-3 pointers with type cast.
783 * @param u Value to align.
784 * @param uAlignment The alignment. Power of two!
785 * @param CastType The type to cast the result to.
786 */
787#define RT_ALIGN_R3PT(u, uAlignment, CastType) ((CastType)RT_ALIGN_T(u, uAlignment, RTR3UINTPTR))
788
789/** @def RT_ALIGN_R0PT
790 * Align macro for ring-0 pointers with type cast.
791 * @param u Value to align.
792 * @param uAlignment The alignment. Power of two!
793 * @param CastType The type to cast the result to.
794 */
795#define RT_ALIGN_R0PT(u, uAlignment, CastType) ((CastType)RT_ALIGN_T(u, uAlignment, RTR0UINTPTR))
796
797/** @def RT_ALIGN_GCPT
798 * Align macro for GC pointers with type cast.
799 * @param u Value to align.
800 * @param uAlignment The alignment. Power of two!
801 * @param CastType The type to cast the result to.
802 */
803#define RT_ALIGN_GCPT(u, uAlignment, CastType) ((CastType)RT_ALIGN_T(u, uAlignment, RTGCUINTPTR))
804
805
806/** @def RT_OFFSETOF
807 * Our own special offsetof() variant, returns a signed result.
808 *
809 * This differs from the usual offsetof() in that it's not relying on builtin
810 * compiler stuff and thus can use variables in arrays the structure may
811 * contain. If in this usful to determin the sizes of structures ending
812 * with a variable length field.
813 *
814 * @returns offset into the structure of the specified member. signed.
815 * @param type Structure type.
816 * @param member Member.
817 */
818#define RT_OFFSETOF(type, member) ( (int)(uintptr_t)&( ((type *)(void *)0)->member) )
819
820/** @def RT_UOFFSETOF
821 * Our own special offsetof() variant, returns an unsigned result.
822 *
823 * This differs from the usual offsetof() in that it's not relying on builtin
824 * compiler stuff and thus can use variables in arrays the structure may
825 * contain. If in this usful to determin the sizes of structures ending
826 * with a variable length field.
827 *
828 * @returns offset into the structure of the specified member. unsigned.
829 * @param type Structure type.
830 * @param member Member.
831 */
832#define RT_UOFFSETOF(type, member) ( (uintptr_t)&( ((type *)(void *)0)->member) )
833
834/** @def RT_SIZEOFMEMB
835 * Get the size of a structure member.
836 *
837 * @returns size of the structure member.
838 * @param type Structure type.
839 * @param member Member.
840 */
841#define RT_SIZEOFMEMB(type, member) ( sizeof(((type *)(void *)0)->member) )
842
843/** @def RT_ELEMENTS
844 * Calcs the number of elements in an array.
845 * @returns Element count.
846 * @param aArray Array in question.
847 */
848#define RT_ELEMENTS(aArray) ( sizeof(aArray) / sizeof((aArray)[0]) )
849
850#ifdef RT_OS_OS2
851/* Undefine RT_MAX since there is an unfortunate clash with the max
852 resource type define in os2.h. */
853# undef RT_MAX
854#endif
855
856/** @def RT_MAX
857 * Finds the maximum value.
858 * @returns The higher of the two.
859 * @param Value1 Value 1
860 * @param Value2 Value 2
861 */
862#define RT_MAX(Value1, Value2) ((Value1) >= (Value2) ? (Value1) : (Value2))
863
864/** @def RT_MIN
865 * Finds the minimum value.
866 * @returns The lower of the two.
867 * @param Value1 Value 1
868 * @param Value2 Value 2
869 */
870#define RT_MIN(Value1, Value2) ((Value1) <= (Value2) ? (Value1) : (Value2))
871
872/** @def RT_ABS
873 * Get the absolute (non-negative) value.
874 * @returns The absolute value of Value.
875 * @param Value The value.
876 */
877#define RT_ABS(Value) ((Value) >= 0 ? (Value) : -(Value))
878
879/** @def RT_LOWORD
880 * Gets the low word (=uint16_t) of something. */
881#define RT_LOWORD(a) ((a) & 0xffff)
882
883/** @def RT_HIWORD
884 * Gets the high word (=uint16_t) of a 32 bit something. */
885#define RT_HIWORD(a) ((a) >> 16)
886
887/** @def RT_LOBYTE
888 * Gets the low byte of something. */
889#define RT_LOBYTE(a) ((a) & 0xff)
890
891/** @def RT_HIBYTE
892 * Gets the low byte of a 16 bit something. */
893#define RT_HIBYTE(a) ((a) >> 8)
894
895/** @def RT_BYTE1
896 * Gets first byte of something. */
897#define RT_BYTE1(a) ((a) & 0xff)
898
899/** @def RT_BYTE2
900 * Gets second byte of something. */
901#define RT_BYTE2(a) (((a) >> 8) & 0xff)
902
903/** @def RT_BYTE3
904 * Gets second byte of something. */
905#define RT_BYTE3(a) (((a) >> 16) & 0xff)
906
907/** @def RT_BYTE4
908 * Gets fourth byte of something. */
909#define RT_BYTE4(a) (((a) >> 24) & 0xff)
910
911
912/** @def RT_MAKE_U64
913 * Constructs a uint64_t value from two uint32_t values.
914 */
915#define RT_MAKE_U64(Lo, Hi) ( (uint64_t)((uint32_t)(Hi)) << 32 | (uint32_t)(Lo) )
916
917/** @def RT_MAKE_U64_FROM_U16
918 * Constructs a uint64_t value from four uint16_t values.
919 */
920#define RT_MAKE_U64_FROM_U16(w0, w1, w2, w3) \
921 ( (uint64_t)((uint16_t)(w3)) << 48 \
922 | (uint64_t)((uint16_t)(w2)) << 32 \
923 | (uint32_t)((uint16_t)(w1)) << 16 \
924 | (uint16_t)(w0) )
925
926/** @def RT_MAKE_U64_FROM_U8
927 * Constructs a uint64_t value from eight uint8_t values.
928 */
929#define RT_MAKE_U64_FROM_U8(b0, b1, b2, b3, b4, b5, b6, b7) \
930 ( (uint64_t)((uint8_t)(b7)) << 56 \
931 | (uint64_t)((uint8_t)(b6)) << 48 \
932 | (uint64_t)((uint8_t)(b5)) << 40 \
933 | (uint64_t)((uint8_t)(b4)) << 32 \
934 | (uint32_t)((uint8_t)(b3)) << 24 \
935 | (uint32_t)((uint8_t)(b2)) << 16 \
936 | (uint16_t)((uint8_t)(b1)) << 8 \
937 | (uint8_t)(b0) )
938
939/** @def RT_MAKE_U32
940 * Constructs a uint32_t value from two uint16_t values.
941 */
942#define RT_MAKE_U32(Lo, Hi) ( (uint32_t)((uint16_t)(Hi)) << 16 | (uint16_t)(Lo) )
943
944/** @def RT_MAKE_U32_FROM_U8
945 * Constructs a uint32_t value from four uint8_t values.
946 */
947#define RT_MAKE_U32_FROM_U8(b0, b1, b2, b3) \
948 ( (uint32_t)((uint8_t)(b3)) << 24 \
949 | (uint32_t)((uint8_t)(b2)) << 16 \
950 | (uint16_t)((uint8_t)(b1)) << 8 \
951 | (uint8_t)(b0) )
952/** @todo remove this after uses in VUSBUrb.cpp has been corrected. */
953#define MAKE_U32_FROM_U8(b0,b1,b2,b3) RT_MAKE_U32_FROM_U8(b0,b1,b2,b3)
954
955/** @def RT_MAKE_U16
956 * Constructs a uint32_t value from two uint16_t values.
957 */
958#define RT_MAKE_U16(Lo, Hi) ( (uint16_t)((uint8_t)(Hi)) << 8 | (uint8_t)(Lo) )
959
960
961/** @def RT_H2LE_U64
962 * Converts uint64_t value from host to little endian byte order. */
963#define RT_H2LE_U64(u64) (u64)
964
965/** @def RT_H2LE_U32
966 * Converts uint32_t value from host to little endian byte order. */
967#define RT_H2LE_U32(u32) (u32)
968
969/** @def RT_H2LE_U16
970 * Converts uint16_t value from host to little endian byte order. */
971#define RT_H2LE_U16(u16) (u16)
972
973/** @def RT_LE2H_U64
974 * Converts uint64_t value from little endian to host byte order. */
975#define RT_LE2H_U64(u64) (u64)
976
977/** @def RT_LE2H_U32
978 * Converts uint32_t value from little endian to host byte order. */
979#define RT_LE2H_U32(u32) (u32)
980
981/** @def RT_LE2H_U16
982 * Converts uint16_t value from little endian to host byte order. */
983#define RT_LE2H_U16(u16) (u16)
984
985
986/** @def RT_H2BE_U64
987 * Converts uint64_t value from host to big endian byte order. */
988#define RT_H2BE_U64(u64) RT_MAKE_U64_FROM_U32(RT_H2BE_U32((u64) >> 32), RT_H2BE_U32((u64) & 0xffffffff))
989
990/** @def RT_H2BE_U32
991 * Converts uint32_t value from host to big endian byte order. */
992#define RT_H2BE_U32(u32) (RT_BYTE4(u32) | (RT_BYTE3(u32) << 8) | (RT_BYTE2(u32) << 16) | (RT_BYTE1(u32) << 24))
993
994/** @def RT_H2BE_U16
995 * Converts uint16_t value from host to big endian byte order. */
996#define RT_H2BE_U16(u16) (RT_HIBYTE(u16) | (RT_LOBYTE(u16) << 8))
997
998/** @def RT_BE2H_U64
999 * Converts uint64_t value from big endian to host byte order. */
1000#define RT_BE2H_U64(u64) RT_MAKE_U64_FROM_U32(RT_H2BE_U32((u64) >> 32), RT_H2BE_U32((u64) & 0xffffffff))
1001
1002/** @def RT_BE2H_U32
1003 * Converts uint32_t value from big endian to host byte order. */
1004#define RT_BE2H_U32(u32) (RT_BYTE4(u32) | (RT_BYTE3(u32) << 8) | (RT_BYTE2(u32) << 16) | (RT_BYTE1(u32) << 24))
1005
1006/** @def RT_BE2H_U16
1007 * Converts uint16_t value from big endian to host byte order. */
1008#define RT_BE2H_U16(u16) (RT_HIBYTE(u16) | (RT_LOBYTE(u16) << 8))
1009
1010
1011/** @def RT_H2N_U32
1012 * Converts uint32_t value from host to network byte order. */
1013#define RT_H2N_U32(u32) RT_H2BE_U32(u32)
1014
1015/** @def RT_H2N_U16
1016 * Converts uint16_t value from host to network byte order. */
1017#define RT_H2N_U16(u16) RT_H2BE_U16(u16)
1018
1019/** @def RT_N2H_U32
1020 * Converts uint32_t value from network to host byte order. */
1021#define RT_N2H_U32(u32) RT_BE2H_U32(u32)
1022
1023/** @def RT_N2H_U16
1024 * Converts uint16_t value from network to host byte order. */
1025#define RT_N2H_U16(u16) RT_BE2H_U16(u16)
1026
1027
1028/** @def RT_NO_DEPRECATED_MACROS
1029 * Define RT_NO_DEPRECATED_MACROS to not define deprecated macros.
1030 */
1031#ifndef RT_NO_DEPRECATED_MACROS
1032/** @copydoc RT_ALIGN_P
1033 * @deprecated use RT_ALIGN_P. */
1034# define ALIGNP(pv, uAlignment) RT_ALIGN_P(pv, uAlignment)
1035/** @copydoc RT_SIZEOFMEMB
1036 * @deprecated Use RT_SIZEOFMEMB. */
1037# define SIZEOFMEMB(type, member) RT_SIZEOFMEMB(type, member)
1038/** @copydoc RT_ELEMENTS
1039 * @deprecated use RT_ELEMENTS. */
1040# define ELEMENTS(aArray) RT_ELEMENTS(aArray)
1041#endif
1042
1043
1044/*
1045 * The BSD sys/param.h + machine/param.h file is a major source of
1046 * namespace pollution. Kill off some of the worse ones unless we're
1047 * compiling kernel code.
1048 */
1049#if defined(RT_OS_DARWIN) \
1050 && !defined(KERNEL) \
1051 && !defined(RT_NO_BSD_PARAM_H_UNDEFING) \
1052 && ( defined(_SYS_PARAM_H_) || defined(_I386_PARAM_H_) )
1053/* sys/param.h: */
1054# undef PSWP
1055# undef PVM
1056# undef PINOD
1057# undef PRIBO
1058# undef PVFS
1059# undef PZERO
1060# undef PSOCK
1061# undef PWAIT
1062# undef PLOCK
1063# undef PPAUSE
1064# undef PUSER
1065# undef PRIMASK
1066# undef MINBUCKET
1067# undef MAXALLOCSAVE
1068# undef FSHIFT
1069# undef FSCALE
1070
1071/* i386/machine.h: */
1072# undef ALIGN
1073# undef ALIGNBYTES
1074# undef DELAY
1075# undef STATUS_WORD
1076# undef USERMODE
1077# undef BASEPRI
1078# undef MSIZE
1079# undef CLSIZE
1080# undef CLSIZELOG2
1081#endif
1082
1083
1084/** @def NULL
1085 * NULL pointer.
1086 */
1087#ifndef NULL
1088# ifdef __cplusplus
1089# define NULL 0
1090# else
1091# define NULL ((void*)0)
1092# endif
1093#endif
1094
1095/** @def NIL_OFFSET
1096 * NIL offset.
1097 * Whenever we use offsets instead of pointers to save space and relocation effort
1098 * NIL_OFFSET shall be used as the equivalent to NULL.
1099 */
1100#define NIL_OFFSET (~0U)
1101
1102/** @def NOREF
1103 * Keeps the compiler from bitching about an unused parameters.
1104 */
1105#define NOREF(var) (void)(var)
1106
1107/** @def Breakpoint
1108 * Emit a debug breakpoint instruction.
1109 *
1110 * Use this for instrumenting a debugging session only!
1111 * No comitted code shall use Breakpoint().
1112 */
1113#ifdef __GNUC__
1114# define Breakpoint() __asm__ __volatile__("int $3\n\t")
1115#endif
1116#ifdef _MSC_VER
1117# define Breakpoint() __asm int 3
1118#endif
1119#if defined(__IBMC__) || defined(__IBMCPP__)
1120# define Breakpoint() __interrupt(3)
1121#endif
1122#ifndef Breakpoint
1123# error "This compiler is not supported!"
1124#endif
1125
1126
1127/** Size Constants
1128 * (Of course, these are binary computer terms, not SI.)
1129 * @{
1130 */
1131/** 1 K (Kilo) (1 024). */
1132#define _1K 0x00000400
1133/** 4 K (Kilo) (4 096). */
1134#define _4K 0x00001000
1135/** 32 K (Kilo) (32 678). */
1136#define _32K 0x00008000
1137/** 64 K (Kilo) (65 536). */
1138#define _64K 0x00010000
1139/** 128 K (Kilo) (131 072). */
1140#define _128K 0x00020000
1141/** 256 K (Kilo) (262 144). */
1142#define _256K 0x00040000
1143/** 512 K (Kilo) (524 288). */
1144#define _512K 0x00080000
1145/** 1 M (Mega) (1 048 576). */
1146#define _1M 0x00100000
1147/** 2 M (Mega) (2 097 152). */
1148#define _2M 0x00200000
1149/** 4 M (Mega) (4 194 304). */
1150#define _4M 0x00400000
1151/** 1 G (Giga) (1 073 741 824). */
1152#define _1G 0x40000000
1153/** 2 G (Giga) (2 147 483 648). (32-bit) */
1154#define _2G32 0x80000000U
1155/** 2 G (Giga) (2 147 483 648). (64-bit) */
1156#define _2G 0x0000000080000000LL
1157/** 4 G (Giga) (4 294 967 296). */
1158#define _4G 0x0000000100000000LL
1159/** 1 T (Tera) (1 099 511 627 776). */
1160#define _1T 0x0000010000000000LL
1161/** 1 P (Peta) (1 125 899 906 842 624). */
1162#define _1P 0x0004000000000000LL
1163/** 1 E (Exa) (1 152 921 504 606 846 976). */
1164#define _1E 0x1000000000000000LL
1165/** 2 E (Exa) (2 305 843 009 213 693 952). */
1166#define _2E 0x2000000000000000ULL
1167/** @} */
1168
1169/** @def VALID_PTR
1170 * Pointer validation macro.
1171 * @param ptr
1172 */
1173#if defined(RT_ARCH_AMD64)
1174# ifdef IN_RING3
1175# if defined(RT_OS_DARWIN) /* first 4GB is reserved for legacy kernel. */
1176# define VALID_PTR(ptr) ( (uintptr_t)(ptr) >= _4G \
1177 && !((uintptr_t)(ptr) & 0xffff800000000000ULL) )
1178# elif defined(RT_OS_SOLARIS) /* The kernel only used the top 2TB, but keep it simple. */
1179# define VALID_PTR(ptr) ( (uintptr_t)(ptr) + 0x1000U >= 0x2000U \
1180 && ( ((uintptr_t)(ptr) & 0xffff800000000000ULL) == 0xffff800000000000ULL \
1181 || ((uintptr_t)(ptr) & 0xffff800000000000ULL) == 0) )
1182# else
1183# define VALID_PTR(ptr) ( (uintptr_t)(ptr) + 0x1000U >= 0x2000U \
1184 && !((uintptr_t)(ptr) & 0xffff800000000000ULL) )
1185# endif
1186# else /* !IN_RING3 */
1187# define VALID_PTR(ptr) ( (uintptr_t)(ptr) + 0x1000U >= 0x2000U \
1188 && ( ((uintptr_t)(ptr) & 0xffff800000000000ULL) == 0xffff800000000000ULL \
1189 || ((uintptr_t)(ptr) & 0xffff800000000000ULL) == 0) )
1190# endif /* !IN_RING3 */
1191#elif defined(RT_ARCH_X86)
1192# define VALID_PTR(ptr) ( (uintptr_t)(ptr) + 0x1000U >= 0x2000U )
1193#else
1194# error "Architecture identifier missing / not implemented."
1195#endif
1196
1197
1198/** @def N_
1199 * The \#define N_ is used mark a string for translation. This is usable in
1200 * any part of the code, as it is only used by the tools that create message
1201 * catalogs. This macro is a no-op as far as the compiler and code generation
1202 * is concerned.
1203 *
1204 * If you want to both mark a string for translation and translate it, use _.
1205 */
1206#define N_(s) (s)
1207
1208/** @def _
1209 * The \#define _ is used mark a string for translation and to translate it in
1210 * one step.
1211 *
1212 * If you want to only mark a string for translation, use N_.
1213 */
1214#define _(s) gettext(s)
1215
1216
1217/** @def __PRETTY_FUNCTION__
1218 * With GNU C we'd like to use the builtin __PRETTY_FUNCTION__, so define that for the other compilers.
1219 */
1220#if !defined(__GNUC__) && !defined(__PRETTY_FUNCTION__)
1221# define __PRETTY_FUNCTION__ __FUNCTION__
1222#endif
1223
1224
1225/** @def RT_STRICT
1226 * The \#define RT_STRICT controls whether or not assertions and other runtime checks
1227 * should be compiled in or not.
1228 *
1229 * If you want assertions which are not a subject to compile time options use
1230 * the AssertRelease*() flavors.
1231 */
1232#if !defined(RT_STRICT) && defined(DEBUG)
1233# define RT_STRICT
1234#endif
1235
1236/** Source position. */
1237#define RT_SRC_POS __FILE__, __LINE__, __PRETTY_FUNCTION__
1238
1239/** Source position declaration. */
1240#define RT_SRC_POS_DECL const char *pszFile, unsigned iLine, const char *pszFunction
1241
1242/** Source position arguments. */
1243#define RT_SRC_POS_ARGS pszFile, iLine, pszFunction
1244
1245/** @} */
1246
1247
1248/** @defgroup grp_rt_cdefs_cpp Special Macros for C++
1249 * @ingroup grp_rt_cdefs
1250 * @{
1251 */
1252
1253#ifdef __cplusplus
1254
1255/** @def WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP
1256 * Macro to work around error C2593 of the not-so-smart MSVC 7.x ambiguity
1257 * resolver. The following snippet clearly demonstrates the code causing this
1258 * error:
1259 * @code
1260 * class A
1261 * {
1262 * public:
1263 * operator bool() const { return false; }
1264 * operator int*() const { return NULL; }
1265 * };
1266 * int main()
1267 * {
1268 * A a;
1269 * if (!a);
1270 * if (a && 0);
1271 * return 0;
1272 * }
1273 * @endcode
1274 * The code itself seems pretty valid to me and GCC thinks the same.
1275 *
1276 * This macro fixes the compiler error by explicitly overloading implicit
1277 * global operators !, && and || that take the given class instance as one of
1278 * their arguments.
1279 *
1280 * The best is to use this macro right after the class declaration.
1281 *
1282 * @note The macro expands to nothing for compilers other than MSVC.
1283 *
1284 * @param Cls Class to apply the workaround to
1285 */
1286#if defined(_MSC_VER)
1287# define WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP(Cls) \
1288 inline bool operator! (const Cls &that) { return !bool (that); } \
1289 inline bool operator&& (const Cls &that, bool b) { return bool (that) && b; } \
1290 inline bool operator|| (const Cls &that, bool b) { return bool (that) || b; } \
1291 inline bool operator&& (bool b, const Cls &that) { return b && bool (that); } \
1292 inline bool operator|| (bool b, const Cls &that) { return b || bool (that); }
1293#else
1294# define WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP(Cls)
1295#endif
1296
1297/** @def WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP_TPL
1298 * Version of WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP for template classes.
1299 *
1300 * @param Tpl Name of the template class to apply the workaround to
1301 * @param ArgsDecl arguments of the template, as declared in |<>| after the
1302 * |template| keyword, including |<>|
1303 * @param Args arguments of the template, as specified in |<>| after the
1304 * template class name when using the, including |<>|
1305 *
1306 * Example:
1307 * @code
1308 * // template class declaration
1309 * template <class C>
1310 * class Foo { ... };
1311 * // applied workaround
1312 * WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP_TPL (Foo, <class C>, <C>)
1313 * @endcode
1314 */
1315#if defined(_MSC_VER)
1316# define WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP_TPL(Tpl, ArgsDecl, Args) \
1317 template ArgsDecl \
1318 inline bool operator! (const Tpl Args &that) { return !bool (that); } \
1319 template ArgsDecl \
1320 inline bool operator&& (const Tpl Args &that, bool b) { return bool (that) && b; } \
1321 template ArgsDecl \
1322 inline bool operator|| (const Tpl Args &that, bool b) { return bool (that) || b; } \
1323 template ArgsDecl \
1324 inline bool operator&& (bool b, const Tpl Args &that) { return b && bool (that); } \
1325 template ArgsDecl \
1326 inline bool operator|| (bool b, const Tpl Args &that) { return b || bool (that); }
1327#else
1328# define WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP_TPL(Tpl, ArgsDecl, Args)
1329#endif
1330
1331
1332/** @def DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP
1333 * Declares the copy constructor and the assignment operation as inlined no-ops
1334 * (non-existent functions) for the given class. Use this macro inside the
1335 * private section if you want to effectively disable these operations for your
1336 * class.
1337 *
1338 * @param Cls class name to declare for
1339 */
1340
1341#define DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(Cls) \
1342 inline Cls (const Cls &); \
1343 inline Cls &operator= (const Cls &);
1344
1345
1346/** @def DECLARE_CLS_NEW_DELETE_NOOP
1347 * Declares the new and delete operations as no-ops (non-existent functions)
1348 * for the given class. Use this macro inside the private section if you want
1349 * to effectively limit creating class instances on the stack only.
1350 *
1351 * @note The destructor of the given class must not be virtual, otherwise a
1352 * compile time error will occur. Note that this is not a drawback: having
1353 * the virtual destructor for a stack-based class is absolutely useless
1354 * (the real class of the stack-based instance is always known to the compiler
1355 * at compile time, so it will always call the correct destructor).
1356 *
1357 * @param Cls class name to declare for
1358 */
1359#define DECLARE_CLS_NEW_DELETE_NOOP(Cls) \
1360 inline static void *operator new (size_t); \
1361 inline static void operator delete (void *);
1362
1363#endif /* defined(__cplusplus) */
1364
1365/** @} */
1366
1367#endif
1368
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette