Home | History | Annotate | Line # | Download | only in libprop
      1  1.24  thorpej /*	$NetBSD: prop_bool.c,v 1.24 2025/05/14 03:25:45 thorpej Exp $	*/
      2   1.1  thorpej 
      3   1.1  thorpej /*-
      4  1.20  thorpej  * Copyright (c) 2006, 2025 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  *
     19   1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  thorpej  */
     31   1.1  thorpej 
     32  1.18  thorpej #include "prop_object_impl.h"
     33   1.1  thorpej #include <prop/prop_bool.h>
     34   1.1  thorpej 
     35   1.1  thorpej struct _prop_bool {
     36   1.1  thorpej 	struct _prop_object	pb_obj;
     37  1.20  thorpej 	bool			pb_value;
     38   1.1  thorpej };
     39   1.1  thorpej 
     40   1.3  thorpej static struct _prop_bool _prop_bool_true;
     41   1.3  thorpej static struct _prop_bool _prop_bool_false;
     42   1.3  thorpej 
     43  1.20  thorpej static const char truestr[] = "true";
     44  1.20  thorpej static const char falsestr[] = "false";
     45  1.20  thorpej 
     46  1.20  thorpej static const struct _prop_object_type_tags _prop_bool_true_type_tags = {
     47  1.20  thorpej 	.xml_tag	=	truestr,
     48  1.20  thorpej 	.json_open_tag	=	truestr,
     49  1.20  thorpej };
     50  1.20  thorpej static const struct _prop_object_type_tags _prop_bool_false_type_tags = {
     51  1.20  thorpej 	.xml_tag	=	falsestr,
     52  1.20  thorpej 	.json_open_tag	=	falsestr,
     53  1.20  thorpej };
     54  1.20  thorpej 
     55  1.16  thorpej static _prop_object_free_rv_t
     56  1.16  thorpej 		_prop_bool_free(prop_stack_t, prop_object_t *);
     57  1.10  thorpej static bool	_prop_bool_externalize(
     58   1.2  thorpej 				struct _prop_object_externalize_context *,
     59   1.2  thorpej 				void *);
     60  1.16  thorpej static _prop_object_equals_rv_t
     61  1.16  thorpej 		_prop_bool_equals(prop_object_t, prop_object_t,
     62  1.12    joerg 				  void **, void **,
     63  1.12    joerg 				  prop_object_t *, prop_object_t *);
     64   1.2  thorpej 
     65   1.2  thorpej static const struct _prop_object_type _prop_object_type_bool = {
     66   1.2  thorpej 	.pot_type	=	PROP_TYPE_BOOL,
     67   1.2  thorpej 	.pot_free	=	_prop_bool_free,
     68   1.2  thorpej 	.pot_extern	=	_prop_bool_externalize,
     69   1.2  thorpej 	.pot_equals	=	_prop_bool_equals,
     70   1.2  thorpej };
     71   1.2  thorpej 
     72   1.2  thorpej #define	prop_object_is_bool(x)		\
     73   1.5  thorpej 	((x) != NULL && (x)->pb_obj.po_type == &_prop_object_type_bool)
     74   1.1  thorpej 
     75  1.11    joerg /* ARGSUSED */
     76  1.16  thorpej static _prop_object_free_rv_t
     77  1.11    joerg _prop_bool_free(prop_stack_t stack, prop_object_t *obj)
     78   1.1  thorpej {
     79   1.3  thorpej 	/*
     80   1.3  thorpej 	 * This should never happen as we "leak" our initial reference
     81   1.3  thorpej 	 * count.
     82   1.3  thorpej 	 */
     83  1.11    joerg 
     84   1.3  thorpej 	/* XXX forced assertion failure? */
     85  1.11    joerg 	return (_PROP_OBJECT_FREE_DONE);
     86   1.1  thorpej }
     87   1.1  thorpej 
     88  1.10  thorpej static bool
     89   1.1  thorpej _prop_bool_externalize(struct _prop_object_externalize_context *ctx,
     90   1.1  thorpej 		       void *v)
     91   1.1  thorpej {
     92   1.1  thorpej 	prop_bool_t pb = v;
     93  1.20  thorpej 	const struct _prop_object_type_tags *tags =
     94  1.20  thorpej 	    pb->pb_value ? &_prop_bool_true_type_tags
     95  1.20  thorpej 			 : &_prop_bool_false_type_tags;
     96   1.1  thorpej 
     97  1.24  thorpej 	return _prop_extern_append_empty_tag(ctx, tags);
     98   1.1  thorpej }
     99   1.1  thorpej 
    100  1.12    joerg /* ARGSUSED */
    101  1.16  thorpej static _prop_object_equals_rv_t
    102  1.12    joerg _prop_bool_equals(prop_object_t v1, prop_object_t v2,
    103  1.12    joerg     void **stored_pointer1, void **stored_pointer2,
    104  1.12    joerg     prop_object_t *next_obj1, prop_object_t *next_obj2)
    105   1.2  thorpej {
    106   1.2  thorpej 	prop_bool_t b1 = v1;
    107   1.2  thorpej 	prop_bool_t b2 = v2;
    108   1.2  thorpej 
    109   1.4  thorpej 	if (! (prop_object_is_bool(b1) &&
    110   1.4  thorpej 	       prop_object_is_bool(b2)))
    111  1.12    joerg 		return (_PROP_OBJECT_EQUALS_FALSE);
    112   1.3  thorpej 
    113   1.3  thorpej 	/*
    114   1.3  thorpej 	 * Since we only ever allocate one true and one false,
    115   1.3  thorpej 	 * save ourselves a couple of memory operations.
    116   1.3  thorpej 	 */
    117  1.12    joerg 	if (b1 == b2)
    118  1.12    joerg 		return (_PROP_OBJECT_EQUALS_TRUE);
    119  1.12    joerg 	else
    120  1.12    joerg 		return (_PROP_OBJECT_EQUALS_FALSE);
    121   1.2  thorpej }
    122   1.2  thorpej 
    123  1.17    pooka _PROP_ONCE_DECL(_prop_bool_init_once)
    124  1.17    pooka 
    125  1.17    pooka static int
    126  1.17    pooka _prop_bool_init(void)
    127  1.17    pooka {
    128  1.17    pooka 
    129  1.17    pooka 	_prop_object_init(&_prop_bool_true.pb_obj,
    130  1.17    pooka 	    &_prop_object_type_bool);
    131  1.17    pooka 	_prop_bool_true.pb_value = true;
    132  1.17    pooka 
    133  1.17    pooka 	_prop_object_init(&_prop_bool_false.pb_obj,
    134  1.17    pooka 	    &_prop_object_type_bool);
    135  1.17    pooka 	_prop_bool_false.pb_value = false;
    136  1.17    pooka 
    137  1.17    pooka 	return 0;
    138  1.17    pooka }
    139  1.17    pooka 
    140   1.1  thorpej static prop_bool_t
    141  1.10  thorpej _prop_bool_alloc(bool val)
    142   1.1  thorpej {
    143   1.1  thorpej 	prop_bool_t pb;
    144   1.1  thorpej 
    145  1.17    pooka 	_PROP_ONCE_RUN(_prop_bool_init_once, _prop_bool_init);
    146   1.3  thorpej 	pb = val ? &_prop_bool_true : &_prop_bool_false;
    147   1.3  thorpej 	prop_object_retain(pb);
    148   1.3  thorpej 
    149   1.1  thorpej 	return (pb);
    150   1.1  thorpej }
    151   1.1  thorpej 
    152   1.1  thorpej /*
    153   1.1  thorpej  * prop_bool_create --
    154   1.1  thorpej  *	Create a prop_bool_t and initialize it with the
    155   1.1  thorpej  *	provided boolean value.
    156   1.1  thorpej  */
    157  1.21  thorpej _PROP_EXPORT prop_bool_t
    158  1.10  thorpej prop_bool_create(bool val)
    159   1.1  thorpej {
    160   1.1  thorpej 
    161   1.3  thorpej 	return (_prop_bool_alloc(val));
    162   1.1  thorpej }
    163   1.1  thorpej 
    164   1.1  thorpej /*
    165   1.1  thorpej  * prop_bool_copy --
    166   1.1  thorpej  *	Copy a prop_bool_t.
    167   1.1  thorpej  */
    168  1.21  thorpej _PROP_EXPORT prop_bool_t
    169   1.1  thorpej prop_bool_copy(prop_bool_t opb)
    170   1.1  thorpej {
    171   1.1  thorpej 
    172   1.4  thorpej 	if (! prop_object_is_bool(opb))
    173   1.4  thorpej 		return (NULL);
    174   1.1  thorpej 
    175   1.3  thorpej 	/*
    176   1.3  thorpej 	 * Because we only ever allocate one true and one false, this
    177   1.3  thorpej 	 * can be reduced to a simple retain operation.
    178   1.3  thorpej 	 */
    179   1.3  thorpej 	prop_object_retain(opb);
    180   1.3  thorpej 	return (opb);
    181   1.1  thorpej }
    182   1.1  thorpej 
    183   1.1  thorpej /*
    184  1.19  thorpej  * prop_bool_value --
    185   1.1  thorpej  *	Get the value of a prop_bool_t.
    186   1.1  thorpej  */
    187  1.21  thorpej _PROP_EXPORT bool
    188  1.19  thorpej prop_bool_value(prop_bool_t pb)
    189   1.1  thorpej {
    190   1.1  thorpej 
    191   1.4  thorpej 	if (! prop_object_is_bool(pb))
    192  1.10  thorpej 		return (false);
    193   1.4  thorpej 
    194   1.1  thorpej 	return (pb->pb_value);
    195   1.1  thorpej }
    196   1.1  thorpej 
    197   1.1  thorpej /*
    198  1.19  thorpej  * prop_bool_true --
    199  1.19  thorpej  *	Historical alias for prop_bool_value().
    200  1.19  thorpej  */
    201  1.21  thorpej _PROP_EXPORT bool
    202  1.19  thorpej prop_bool_true(prop_bool_t pb)
    203  1.19  thorpej {
    204  1.19  thorpej 	return prop_bool_value(pb);
    205  1.19  thorpej }
    206  1.19  thorpej 
    207  1.19  thorpej /*
    208   1.2  thorpej  * prop_bool_equals --
    209  1.10  thorpej  *	Return true if the boolean values are equivalent.
    210   1.2  thorpej  */
    211  1.21  thorpej _PROP_EXPORT bool
    212   1.2  thorpej prop_bool_equals(prop_bool_t b1, prop_bool_t b2)
    213   1.2  thorpej {
    214  1.12    joerg 	if (!prop_object_is_bool(b1) || !prop_object_is_bool(b2))
    215  1.12    joerg 		return (false);
    216   1.2  thorpej 
    217  1.12    joerg 	return (prop_object_equals(b1, b2));
    218   1.2  thorpej }
    219   1.2  thorpej 
    220   1.2  thorpej /*
    221   1.1  thorpej  * _prop_bool_internalize --
    222   1.1  thorpej  *	Parse a <true/> or <false/> and return the object created from
    223   1.1  thorpej  *	the external representation.
    224   1.1  thorpej  */
    225  1.11    joerg 
    226  1.11    joerg /* ARGSUSED */
    227  1.11    joerg bool
    228  1.11    joerg _prop_bool_internalize(prop_stack_t stack, prop_object_t *obj,
    229  1.11    joerg     struct _prop_object_internalize_context *ctx)
    230   1.1  thorpej {
    231  1.10  thorpej 	bool val;
    232   1.1  thorpej 
    233  1.20  thorpej 	/*
    234  1.20  thorpej 	 * N.B. For internalizing JSON, the layer above us has
    235  1.20  thorpej 	 * made it look like XML for this object type.
    236  1.20  thorpej 	 */
    237  1.20  thorpej 
    238   1.1  thorpej 	/* No attributes, and it must be an empty element. */
    239   1.1  thorpej 	if (ctx->poic_tagattr != NULL ||
    240  1.10  thorpej 	    ctx->poic_is_empty_element == false)
    241  1.11    joerg 	    	return (true);
    242   1.1  thorpej 
    243  1.20  thorpej 	if (_PROP_TAG_MATCH(ctx, truestr))
    244  1.10  thorpej 		val = true;
    245   1.1  thorpej 	else {
    246  1.20  thorpej 		_PROP_ASSERT(_PROP_TAG_MATCH(ctx, falsestr));
    247  1.10  thorpej 		val = false;
    248   1.1  thorpej 	}
    249  1.11    joerg 	*obj = prop_bool_create(val);
    250  1.11    joerg 	return (true);
    251   1.1  thorpej }
    252