prop_bool.c revision 1.9.2.2 1 1.9.2.2 thorpej /* $NetBSD: prop_bool.c,v 1.9.2.2 2006/10/16 03:21:08 thorpej Exp $ */
2 1.9.2.2 thorpej
3 1.9.2.2 thorpej /*-
4 1.9.2.2 thorpej * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.9.2.2 thorpej * All rights reserved.
6 1.9.2.2 thorpej *
7 1.9.2.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.9.2.2 thorpej * by Jason R. Thorpe.
9 1.9.2.2 thorpej *
10 1.9.2.2 thorpej * Redistribution and use in source and binary forms, with or without
11 1.9.2.2 thorpej * modification, are permitted provided that the following conditions
12 1.9.2.2 thorpej * are met:
13 1.9.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.9.2.2 thorpej * notice, this list of conditions and the following disclaimer.
15 1.9.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.9.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.9.2.2 thorpej * documentation and/or other materials provided with the distribution.
18 1.9.2.2 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.9.2.2 thorpej * must display the following acknowledgement:
20 1.9.2.2 thorpej * This product includes software developed by the NetBSD
21 1.9.2.2 thorpej * Foundation, Inc. and its contributors.
22 1.9.2.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.9.2.2 thorpej * contributors may be used to endorse or promote products derived
24 1.9.2.2 thorpej * from this software without specific prior written permission.
25 1.9.2.2 thorpej *
26 1.9.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.9.2.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.9.2.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.9.2.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.9.2.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.9.2.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.9.2.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.9.2.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.9.2.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.9.2.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.9.2.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.9.2.2 thorpej */
38 1.9.2.2 thorpej
39 1.9.2.2 thorpej #include <prop/prop_bool.h>
40 1.9.2.2 thorpej #include "prop_object_impl.h"
41 1.9.2.2 thorpej
42 1.9.2.2 thorpej struct _prop_bool {
43 1.9.2.2 thorpej struct _prop_object pb_obj;
44 1.9.2.2 thorpej boolean_t pb_value;
45 1.9.2.2 thorpej };
46 1.9.2.2 thorpej
47 1.9.2.2 thorpej static struct _prop_bool _prop_bool_true;
48 1.9.2.2 thorpej static struct _prop_bool _prop_bool_false;
49 1.9.2.2 thorpej
50 1.9.2.2 thorpej _PROP_MUTEX_DECL_STATIC(_prop_bool_initialized_mutex)
51 1.9.2.2 thorpej static boolean_t _prop_bool_initialized;
52 1.9.2.2 thorpej
53 1.9.2.2 thorpej static void _prop_bool_free(void *);
54 1.9.2.2 thorpej static boolean_t _prop_bool_externalize(
55 1.9.2.2 thorpej struct _prop_object_externalize_context *,
56 1.9.2.2 thorpej void *);
57 1.9.2.2 thorpej static boolean_t _prop_bool_equals(void *, void *);
58 1.9.2.2 thorpej
59 1.9.2.2 thorpej static const struct _prop_object_type _prop_object_type_bool = {
60 1.9.2.2 thorpej .pot_type = PROP_TYPE_BOOL,
61 1.9.2.2 thorpej .pot_free = _prop_bool_free,
62 1.9.2.2 thorpej .pot_extern = _prop_bool_externalize,
63 1.9.2.2 thorpej .pot_equals = _prop_bool_equals,
64 1.9.2.2 thorpej };
65 1.9.2.2 thorpej
66 1.9.2.2 thorpej #define prop_object_is_bool(x) \
67 1.9.2.2 thorpej ((x) != NULL && (x)->pb_obj.po_type == &_prop_object_type_bool)
68 1.9.2.2 thorpej
69 1.9.2.2 thorpej static void
70 1.9.2.2 thorpej /*ARGSUSED*/
71 1.9.2.2 thorpej _prop_bool_free(void *v _PROP_ARG_UNUSED)
72 1.9.2.2 thorpej {
73 1.9.2.2 thorpej
74 1.9.2.2 thorpej /*
75 1.9.2.2 thorpej * This should never happen as we "leak" our initial reference
76 1.9.2.2 thorpej * count.
77 1.9.2.2 thorpej */
78 1.9.2.2 thorpej /* XXX forced assertion failure? */
79 1.9.2.2 thorpej }
80 1.9.2.2 thorpej
81 1.9.2.2 thorpej static boolean_t
82 1.9.2.2 thorpej _prop_bool_externalize(struct _prop_object_externalize_context *ctx,
83 1.9.2.2 thorpej void *v)
84 1.9.2.2 thorpej {
85 1.9.2.2 thorpej prop_bool_t pb = v;
86 1.9.2.2 thorpej
87 1.9.2.2 thorpej return (_prop_object_externalize_empty_tag(ctx,
88 1.9.2.2 thorpej pb->pb_value ? "true" : "false"));
89 1.9.2.2 thorpej }
90 1.9.2.2 thorpej
91 1.9.2.2 thorpej static boolean_t
92 1.9.2.2 thorpej _prop_bool_equals(void *v1, void *v2)
93 1.9.2.2 thorpej {
94 1.9.2.2 thorpej prop_bool_t b1 = v1;
95 1.9.2.2 thorpej prop_bool_t b2 = v2;
96 1.9.2.2 thorpej
97 1.9.2.2 thorpej if (! (prop_object_is_bool(b1) &&
98 1.9.2.2 thorpej prop_object_is_bool(b2)))
99 1.9.2.2 thorpej return (FALSE);
100 1.9.2.2 thorpej
101 1.9.2.2 thorpej /*
102 1.9.2.2 thorpej * Since we only ever allocate one true and one false,
103 1.9.2.2 thorpej * save ourselves a couple of memory operations.
104 1.9.2.2 thorpej */
105 1.9.2.2 thorpej return (b1 == b2);
106 1.9.2.2 thorpej }
107 1.9.2.2 thorpej
108 1.9.2.2 thorpej static prop_bool_t
109 1.9.2.2 thorpej _prop_bool_alloc(boolean_t val)
110 1.9.2.2 thorpej {
111 1.9.2.2 thorpej prop_bool_t pb;
112 1.9.2.2 thorpej
113 1.9.2.2 thorpej if (! _prop_bool_initialized) {
114 1.9.2.2 thorpej _PROP_MUTEX_LOCK(_prop_bool_initialized_mutex);
115 1.9.2.2 thorpej if (! _prop_bool_initialized) {
116 1.9.2.2 thorpej _prop_object_init(&_prop_bool_true.pb_obj,
117 1.9.2.2 thorpej &_prop_object_type_bool);
118 1.9.2.2 thorpej _prop_bool_true.pb_value = TRUE;
119 1.9.2.2 thorpej
120 1.9.2.2 thorpej _prop_object_init(&_prop_bool_false.pb_obj,
121 1.9.2.2 thorpej &_prop_object_type_bool);
122 1.9.2.2 thorpej _prop_bool_false.pb_value = FALSE;
123 1.9.2.2 thorpej
124 1.9.2.2 thorpej _prop_bool_initialized = TRUE;
125 1.9.2.2 thorpej }
126 1.9.2.2 thorpej _PROP_MUTEX_UNLOCK(_prop_bool_initialized_mutex);
127 1.9.2.2 thorpej }
128 1.9.2.2 thorpej
129 1.9.2.2 thorpej pb = val ? &_prop_bool_true : &_prop_bool_false;
130 1.9.2.2 thorpej prop_object_retain(pb);
131 1.9.2.2 thorpej
132 1.9.2.2 thorpej return (pb);
133 1.9.2.2 thorpej }
134 1.9.2.2 thorpej
135 1.9.2.2 thorpej /*
136 1.9.2.2 thorpej * prop_bool_create --
137 1.9.2.2 thorpej * Create a prop_bool_t and initialize it with the
138 1.9.2.2 thorpej * provided boolean value.
139 1.9.2.2 thorpej */
140 1.9.2.2 thorpej prop_bool_t
141 1.9.2.2 thorpej prop_bool_create(boolean_t val)
142 1.9.2.2 thorpej {
143 1.9.2.2 thorpej
144 1.9.2.2 thorpej return (_prop_bool_alloc(val));
145 1.9.2.2 thorpej }
146 1.9.2.2 thorpej
147 1.9.2.2 thorpej /*
148 1.9.2.2 thorpej * prop_bool_copy --
149 1.9.2.2 thorpej * Copy a prop_bool_t.
150 1.9.2.2 thorpej */
151 1.9.2.2 thorpej prop_bool_t
152 1.9.2.2 thorpej prop_bool_copy(prop_bool_t opb)
153 1.9.2.2 thorpej {
154 1.9.2.2 thorpej
155 1.9.2.2 thorpej if (! prop_object_is_bool(opb))
156 1.9.2.2 thorpej return (NULL);
157 1.9.2.2 thorpej
158 1.9.2.2 thorpej /*
159 1.9.2.2 thorpej * Because we only ever allocate one true and one false, this
160 1.9.2.2 thorpej * can be reduced to a simple retain operation.
161 1.9.2.2 thorpej */
162 1.9.2.2 thorpej prop_object_retain(opb);
163 1.9.2.2 thorpej return (opb);
164 1.9.2.2 thorpej }
165 1.9.2.2 thorpej
166 1.9.2.2 thorpej /*
167 1.9.2.2 thorpej * prop_bool_true --
168 1.9.2.2 thorpej * Get the value of a prop_bool_t.
169 1.9.2.2 thorpej */
170 1.9.2.2 thorpej boolean_t
171 1.9.2.2 thorpej prop_bool_true(prop_bool_t pb)
172 1.9.2.2 thorpej {
173 1.9.2.2 thorpej
174 1.9.2.2 thorpej if (! prop_object_is_bool(pb))
175 1.9.2.2 thorpej return (FALSE);
176 1.9.2.2 thorpej
177 1.9.2.2 thorpej return (pb->pb_value);
178 1.9.2.2 thorpej }
179 1.9.2.2 thorpej
180 1.9.2.2 thorpej /*
181 1.9.2.2 thorpej * prop_bool_equals --
182 1.9.2.2 thorpej * Return TRUE if the boolean values are equivalent.
183 1.9.2.2 thorpej */
184 1.9.2.2 thorpej boolean_t
185 1.9.2.2 thorpej prop_bool_equals(prop_bool_t b1, prop_bool_t b2)
186 1.9.2.2 thorpej {
187 1.9.2.2 thorpej
188 1.9.2.2 thorpej return (_prop_bool_equals(b1, b2));
189 1.9.2.2 thorpej }
190 1.9.2.2 thorpej
191 1.9.2.2 thorpej /*
192 1.9.2.2 thorpej * _prop_bool_internalize --
193 1.9.2.2 thorpej * Parse a <true/> or <false/> and return the object created from
194 1.9.2.2 thorpej * the external representation.
195 1.9.2.2 thorpej */
196 1.9.2.2 thorpej prop_object_t
197 1.9.2.2 thorpej _prop_bool_internalize(struct _prop_object_internalize_context *ctx)
198 1.9.2.2 thorpej {
199 1.9.2.2 thorpej boolean_t val;
200 1.9.2.2 thorpej
201 1.9.2.2 thorpej /* No attributes, and it must be an empty element. */
202 1.9.2.2 thorpej if (ctx->poic_tagattr != NULL ||
203 1.9.2.2 thorpej ctx->poic_is_empty_element == FALSE)
204 1.9.2.2 thorpej return (NULL);
205 1.9.2.2 thorpej
206 1.9.2.2 thorpej if (_PROP_TAG_MATCH(ctx, "true"))
207 1.9.2.2 thorpej val = TRUE;
208 1.9.2.2 thorpej else {
209 1.9.2.2 thorpej _PROP_ASSERT(_PROP_TAG_MATCH(ctx, "false"));
210 1.9.2.2 thorpej val = FALSE;
211 1.9.2.2 thorpej }
212 1.9.2.2 thorpej
213 1.9.2.2 thorpej return (prop_bool_create(val));
214 1.9.2.2 thorpej }
215