Make `sockets.test' more robust.
[bpt/guile.git] / libguile / boolean.h
CommitLineData
0f2d19dd
JB
1/* classes: h_files */
2
0527e687
DH
3#ifndef SCM_BOOLEAN_H
4#define SCM_BOOLEAN_H
5
45f4cbdf 6/* Copyright (C) 1995,1996,2000, 2006, 2008, 2009 Free Software Foundation, Inc.
0527e687 7 *
73be1d9e 8 * This library is free software; you can redistribute it and/or
53befeb7
NJ
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
0527e687 12 *
53befeb7
NJ
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
0527e687 17 *
73be1d9e
MV
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
53befeb7
NJ
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301 USA
73be1d9e 22 */
0527e687 23
0f2d19dd 24\f
0527e687 25
b4309c3c 26#include "libguile/__scm.h"
0f2d19dd
JB
27
28\f
29
30/* Boolean Values
31 *
32 */
0f2d19dd 33
45f4cbdf
MW
34/*
35 * Use these macros if it's important (for correctness)
36 * that %nil MUST be considered true
37 */
38#define scm_is_false_and_not_nil(x) (scm_is_eq ((x), SCM_BOOL_F))
39#define scm_is_true_or_nil(x) (!scm_is_eq ((x), SCM_BOOL_F))
40
41/*
42 * Use these macros if %nil will never be tested,
43 * for increased efficiency.
44 */
45#define scm_is_false_assume_not_nil(x) (scm_is_eq ((x), SCM_BOOL_F))
46#define scm_is_true_assume_not_nil(x) (!scm_is_eq ((x), SCM_BOOL_F))
47
48/*
49 * See the comments preceeding the definitions of SCM_BOOL_F and
50 * SCM_MATCHES_BITS_IN_COMMON in tags.h for more information on
51 * how the following macro works.
52 */
53#if SCM_ENABLE_ELISP
54# define scm_is_false_or_nil(x) \
55 (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_ELISP_NIL, SCM_BOOL_F))
56#else
57# define scm_is_false_or_nil(x) (scm_is_false_assume_not_nil (x))
58#endif
59#define scm_is_true_and_not_nil(x) (!scm_is_false_or_nil (x))
0f2d19dd 60
45f4cbdf
MW
61/* XXX Should these macros treat %nil as false by default? */
62#define scm_is_false(x) (scm_is_false_and_not_nil (x))
63#define scm_is_true(x) (!scm_is_false (x))
64
65/*
66 * Since we know SCM_BOOL_F and SCM_BOOL_T differ by exactly one bit,
67 * and that SCM_BOOL_F and SCM_ELISP_NIL differ by exactly one bit,
68 * and that they of course can't be the same bit (or else SCM_BOOL_T
69 * and SCM_ELISP_NIL be would equal), it follows that SCM_BOOL_T and
70 * SCM_ELISP_NIL differ by exactly two bits, and these are the bits
71 * which will be ignored by SCM_MATCHES_BITS_IN_COMMON below.
72 *
73 * See the comments preceeding the definitions of SCM_BOOL_F and
74 * SCM_MATCHES_BITS_IN_COMMON in tags.h for more information.
75 *
76 * If SCM_ENABLE_ELISP is true, then scm_is_bool_or_nil(x)
77 * returns 1 if and only if x is one of the following: SCM_BOOL_F,
78 * SCM_BOOL_T, SCM_ELISP_NIL, or SCM_XXX_ANOTHER_BOOLEAN_DONT_USE.
79 * Otherwise, it returns 0.
80 */
81#if SCM_ENABLE_ELISP
82# define scm_is_bool_or_nil(x) \
83 (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_BOOL_T, SCM_ELISP_NIL))
84#else
85# define scm_is_bool_or_nil(x) (scm_is_bool_and_not_nil (x))
86#endif
87
88#define scm_is_bool_and_not_nil(x) \
89 (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_BOOL_F, SCM_BOOL_T))
90
91/* XXX Should scm_is_bool treat %nil as a boolean? */
92#define scm_is_bool(x) (scm_is_bool_and_not_nil (x))
3a684cc6 93
ede310d8 94#define scm_from_bool(x) ((x) ? SCM_BOOL_T : SCM_BOOL_F)
3a684cc6
MV
95SCM_API int scm_to_bool (SCM x);
96
0f2d19dd
JB
97\f
98
45f4cbdf
MW
99/*
100 * The following macros efficiently implement boolean truth testing as
101 * expected by most lisps, which treat '() aka SCM_EOL as false.
102 *
103 * Since we know SCM_ELISP_NIL and SCM_BOOL_F differ by exactly one
104 * bit, and that SCM_ELISP_NIL and SCM_EOL differ by exactly one bit,
105 * and that they of course can't be the same bit (or else SCM_BOOL_F
106 * and SCM_EOL be would equal), it follows that SCM_BOOL_F and SCM_EOL
107 * differ by exactly two bits, and these are the bits which will be
108 * ignored by SCM_MATCHES_BITS_IN_COMMON below.
109 *
110 * See the comments preceeding the definitions of SCM_BOOL_F and
111 * SCM_MATCHES_BITS_IN_COMMON in tags.h for more information.
112 *
113 * scm_is_lisp_false(x) returns 1 if and only if x is one of the
114 * following: SCM_BOOL_F, SCM_ELISP_NIL, SCM_EOL or
115 * SCM_XXX_ANOTHER_LISP_FALSE_DONT_USE. Otherwise, it returns 0.
116 */
117#if SCM_ENABLE_ELISP
118# define scm_is_lisp_false(x) \
119 (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_BOOL_F, SCM_EOL))
120# define scm_is_lisp_true(x) (!scm_is_lisp_false(x))
121#endif
122
123\f
124
e4e93373
MV
125SCM_API SCM scm_not (SCM x);
126SCM_API SCM scm_boolean_p (SCM obj);
3a684cc6 127
102dbb6f 128SCM_INTERNAL void scm_init_boolean (void);
0f2d19dd 129
0527e687 130#endif /* SCM_BOOLEAN_H */
89e00824
ML
131
132/*
133 Local Variables:
134 c-file-style: "gnu"
135 End:
136*/