1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-objectid.c DBusObjectID type
4 * Copyright (C) 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-objectid.h"
25 #include "dbus-internals.h"
27 #ifdef DBUS_HAVE_INT64
28 #define VALUE(objid) ((objid)->dbus_do_not_use_dummy1)
29 #define HIGH_BITS(objid) ((dbus_uint32_t) (VALUE (obj_id) >> 32))
30 #define LOW_BITS(objid) ((dbus_uint32_t) (VALUE (obj_id) & DBUS_UINT64_CONSTANT (0x00000000ffffffff)))
32 #define HIGH_BITS(objid) ((objid)->dbus_do_not_use_dummy1)
33 #define LOW_BITS(objid) ((objid)->dbus_do_not_use_dummy2)
37 * @defgroup DBusObjectID object IDs
38 * @ingroup DBusObjectID
39 * @brief object ID datatype
41 * Value type representing an object ID, i.e. an object in the remote
42 * application that can be communicated with.
48 * Checks whether two object IDs have the same value.
50 * @param a the first object ID
51 * @param b the second object ID
52 * @returns #TRUE if they are equal
55 dbus_object_id_equal (const DBusObjectID *a,
56 const DBusObjectID *b)
58 #ifdef DBUS_HAVE_INT64
59 return VALUE (a) == VALUE (b);
61 return HIGH_BITS (a) == HIGH_BITS (b) &&
62 LOW_BITS (a) == LOW_BITS (b);
67 * Compares two object IDs, appropriate for
68 * qsort(). Higher/lower IDs have no significance,
69 * but the comparison can be used for data structures
70 * that require ordering.
72 * @param a the first object ID
73 * @param b the second object ID
74 * @returns -1, 0, 1 as with strcmp()
77 dbus_object_id_compare (const DBusObjectID *a,
78 const DBusObjectID *b)
80 #ifdef DBUS_HAVE_INT64
81 if (VALUE (a) > VALUE (b))
83 else if (VALUE (a) < VALUE (b))
88 if (HIGH_BITS (a) > HIGH_BITS (b))
90 else if (HIGH_BITS (a) < HIGH_BITS (b))
92 else if (LOW_BITS (a) > LOW_BITS (b))
94 else if (LOW_BITS (a) < LOW_BITS (b))
102 * An object ID contains 64 bits of data. This function
103 * returns half of those bits. If you are willing to limit
104 * portability to compilers with a 64-bit type (this includes
105 * C99 compilers and almost all other compilers) consider
106 * dbus_object_id_get_as_integer() instead.
108 * @param obj_id the object ID
109 * @returns the high bits of the ID
113 dbus_object_id_get_high_bits (const DBusObjectID *obj_id)
115 return HIGH_BITS (obj_id);
119 * An object ID contains 64 bits of data. This function
120 * returns half of those bits. If you are willing to limit
121 * portability to compilers with a 64-bit type (this includes
122 * C99 compilers and almost all other compilers) consider
123 * dbus_object_id_get_as_integer() instead.
125 * @param obj_id the object ID
126 * @returns the low bits of the ID
130 dbus_object_id_get_low_bits (const DBusObjectID *obj_id)
132 return LOW_BITS (obj_id);
136 * An object ID contains 64 bits of data. This function
137 * sets half of those bits. If you are willing to limit
138 * portability to compilers with a 64-bit type (this includes
139 * C99 compilers and almost all other compilers) consider
140 * dbus_object_id_set_as_integer() instead.
142 * @param obj_id the object ID
143 * @param value the new value of the high bits
147 dbus_object_id_set_high_bits (DBusObjectID *obj_id,
150 #ifdef DBUS_HAVE_INT64
151 VALUE (obj_id) = (((dbus_uint64_t) value) << 32) | LOW_BITS (obj_id);
153 HIGH_BITS (obj_id) = value;
158 * An object ID contains 64 bits of data. This function
159 * sets half of those bits. If you are willing to limit
160 * portability to compilers with a 64-bit type (this includes
161 * C99 compilers and almost all other compilers) consider
162 * dbus_object_id_set_as_integer() instead.
164 * @param obj_id the object ID
165 * @param value the new value of the low bits
169 dbus_object_id_set_low_bits (DBusObjectID *obj_id,
172 #ifdef DBUS_HAVE_INT64
173 VALUE (obj_id) = ((dbus_uint64_t) value) |
174 (((dbus_uint64_t) HIGH_BITS (obj_id)) << 32);
176 LOW_BITS (obj_id) = value;
180 #ifdef DBUS_HAVE_INT64
182 * An object ID contains 64 bits of data. This function
183 * returns all of them as a 64-bit integer.
185 * Use this function only if you are willing to limit portability to
186 * compilers with a 64-bit type (this includes C99 compilers and
187 * almost all other compilers).
189 * This function only exists if DBUS_HAVE_INT64 is defined.
191 * @param obj_id the object ID
192 * @returns the object ID as a 64-bit integer.
195 dbus_object_id_get_as_integer (const DBusObjectID *obj_id)
197 return VALUE (obj_id);
201 * An object ID contains 64 bits of data. This function sets all of
202 * them as a 64-bit integer.
204 * Use this function only if you are willing to limit portability to
205 * compilers with a 64-bit type (this includes C99 compilers and
206 * almost all other compilers).
208 * This function only exists if #DBUS_HAVE_INT64 is defined.
210 * @param obj_id the object ID
211 * @param value the new value of the object ID
214 dbus_object_id_set_as_integer (DBusObjectID *obj_id,
217 VALUE (obj_id) = value;
219 #endif /* DBUS_HAVE_INT64 */
223 #ifdef DBUS_BUILD_TESTS
224 #include "dbus-test.h"
228 * Test for object ID routines.
230 * @returns #TRUE on success
233 _dbus_object_id_test (void)
238 dbus_object_id_set_high_bits (&tmp, 340);
239 _dbus_assert (dbus_object_id_get_high_bits (&tmp) == 340);
241 dbus_object_id_set_low_bits (&tmp, 1492);
242 _dbus_assert (dbus_object_id_get_low_bits (&tmp) == 1492);
243 _dbus_assert (dbus_object_id_get_high_bits (&tmp) == 340);
246 _dbus_assert (dbus_object_id_equal (&tmp, &tmp2));
248 #ifdef DBUS_HAVE_INT64
249 _dbus_assert (dbus_object_id_get_as_integer (&tmp) ==
250 ((DBUS_UINT64_CONSTANT (340) << 32) |
251 DBUS_UINT64_CONSTANT (1492)));
253 dbus_object_id_set_as_integer (&tmp, _DBUS_UINT64_MAX);
254 _dbus_assert (dbus_object_id_get_as_integer (&tmp) ==
256 _dbus_assert (dbus_object_id_get_high_bits (&tmp) ==
258 _dbus_assert (dbus_object_id_get_low_bits (&tmp) ==
261 dbus_object_id_set_as_integer (&tmp, 1);
262 dbus_object_id_set_as_integer (&tmp2, 2);
263 _dbus_assert (dbus_object_id_compare (&tmp, &tmp2) == -1);
264 dbus_object_id_set_as_integer (&tmp2, 0);
265 _dbus_assert (dbus_object_id_compare (&tmp, &tmp2) == 1);
266 dbus_object_id_set_as_integer (&tmp2, 1);
267 _dbus_assert (dbus_object_id_compare (&tmp, &tmp2) == 0);
272 dbus_object_id_set_high_bits (&tmp, 1);
273 dbus_object_id_set_high_bits (&tmp2, 2);
274 _dbus_assert (dbus_object_id_compare (&tmp, &tmp2) == -1);
275 dbus_object_id_set_high_bits (&tmp2, 0);
276 _dbus_assert (dbus_object_id_compare (&tmp, &tmp2) == 1);
277 dbus_object_id_set_high_bits (&tmp2, 1);
278 _dbus_assert (dbus_object_id_compare (&tmp, &tmp2) == 0);
280 dbus_object_id_set_low_bits (&tmp, 1);
282 dbus_object_id_set_low_bits (&tmp2, 2);
283 _dbus_assert (dbus_object_id_compare (&tmp, &tmp2) == -1);
284 dbus_object_id_set_low_bits (&tmp2, 0);
285 _dbus_assert (dbus_object_id_compare (&tmp, &tmp2) == 1);
286 dbus_object_id_set_low_bits (&tmp2, 1);
287 _dbus_assert (dbus_object_id_compare (&tmp, &tmp2) == 0);
292 #endif /* DBUS_BUILD_TESTS */