stdbool.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /****************************************************************************
  2. * include/stdbool.h
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. #ifndef __INCLUDE_STDBOOL_H
  21. #define __INCLUDE_STDBOOL_H
  22. /****************************************************************************
  23. * Included Files
  24. ****************************************************************************/
  25. #include <nuttx/config.h>
  26. /* If CONFIG_ARCH_STDBOOL_H is set, then the architecture will provide its
  27. * own stdbool.h file. In this case, this header file will simply re-direct
  28. * to the architecture-specfic stdbool.h header file.
  29. */
  30. #ifdef CONFIG_ARCH_STDBOOL_H
  31. # include <arch/stdbool.h>
  32. /* NuttX will insist that the sizeof(bool) is 8-bits. The sizeof of _Bool
  33. * used by any specific compiler is implementation specific: It can vary from
  34. * compiler-to-compiler and even vary between different versions of the same
  35. * compiler. Compilers seems to be converging to sizeof(_Bool) == 1. If that
  36. * is true for your compiler, you should define CONFIG_C99_BOOL8 in your
  37. * NuttX configuration for better standards compatibility.
  38. *
  39. * CONFIG_C99_BOOL8 - Means (1) your C++ compiler has sizeof(_Bool) == 8,
  40. * (2) your C compiler supports the C99 _Bool intrinsic type, and (3) that
  41. * the C99 _Bool type also has size 1.
  42. */
  43. #else
  44. /* nuttx/compiler.h may also define or undefine CONFIG_C99_BOOL8 */
  45. # include <nuttx/compiler.h>
  46. # include <stdint.h>
  47. /****************************************************************************
  48. * Pre-processor Definitions
  49. ****************************************************************************/
  50. /* bool, true, and false must be provided as macros so that they can be
  51. * redefined by the application if necessary.
  52. *
  53. * NOTE: Under C99 'bool' is required to be defined to be the intrinsic type
  54. * _Bool. However, in this NuttX context, we need backward compatibility
  55. * to pre-C99 standards where _Bool is not an intrinsic type. Hence, we
  56. * use _Bool8 as the underlying type (unless CONFIG_C99_BOOL8 is defined)
  57. */
  58. #ifndef __cplusplus
  59. #ifdef CONFIG_C99_BOOL8
  60. # define bool _Bool
  61. #else
  62. # define bool _Bool8
  63. #endif
  64. #define true (bool)1
  65. #define false (bool)0
  66. #define __bool_true_false_are_defined 1
  67. #endif /* __cplusplus */
  68. /****************************************************************************
  69. * Public Types
  70. ****************************************************************************/
  71. /* A byte is the smallest address memory element (at least in architectures
  72. * that do not support bit banding). The requirement is only that type _Bool
  73. * be large enough to hold the values 0 and 1. We select uint8_t to minimize
  74. * the RAM footprint of the executable.
  75. *
  76. * NOTE: We can't actually define the type _Bool here. Under C99 _Bool is
  77. * an intrinsic type and cannot be the target of a typedef. However, in this
  78. * NuttX context, we also need backward compatibility to pre-C99 standards
  79. * where _Bool is not an intrinsic type. We work around this by using _Bool8
  80. * as the underlying type.
  81. */
  82. #ifndef CONFIG_C99_BOOL8
  83. typedef uint8_t _Bool8;
  84. #endif
  85. #endif /* CONFIG_ARCH_STDBOOL_H */
  86. #endif /* __INCLUDE_STDBOOL_H */