1fb83e44f2597c130d26cd138a7ea91dda650d2b
[platform/upstream/dbus.git] / dbus / dbus-objectid.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-objectid.c  DBusObjectID type
3  *
4  * Copyright (C) 2003  Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23
24 #include "dbus-objectid.h"
25 #include "dbus-internals.h"
26
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)))
31 #else
32 #define HIGH_BITS(objid) ((objid)->dbus_do_not_use_dummy1)
33 #define LOW_BITS(objid) ((objid)->dbus_do_not_use_dummy2)
34 #endif
35
36 /**
37  * @defgroup DBusObjectID object IDs
38  * @ingroup  DBusObjectID
39  * @brief object ID datatype
40  *
41  * Value type representing an object ID, i.e. an object in the remote
42  * application that can be communicated with.
43  *
44  * @{
45  */
46
47 /**
48  * Checks whether two object IDs have the same value.
49  *
50  * @param a the first object ID
51  * @param b the second object ID
52  * @returns #TRUE if they are equal
53  */
54 dbus_bool_t
55 dbus_object_id_equal (const DBusObjectID *a,
56                       const DBusObjectID *b)
57 {
58 #ifdef DBUS_HAVE_INT64
59   return VALUE (a) == VALUE (b);
60 #else
61   return HIGH_BITS (a) == HIGH_BITS (b) &&
62     LOW_BITS (a) == LOW_BITS (b);
63 #endif
64 }
65
66 /**
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.
71  *
72  * @param a the first object ID
73  * @param b the second object ID
74  * @returns -1, 0, 1 as with strcmp()
75  */
76 int
77 dbus_object_id_compare (const DBusObjectID *a,
78                         const DBusObjectID *b)
79 {
80 #ifdef DBUS_HAVE_INT64
81   if (VALUE (a) > VALUE (b))
82     return 1;
83   else if (VALUE (a) < VALUE (b))
84     return -1;
85   else
86     return 0;
87 #else
88   if (HIGH_BITS (a) > HIGH_BITS (b))
89     return 1;
90   else if (HIGH_BITS (a) < HIGH_BITS (b))
91     return -1;
92   else if (LOW_BITS (a) > LOW_BITS (b))
93     return 1;
94   else if (LOW_BITS (a) < LOW_BITS (b))
95     return -1;
96   else
97     return 0;
98 #endif
99 }
100
101 /**
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.
107  *
108  * @param obj_id the object ID
109  * @returns the high bits of the ID
110  * 
111  */
112 dbus_uint32_t
113 dbus_object_id_get_high_bits (const DBusObjectID *obj_id)
114 {
115   return HIGH_BITS (obj_id);
116 }
117
118 /**
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.
124  *
125  * @param obj_id the object ID
126  * @returns the low bits of the ID
127  * 
128  */
129 dbus_uint32_t
130 dbus_object_id_get_low_bits (const DBusObjectID *obj_id)
131 {
132   return LOW_BITS (obj_id);
133 }
134
135 /**
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.
141  *
142  * @param obj_id the object ID
143  * @param value the new value of the high bits
144  * 
145  */
146 void
147 dbus_object_id_set_high_bits (DBusObjectID       *obj_id,
148                               dbus_uint32_t       value)
149 {
150 #ifdef DBUS_HAVE_INT64
151   VALUE (obj_id) = (((dbus_uint64_t) value) << 32) | LOW_BITS (obj_id);
152 #else
153   HIGH_BITS (obj_id) = value;
154 #endif
155 }
156
157 /**
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.
163  *
164  * @param obj_id the object ID
165  * @param value the new value of the low bits
166  * 
167  */
168 void
169 dbus_object_id_set_low_bits (DBusObjectID       *obj_id,
170                              dbus_uint32_t       value)
171 {
172 #ifdef DBUS_HAVE_INT64
173   VALUE (obj_id) = ((dbus_uint64_t) value) |
174     (((dbus_uint64_t) HIGH_BITS (obj_id)) << 32);
175 #else
176   LOW_BITS (obj_id) = value;
177 #endif
178 }
179
180 #ifdef DBUS_HAVE_INT64
181 /**
182  * An object ID contains 64 bits of data. This function
183  * returns all of them as a 64-bit integer.
184  *  
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).
188  *
189  * This function only exists if DBUS_HAVE_INT64 is defined.
190  *
191  * @param obj_id the object ID
192  * @returns the object ID as a 64-bit integer.
193  */
194 dbus_uint64_t
195 dbus_object_id_get_as_integer (const DBusObjectID *obj_id)
196 {
197   return VALUE (obj_id);
198 }
199
200 /**
201  * An object ID contains 64 bits of data. This function sets all of
202  * them as a 64-bit integer.
203  *  
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).
207  * 
208  * This function only exists if #DBUS_HAVE_INT64 is defined.
209  *
210  * @param obj_id the object ID
211  * @param value the new value of the object ID
212  */
213 void
214 dbus_object_id_set_as_integer (DBusObjectID       *obj_id,
215                                dbus_uint64_t       value)
216 {
217   VALUE (obj_id) = value;
218 }
219 #endif /* DBUS_HAVE_INT64 */
220
221 /** @} */
222
223 #ifdef DBUS_BUILD_TESTS
224 #include "dbus-test.h"
225 #include <stdio.h>
226
227 /**
228  * Test for object ID routines.
229  *
230  * @returns #TRUE on success
231  */
232 dbus_bool_t
233 _dbus_object_id_test (void)
234 {
235   DBusObjectID tmp;
236   DBusObjectID tmp2;
237
238   dbus_object_id_set_high_bits (&tmp, 340);
239   _dbus_assert (dbus_object_id_get_high_bits (&tmp) == 340);
240
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);
244   
245   tmp2 = tmp;
246   _dbus_assert (dbus_object_id_equal (&tmp, &tmp2));
247   
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)));
252
253   dbus_object_id_set_as_integer (&tmp, _DBUS_UINT64_MAX);
254   _dbus_assert (dbus_object_id_get_as_integer (&tmp) ==
255                 _DBUS_UINT64_MAX);
256   _dbus_assert (dbus_object_id_get_high_bits (&tmp) ==
257                 _DBUS_UINT_MAX);
258   _dbus_assert (dbus_object_id_get_low_bits (&tmp) ==
259                 _DBUS_UINT_MAX);
260
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);
268 #endif
269
270   tmp2 = tmp;
271   
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);
279
280   dbus_object_id_set_low_bits (&tmp, 1);
281   
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);
288   
289   return TRUE;
290 }
291
292 #endif /* DBUS_BUILD_TESTS */