1*d3273b5bSchristos /* $NetBSD: number.c,v 1.2 2017/01/28 21:31:45 christos Exp $ */
2b9d004c6Schristos
3b9d004c6Schristos /*
4b9d004c6Schristos * Copyright (c) 2010 Kungliga Tekniska Högskolan
5b9d004c6Schristos * (Royal Institute of Technology, Stockholm, Sweden).
6b9d004c6Schristos * All rights reserved.
7b9d004c6Schristos *
8b9d004c6Schristos * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
9b9d004c6Schristos *
10b9d004c6Schristos * Redistribution and use in source and binary forms, with or without
11b9d004c6Schristos * modification, are permitted provided that the following conditions
12b9d004c6Schristos * are met:
13b9d004c6Schristos *
14b9d004c6Schristos * 1. Redistributions of source code must retain the above copyright
15b9d004c6Schristos * notice, this list of conditions and the following disclaimer.
16b9d004c6Schristos *
17b9d004c6Schristos * 2. Redistributions in binary form must reproduce the above copyright
18b9d004c6Schristos * notice, this list of conditions and the following disclaimer in the
19b9d004c6Schristos * documentation and/or other materials provided with the distribution.
20b9d004c6Schristos *
21b9d004c6Schristos * 3. Neither the name of the Institute nor the names of its contributors
22b9d004c6Schristos * may be used to endorse or promote products derived from this software
23b9d004c6Schristos * without specific prior written permission.
24b9d004c6Schristos *
25b9d004c6Schristos * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26b9d004c6Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27b9d004c6Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28b9d004c6Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29b9d004c6Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30b9d004c6Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31b9d004c6Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32b9d004c6Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33b9d004c6Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34b9d004c6Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35b9d004c6Schristos * SUCH DAMAGE.
36b9d004c6Schristos */
37b9d004c6Schristos
38b9d004c6Schristos #include "baselocl.h"
39b9d004c6Schristos
40b9d004c6Schristos static void
number_dealloc(void * ptr)41b9d004c6Schristos number_dealloc(void *ptr)
42b9d004c6Schristos {
43b9d004c6Schristos }
44b9d004c6Schristos
45b9d004c6Schristos static int
number_cmp(void * a,void * b)46b9d004c6Schristos number_cmp(void *a, void *b)
47b9d004c6Schristos {
48b9d004c6Schristos int na, nb;
49b9d004c6Schristos
50b9d004c6Schristos if (heim_base_is_tagged_object(a))
51b9d004c6Schristos na = heim_base_tagged_object_value(a);
52b9d004c6Schristos else
53b9d004c6Schristos na = *(int *)a;
54b9d004c6Schristos
55b9d004c6Schristos if (heim_base_is_tagged_object(b))
56b9d004c6Schristos nb = heim_base_tagged_object_value(b);
57b9d004c6Schristos else
58b9d004c6Schristos nb = *(int *)b;
59b9d004c6Schristos
60b9d004c6Schristos return na - nb;
61b9d004c6Schristos }
62b9d004c6Schristos
63b9d004c6Schristos static unsigned long
number_hash(void * ptr)64b9d004c6Schristos number_hash(void *ptr)
65b9d004c6Schristos {
66b9d004c6Schristos if (heim_base_is_tagged_object(ptr))
67b9d004c6Schristos return heim_base_tagged_object_value(ptr);
68b9d004c6Schristos return (unsigned long)*(int *)ptr;
69b9d004c6Schristos }
70b9d004c6Schristos
71b9d004c6Schristos struct heim_type_data _heim_number_object = {
72b9d004c6Schristos HEIM_TID_NUMBER,
73b9d004c6Schristos "number-object",
74b9d004c6Schristos NULL,
75b9d004c6Schristos number_dealloc,
76b9d004c6Schristos NULL,
77b9d004c6Schristos number_cmp,
78b9d004c6Schristos number_hash,
79b9d004c6Schristos NULL
80b9d004c6Schristos };
81b9d004c6Schristos
82b9d004c6Schristos /**
83b9d004c6Schristos * Create a number object
84b9d004c6Schristos *
85b9d004c6Schristos * @param the number to contain in the object
86b9d004c6Schristos *
87b9d004c6Schristos * @return a number object
88b9d004c6Schristos */
89b9d004c6Schristos
90b9d004c6Schristos heim_number_t
heim_number_create(int number)91b9d004c6Schristos heim_number_create(int number)
92b9d004c6Schristos {
93b9d004c6Schristos heim_number_t n;
94b9d004c6Schristos
95b9d004c6Schristos if (number < 0xffffff && number >= 0)
96b9d004c6Schristos return heim_base_make_tagged_object(number, HEIM_TID_NUMBER);
97b9d004c6Schristos
98b9d004c6Schristos n = _heim_alloc_object(&_heim_number_object, sizeof(int));
99b9d004c6Schristos if (n)
100b9d004c6Schristos *((int *)n) = number;
101b9d004c6Schristos return n;
102b9d004c6Schristos }
103b9d004c6Schristos
104b9d004c6Schristos /**
105b9d004c6Schristos * Return the type ID of number objects
106b9d004c6Schristos *
107b9d004c6Schristos * @return type id of number objects
108b9d004c6Schristos */
109b9d004c6Schristos
110b9d004c6Schristos heim_tid_t
heim_number_get_type_id(void)111b9d004c6Schristos heim_number_get_type_id(void)
112b9d004c6Schristos {
113b9d004c6Schristos return HEIM_TID_NUMBER;
114b9d004c6Schristos }
115b9d004c6Schristos
116b9d004c6Schristos /**
117b9d004c6Schristos * Get the int value of the content
118b9d004c6Schristos *
119b9d004c6Schristos * @param number the number object to get the value from
120b9d004c6Schristos *
121b9d004c6Schristos * @return an int
122b9d004c6Schristos */
123b9d004c6Schristos
124b9d004c6Schristos int
heim_number_get_int(heim_number_t number)125b9d004c6Schristos heim_number_get_int(heim_number_t number)
126b9d004c6Schristos {
127b9d004c6Schristos if (heim_base_is_tagged_object(number))
128b9d004c6Schristos return heim_base_tagged_object_value(number);
129b9d004c6Schristos return *(int *)number;
130b9d004c6Schristos }
131