NetBSD/common/lib/libprop/prop_number.3

298 lines
10 KiB
Groff

.\" $NetBSD: prop_number.3,v 1.12 2020/06/06 21:25:59 thorpej Exp $
.\"
.\" Copyright (c) 2006, 2020 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Jason R. Thorpe.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd June 2, 2020
.Dt PROP_NUMBER 3
.Os
.Sh NAME
.Nm prop_number ,
.Nm prop_number_create_signed ,
.Nm prop_number_create_unsigned ,
.Nm prop_number_copy ,
.Nm prop_number_size ,
.Nm prop_number_unsigned ,
.Nm prop_number_signed_value ,
.Nm prop_number_unsigned_value ,
.Nm prop_number_schar_value ,
.Nm prop_number_short_value ,
.Nm prop_number_int_value ,
.Nm prop_number_long_value ,
.Nm prop_number_longlong_value ,
.Nm prop_number_intptr_value ,
.Nm prop_number_int8_value ,
.Nm prop_number_int16_value ,
.Nm prop_number_int32_value ,
.Nm prop_number_int64_value ,
.Nm prop_number_uchar_value ,
.Nm prop_number_ushort_value ,
.Nm prop_number_uint_value ,
.Nm prop_number_ulong_value ,
.Nm prop_number_ulonglong_value ,
.Nm prop_number_uintptr_value ,
.Nm prop_number_uint8_value ,
.Nm prop_number_uint16_value ,
.Nm prop_number_uint32_value ,
.Nm prop_number_uint64_value ,
.Nm prop_number_equals ,
.Nm prop_number_equals_signed ,
.Nm prop_number_equals_unsigned
.Nd numeric value property object
.Sh LIBRARY
.Lb libprop
.Sh SYNOPSIS
.In prop/proplib.h
.\"
.Ft prop_number_t
.Fn prop_number_create_signed "intmax_t val"
.Ft prop_number_t
.Fn prop_number_create_unsigned "uintmax_t val"
.Ft prop_number_t
.Fn prop_number_copy "prop_number_t number"
.\"
.Ft int
.Fn prop_number_size "prop_number_t number"
.Ft bool
.Fn prop_number_unsigned "prop_number_t number"
.Ft intmax_t
.Fn prop_number_signed_value "prop_number_t number"
.Ft uintmax_t
.Fn prop_number_usigned_value "prop_number_t number"
.\"
.Ft bool
.Fn prop_number_schar_value "prop_number_t number" "signed char *valp"
.Ft bool
.Fn prop_number_short_value "prop_number_t number" "short *valp"
.Ft bool
.Fn prop_number_int_value "prop_number_t number" "int *valp"
.Ft bool
.Fn prop_number_long_value "prop_number_t number" "long *valp"
.Ft bool
.Fn prop_number_longlong_value "prop_number_t number" "long long *valp"
.Ft bool
.Fn prop_number_intptr_value "prop_number_t number" "intptr_t *valp"
.Ft bool
.Fn prop_number_int8_value "prop_number_t number" "int8_t *valp"
.Ft bool
.Fn prop_number_int16_value "prop_number_t number" "int16_t *valp"
.Ft bool
.Fn prop_number_int32_value "prop_number_t number" "int32_t *valp"
.Ft bool
.Fn prop_number_int64_value "prop_number_t number" "int64_t *valp"
.\"
.Ft bool
.Fn prop_number_uchar_value "prop_number_t number" "unsigned char *valp"
.Ft bool
.Fn prop_number_ushort_value "prop_number_t number" "unsigned short *valp"
.Ft bool
.Fn prop_number_uint_value "prop_number_t number" "unsigned int *valp"
.Ft bool
.Fn prop_number_ulong_value "prop_number_t number" "unsigned long *valp"
.Ft bool
.Fn prop_number_ulonglong_value "prop_number_t number" "unsigned long long *valp"
.Ft bool
.Fn prop_number_uintptr_value "prop_number_t number" "uintptr_t *valp"
.Ft bool
.Fn prop_number_uint8_value "prop_number_t number" "uint8_t *valp"
.Ft bool
.Fn prop_number_uint16_value "prop_number_t number" "uint16_t *valp"
.Ft bool
.Fn prop_number_uint32_value "prop_number_t number" "uint32_t *valp"
.Ft bool
.Fn prop_number_uint64_value "prop_number_t number" "uint64_t *valp"
.\"
.Ft bool
.Fn prop_number_equals "prop_number_t num1" "prop_number_t num2"
.Ft bool
.Fn prop_number_equals_signed "prop_number_t number" "intmax_t val"
.Ft bool
.Fn prop_number_equals_unsigned "prop_number_t number" "uintmax_t val"
.Sh DESCRIPTION
The
.Nm
family of functions operate on a numeric value property object type.
Values are either signed or unsigned, and promoted to the maximum size
integer type
.Pq intmax_t or uintmax_t , respectively .
.Pp
It is possible to compare number objects that differ in sign.
Such comparisons first test to see if each object is within the valid
number range of the other:
.Bl -bullet
.It
Signed numbers that are greater than or equal to 0 can be compared to
unsigned numbers.
.It
Unsigned numbers that are less than or equal to the largest signed
integer value
.Pq Dv INTMAX_MAX
can be compared to signed numbers.
.El
.Pp
Number objects have a different externalized representation depending
on their sign:
.Bl -bullet
.It
Signed numbers are externalized in base-10
.Pq decimal .
.It
Unsigned numbers are externalized in base-16
.Pq hexadecimal .
.El
.Pp
When numbers are internalized, the sign of the resulting number object
.Pq and thus its valid range
is determined by a set of rules evaluated in the following order:
.Bl -bullet
.It
If the first character of the number is a
.Sq -
then the number is signed.
.It
If the first two characters of the number are
.Sq 0x
then the number is unsigned.
.It
If the number value fits into the range of a signed number then the
number is signed.
.It
In all other cases, the number is unsigned.
.El
.Bl -tag -width "xxxxx"
.It Fn prop_number_create_signed "intmax_t val"
Create a numeric value object with the signed value
.Fa val .
Returns
.Dv NULL
on failure.
.It Fn prop_number_create_unsigned "uintmax_t val"
Create a numeric value object with the unsigned value
.Fa val .
Returns
.Dv NULL
on failure.
.It Fn prop_number_copy "prop_number_t number"
Copy a numeric value object.
If the supplied object isn't a numeric value,
.Dv NULL
is returned.
.It Fn prop_number_size "prop_number_t number"
Returns 8, 16, 32, or 64, representing the number of bits required to
hold the value of the object.
If the supplied object isn't a numeric value, 0 is returned.
.It Fn prop_number_unsigned "prop_number_t number"
Returns
.Dv true
if the numeric value object has an unsigned value.
.It Fn prop_number_signed_value "prop_number_t number"
Returns the signed value of the numeric value object.
If the supplied object isn't a numeric value, zero is returned.
Thus,
it is not possible to distinguish between
.Dq not a prop_number_t
and
.Dq prop_number_t has a value of 0 .
.It Fn prop_number_unsigned_value "prop_number_t number"
Returns the unsigned value of the numeric value object.
If the supplied object isn't a numeric value, zero is returned.
Thus,
it is not possible to distinguish between
.Dq not a prop_number_t
and
.Dq prop_number_t has a value of 0 .
.\"
.It Fn prop_number_schar_value "prop_number_t number" "signed char *valp"
.It Fn prop_number_short_value "prop_number_t number" "short *valp"
.It Fn prop_number_int_value "prop_number_t number" "int *valp"
.It Fn prop_number_long_value "prop_number_t number" "long *valp"
.It Fn prop_number_longlong_value "prop_number_t number" "long long *valp"
.It Fn prop_number_intptr_value "prop_number_t number" "intptr_t *valp"
.It Fn prop_number_int8_value "prop_number_t number" "int8_t *valp"
.It Fn prop_number_int16_value "prop_number_t number" "int16_t *valp"
.It Fn prop_number_int32_value "prop_number_t number" "int32_t *valp"
.It Fn prop_number_int64_value "prop_number_t number" "int64_t *valp"
.It Fn prop_number_uchar_value "prop_number_t number" "unsigned char *valp"
.It Fn prop_number_ushort_value "prop_number_t number" "unsigned short *valp"
.It Fn prop_number_uint_value "prop_number_t number" "unsigned int *valp"
.It Fn prop_number_ulong_value "prop_number_t number" "unsigned long *valp"
.It Fn prop_number_ulonglong_value "prop_number_t number" "unsigned long long *valp"
.It Fn prop_number_uintptr_value "prop_number_t number" "uintptr_t *valp"
.It Fn prop_number_uint8_value "prop_number_t number" "uint8_t *valp"
.It Fn prop_number_uint16_value "prop_number_t number" "uint16_t *valp"
.It Fn prop_number_uint32_value "prop_number_t number" "uint32_t *valp"
.It Fn prop_number_uint64_value "prop_number_t number" "uint64_t *valp"
These functions extract the numeric value as the specified type and
store it in
.Fa valp .
The value is bounds-checked against the minimum and maximum values of
the type.
If the value can be represented in the specified type, these functions
return
.Dv true .
Otherwise, they return
.Dv false .
.\"
.It Fn prop_number_equals "prop_number_t num1" "prop_number_t num2"
Returns
.Dv true
if the two numeric value objects are equivalent.
If at least one of the supplied objects isn't a numeric value,
.Dv false
is returned.
.It Fn prop_number_equals_signed "prop_number_t number" "intmax_t val"
Returns
.Dv true
if the object's value is equivalent to the signed value
.Fa val .
If the supplied object isn't a numerical value,
.Dv false
is returned.
.It Fn prop_number_equals_unsigned "prop_number_t number" \
"uintmax_t val"
Returns
.Dv true
if the object's value is equivalent to the unsigned value
.Fa val .
If the supplied object isn't a numerical value,
.Dv false
is returned.
.El
.Sh SEE ALSO
.Xr prop_array 3 ,
.Xr prop_bool 3 ,
.Xr prop_data 3 ,
.Xr prop_dictionary 3 ,
.Xr prop_object 3 ,
.Xr prop_string 3 ,
.Xr proplib 3
.Sh HISTORY
The
.Xr proplib 3
property container object library first appeared in
.Nx 4.0 .