stdbool_.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Copyright (C) 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
  2. Written by Bruno Haible <haible@clisp.cons.org>, 2001.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. #ifndef _STDBOOL_H
  15. #define _STDBOOL_H
  16. /* ISO C 99 <stdbool.h> for platforms that lack it. */
  17. /* Usage suggestions:
  18. Programs that use <stdbool.h> should be aware of some limitations
  19. and standards compliance issues.
  20. Standards compliance:
  21. - <stdbool.h> must be #included before 'bool', 'false', 'true'
  22. can be used.
  23. - You cannot assume that sizeof (bool) == 1.
  24. - Programs should not undefine the macros bool, true, and false,
  25. as C99 lists that as an "obsolescent feature".
  26. Limitations of this substitute, when used in a C89 environment:
  27. - <stdbool.h> must be #included before the '_Bool' type can be used.
  28. - You cannot assume that _Bool is a typedef; it might be a macro.
  29. - In C99, casts and automatic conversions to '_Bool' or 'bool' are
  30. performed in such a way that every nonzero value gets converted
  31. to 'true', and zero gets converted to 'false'. This doesn't work
  32. with this substitute. With this substitute, only the values 0 and 1
  33. give the expected result when converted to _Bool' or 'bool'.
  34. Also, it is suggested that programs use 'bool' rather than '_Bool';
  35. this isn't required, but 'bool' is more common. */
  36. /* 7.16. Boolean type and values */
  37. /* BeOS <sys/socket.h> already #defines false 0, true 1. We use the same
  38. definitions below, which is OK. */
  39. #ifdef __BEOS__
  40. # include <OS.h> /* defines bool but not _Bool */
  41. #endif
  42. /* C++ and BeOS have a reliable bool (and _Bool, if it exists).
  43. Otherwise, since this file is being compiled, the system
  44. <stdbool.h> is not reliable so assume that the system _Bool is not
  45. reliable either. Under that assumption, it is tempting to write
  46. typedef enum { false, true } _Bool;
  47. so that gdb prints values of type 'bool' symbolically. But if we do
  48. this, values of type '_Bool' may promote to 'int' or 'unsigned int'
  49. (see ISO C 99 6.7.2.2.(4)); however, '_Bool' must promote to 'int'
  50. (see ISO C 99 6.3.1.1.(2)). We could instead try this:
  51. typedef enum { _Bool_dummy = -1, false, true } _Bool;
  52. as the negative value ensures that '_Bool' promotes to 'int'.
  53. However, this runs into some other problems. First, Sun's C
  54. compiler when (__SUNPRO_C < 0x550 || __STDC__ == 1) issues a stupid
  55. "warning: _Bool is a keyword in ISO C99". Second, IBM's AIX cc
  56. compiler 6.0.0.0 (and presumably other versions) mishandles
  57. subscripts involving _Bool (effectively, _Bool promotes to unsigned
  58. int in this case), and we need to redefine _Bool in that case.
  59. Third, HP-UX 10.20's C compiler lacks <stdbool.h> but has _Bool and
  60. mishandles comparisons of _Bool to int (it promotes _Bool to
  61. unsigned int).
  62. The simplest way to work around these problems is to ignore any
  63. existing definition of _Bool and use our own. */
  64. #if defined __cplusplus || defined __BEOS__
  65. # if !@HAVE__BOOL@
  66. typedef bool _Bool;
  67. # endif
  68. #else
  69. # define _Bool signed char
  70. #endif
  71. #define bool _Bool
  72. /* The other macros must be usable in preprocessor directives. */
  73. #define false 0
  74. #define true 1
  75. #define __bool_true_false_are_defined 1
  76. #endif /* _STDBOOL_H */