summaryrefslogtreecommitdiff
path: root/dbus/dbus-objectid.c
blob: 1fb83e44f2597c130d26cd138a7ea91dda650d2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-objectid.c  DBusObjectID type
 *
 * Copyright (C) 2003  Red Hat Inc.
 *
 * Licensed under the Academic Free License version 1.2
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "dbus-objectid.h"
#include "dbus-internals.h"

#ifdef DBUS_HAVE_INT64
#define VALUE(objid) ((objid)->dbus_do_not_use_dummy1)
#define HIGH_BITS(objid) ((dbus_uint32_t) (VALUE (obj_id) >> 32))
#define LOW_BITS(objid)  ((dbus_uint32_t) (VALUE (obj_id) & DBUS_UINT64_CONSTANT (0x00000000ffffffff)))
#else
#define HIGH_BITS(objid) ((objid)->dbus_do_not_use_dummy1)
#define LOW_BITS(objid) ((objid)->dbus_do_not_use_dummy2)
#endif

/**
 * @defgroup DBusObjectID object IDs
 * @ingroup  DBusObjectID
 * @brief object ID datatype
 *
 * Value type representing an object ID, i.e. an object in the remote
 * application that can be communicated with.
 *
 * @{
 */

/**
 * Checks whether two object IDs have the same value.
 *
 * @param a the first object ID
 * @param b the second object ID
 * @returns #TRUE if they are equal
 */
dbus_bool_t
dbus_object_id_equal (const DBusObjectID *a,
                      const DBusObjectID *b)
{
#ifdef DBUS_HAVE_INT64
  return VALUE (a) == VALUE (b);
#else
  return HIGH_BITS (a) == HIGH_BITS (b) &&
    LOW_BITS (a) == LOW_BITS (b);
#endif
}

/**
 * Compares two object IDs, appropriate for
 * qsort(). Higher/lower IDs have no significance,
 * but the comparison can be used for data structures
 * that require ordering.
 *
 * @param a the first object ID
 * @param b the second object ID
 * @returns -1, 0, 1 as with strcmp()
 */
int
dbus_object_id_compare (const DBusObjectID *a,
                        const DBusObjectID *b)
{
#ifdef DBUS_HAVE_INT64
  if (VALUE (a) > VALUE (b))
    return 1;
  else if (VALUE (a) < VALUE (b))
    return -1;
  else
    return 0;
#else
  if (HIGH_BITS (a) > HIGH_BITS (b))
    return 1;
  else if (HIGH_BITS (a) < HIGH_BITS (b))
    return -1;
  else if (LOW_BITS (a) > LOW_BITS (b))
    return 1;
  else if (LOW_BITS (a) < LOW_BITS (b))
    return -1;
  else
    return 0;
#endif
}

/**
 * An object ID contains 64 bits of data. This function
 * returns half of those bits. If you are willing to limit
 * portability to compilers with a 64-bit type (this includes
 * C99 compilers and almost all other compilers) consider
 * dbus_object_id_get_as_integer() instead.
 *
 * @param obj_id the object ID
 * @returns the high bits of the ID
 * 
 */
dbus_uint32_t
dbus_object_id_get_high_bits (const DBusObjectID *obj_id)
{
  return HIGH_BITS (obj_id);
}

/**
 * An object ID contains 64 bits of data. This function
 * returns half of those bits. If you are willing to limit
 * portability to compilers with a 64-bit type (this includes
 * C99 compilers and almost all other compilers) consider
 * dbus_object_id_get_as_integer() instead.
 *
 * @param obj_id the object ID
 * @returns the low bits of the ID
 * 
 */
dbus_uint32_t
dbus_object_id_get_low_bits (const DBusObjectID *obj_id)
{
  return LOW_BITS (obj_id);
}

/**
 * An object ID contains 64 bits of data. This function
 * sets half of those bits. If you are willing to limit
 * portability to compilers with a 64-bit type (this includes
 * C99 compilers and almost all other compilers) consider
 * dbus_object_id_set_as_integer() instead.
 *
 * @param obj_id the object ID
 * @param value the new value of the high bits
 * 
 */
void
dbus_object_id_set_high_bits (DBusObjectID       *obj_id,
                              dbus_uint32_t       value)
{
#ifdef DBUS_HAVE_INT64
  VALUE (obj_id) = (((dbus_uint64_t) value) << 32) | LOW_BITS (obj_id);
#else
  HIGH_BITS (obj_id) = value;
#endif
}

/**
 * An object ID contains 64 bits of data. This function
 * sets half of those bits. If you are willing to limit
 * portability to compilers with a 64-bit type (this includes
 * C99 compilers and almost all other compilers) consider
 * dbus_object_id_set_as_integer() instead.
 *
 * @param obj_id the object ID
 * @param value the new value of the low bits
 * 
 */
void
dbus_object_id_set_low_bits (DBusObjectID       *obj_id,
                             dbus_uint32_t       value)
{
#ifdef DBUS_HAVE_INT64
  VALUE (obj_id) = ((dbus_uint64_t) value) |
    (((dbus_uint64_t) HIGH_BITS (obj_id)) << 32);
#else
  LOW_BITS (obj_id) = value;
#endif
}

#ifdef DBUS_HAVE_INT64
/**
 * An object ID contains 64 bits of data. This function
 * returns all of them as a 64-bit integer.
 *  
 * Use this function only if you are willing to limit portability to
 * compilers with a 64-bit type (this includes C99 compilers and
 * almost all other compilers).
 *
 * This function only exists if DBUS_HAVE_INT64 is defined.
 *
 * @param obj_id the object ID
 * @returns the object ID as a 64-bit integer.
 */
dbus_uint64_t
dbus_object_id_get_as_integer (const DBusObjectID *obj_id)
{
  return VALUE (obj_id);
}

/**
 * An object ID contains 64 bits of data. This function sets all of
 * them as a 64-bit integer.
 *  
 * Use this function only if you are willing to limit portability to
 * compilers with a 64-bit type (this includes C99 compilers and
 * almost all other compilers).
 * 
 * This function only exists if #DBUS_HAVE_INT64 is defined.
 *
 * @param obj_id the object ID
 * @param value the new value of the object ID
 */
void
dbus_object_id_set_as_integer (DBusObjectID       *obj_id,
                               dbus_uint64_t       value)
{
  VALUE (obj_id) = value;
}
#endif /* DBUS_HAVE_INT64 */

/** @} */

#ifdef DBUS_BUILD_TESTS
#include "dbus-test.h"
#include <stdio.h>

/**
 * Test for object ID routines.
 *
 * @returns #TRUE on success
 */
dbus_bool_t
_dbus_object_id_test (void)
{
  DBusObjectID tmp;
  DBusObjectID tmp2;

  dbus_object_id_set_high_bits (&tmp, 340);
  _dbus_assert (dbus_object_id_get_high_bits (&tmp) == 340);

  dbus_object_id_set_low_bits (&tmp, 1492);
  _dbus_assert (dbus_object_id_get_low_bits (&tmp) == 1492);
  _dbus_assert (dbus_object_id_get_high_bits (&tmp) == 340);
  
  tmp2 = tmp;
  _dbus_assert (dbus_object_id_equal (&tmp, &tmp2));
  
#ifdef DBUS_HAVE_INT64
  _dbus_assert (dbus_object_id_get_as_integer (&tmp) ==
                ((DBUS_UINT64_CONSTANT (340) << 32) |
                 DBUS_UINT64_CONSTANT (1492)));

  dbus_object_id_set_as_integer (&tmp, _DBUS_UINT64_MAX);
  _dbus_assert (dbus_object_id_get_as_integer (&tmp) ==
                _DBUS_UINT64_MAX);
  _dbus_assert (dbus_object_id_get_high_bits (&tmp) ==
                _DBUS_UINT_MAX);
  _dbus_assert (dbus_object_id_get_low_bits (&tmp) ==
                _DBUS_UINT_MAX);

  dbus_object_id_set_as_integer (&tmp, 1);
  dbus_object_id_set_as_integer (&tmp2, 2);
  _dbus_assert (dbus_object_id_compare (&tmp, &tmp2) == -1);
  dbus_object_id_set_as_integer (&tmp2, 0);
  _dbus_assert (dbus_object_id_compare (&tmp, &tmp2) == 1);
  dbus_object_id_set_as_integer (&tmp2, 1);
  _dbus_assert (dbus_object_id_compare (&tmp, &tmp2) == 0);
#endif

  tmp2 = tmp;
  
  dbus_object_id_set_high_bits (&tmp, 1);
  dbus_object_id_set_high_bits (&tmp2, 2);
  _dbus_assert (dbus_object_id_compare (&tmp, &tmp2) == -1);
  dbus_object_id_set_high_bits (&tmp2, 0);
  _dbus_assert (dbus_object_id_compare (&tmp, &tmp2) == 1);
  dbus_object_id_set_high_bits (&tmp2, 1);
  _dbus_assert (dbus_object_id_compare (&tmp, &tmp2) == 0);

  dbus_object_id_set_low_bits (&tmp, 1);
  
  dbus_object_id_set_low_bits (&tmp2, 2);
  _dbus_assert (dbus_object_id_compare (&tmp, &tmp2) == -1);
  dbus_object_id_set_low_bits (&tmp2, 0);
  _dbus_assert (dbus_object_id_compare (&tmp, &tmp2) == 1);
  dbus_object_id_set_low_bits (&tmp2, 1);
  _dbus_assert (dbus_object_id_compare (&tmp, &tmp2) == 0);
  
  return TRUE;
}

#endif /* DBUS_BUILD_TESTS */