|
| 1 | +/* |
| 2 | + * nut_bool.h - Network UPS Tools boolean type definitions |
| 3 | + * which should ensure a "nut_bool_t" name with |
| 4 | + * lower-case values "true" and "false" |
| 5 | + * |
| 6 | + * Inspired by earlier efforts and numerous definitions in NUT codebase. |
| 7 | + * Copyright (C) 2024 Jim Klimov <[email protected]> |
| 8 | + * |
| 9 | + * This program is free software; you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU General Public License as published by |
| 11 | + * the Free Software Foundation; either version 2 of the License, or |
| 12 | + * (at your option) any later version. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public License |
| 20 | + * along with this program; if not, write to the Free Software |
| 21 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | + */ |
| 23 | + |
| 24 | +#ifndef NUT_BOOL_H_SEEN |
| 25 | +#define NUT_BOOL_H_SEEN 1 |
| 26 | + |
| 27 | +#ifdef __cplusplus |
| 28 | +/* *INDENT-OFF* */ |
| 29 | +extern "C" { |
| 30 | +/* *INDENT-ON* */ |
| 31 | +#endif |
| 32 | + |
| 33 | +/* "config.h" is generated by autotools and lacks a header guard, so |
| 34 | + * we use an unambiguously named macro we know we must have, as one. |
| 35 | + * It must be the first header: be sure to know all about system config. |
| 36 | + */ |
| 37 | +#ifndef NUT_NETVERSION |
| 38 | +# include "config.h" |
| 39 | +#endif |
| 40 | + |
| 41 | +/* See also https://en.cppreference.com/w/cpp/header/cstdbool for more |
| 42 | + * info about what should be available where per standard approach. */ |
| 43 | +#ifdef __cplusplus |
| 44 | +# if defined HAVE_CSTDBOOL_H || defined HAVE_CSTDBOOL |
| 45 | +# include <cstdbool> |
| 46 | +# else |
| 47 | +# ifdef HAVE_STDBOOL_H |
| 48 | +# include <stdbool.h> |
| 49 | +# endif |
| 50 | +# endif |
| 51 | +#else |
| 52 | +/* plain C */ |
| 53 | +# ifdef HAVE_STDBOOL_H |
| 54 | +# include <stdbool.h> |
| 55 | +# endif |
| 56 | +#endif |
| 57 | + |
| 58 | +/* Is the goal achieved by the system headers or compiler itself, |
| 59 | + * so we can just alias to existing type and its values? */ |
| 60 | +#if defined __bool_true_false_are_defined && __bool_true_false_are_defined |
| 61 | + typedef bool nut_bool_t; |
| 62 | +#elif defined FOUND__BOOL_TYPE && defined HAVE__BOOL_VALUE_LOWERCASE |
| 63 | + typedef FOUND__BOOL_TYPE nut_bool_t; |
| 64 | +#elif defined FOUND_BOOL_TYPE && defined HAVE_BOOL_VALUE_LOWERCASE |
| 65 | + typedef FOUND_BOOL_TYPE nut_bool_t; |
| 66 | +#elif defined FOUND_BOOLEAN_TYPE && defined HAVE_BOOLEAN_VALUE_LOWERCASE |
| 67 | + typedef FOUND_BOOLEAN_TYPE nut_bool_t; |
| 68 | +#elif defined FOUND_BOOL_T_TYPE && defined HAVE_BOOL_T_VALUE_LOWERCASE |
| 69 | + typedef FOUND_BOOL_T_TYPE nut_bool_t; |
| 70 | +#else |
| 71 | + /* Need a new type; can we use an enum with lower-case values? */ |
| 72 | +# if (defined true && defined false) || defined HAVE__BOOL_VALUE_LOWERCASE || defined HAVE_BOOL_VALUE_LOWERCASE || defined HAVE_BOOLEAN_VALUE_LOWERCASE || defined HAVE_BOOL_T_VALUE_LOWERCASE |
| 73 | + /* Lower-case true/false are known */ |
| 74 | +# if defined FOUND__BOOL_TYPE |
| 75 | + /* Got a C99 built-in mandated by the standard */ |
| 76 | + typedef FOUND__BOOL_TYPE nut_bool_t; |
| 77 | +# else |
| 78 | + typedef int nut_bool_t; |
| 79 | +# endif |
| 80 | +# elif defined FOUND__BOOL_VALUE_TRUE && defined FOUND__BOOL_VALUE_FALSE |
| 81 | + typedef enum nut_bool_enum { false = FOUND__BOOL_VALUE_FALSE, true = FOUND__BOOL_VALUE_TRUE } nut_bool_t; |
| 82 | +# elif defined FOUND_BOOL_VALUE_TRUE && defined FOUND_BOOL_VALUE_FALSE |
| 83 | + typedef enum nut_bool_enum { false = FOUND_BOOL_VALUE_FALSE, true = FOUND_BOOL_VALUE_TRUE } nut_bool_t; |
| 84 | +# elif defined FOUND_BOOLEAN_VALUE_TRUE && defined FOUND_BOOLEAN_VALUE_FALSE |
| 85 | + typedef enum nut_bool_enum { false = FOUND_BOOLEAN_VALUE_FALSE, true = FOUND_BOOLEAN_VALUE_TRUE } nut_bool_t; |
| 86 | +# elif defined FOUND_BOOL_T_VALUE_TRUE && defined FOUND_BOOL_T_VALUE_FALSE |
| 87 | + typedef enum nut_bool_enum { false = FOUND_BOOL_T_VALUE_FALSE, true = FOUND_BOOL_T_VALUE_TRUE } nut_bool_t; |
| 88 | +# else |
| 89 | + typedef enum nut_bool_enum { false = 0, true = 1 } nut_bool_t; |
| 90 | +# endif |
| 91 | +#endif |
| 92 | + |
| 93 | +#ifdef __cplusplus |
| 94 | +/* *INDENT-OFF* */ |
| 95 | +} |
| 96 | +/* *INDENT-ON* */ |
| 97 | +#endif |
| 98 | + |
| 99 | +#endif /* NUT_BOOL_H_SEEN */ |
0 commit comments