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