1/* -*- mode: C; c-file-style: "gnu" -*- */
2/* dbus-hash.h Generic hash table utility (internal to D-Bus implementation)
3 *
4 * Copyright (C) 2002  Red Hat, Inc.
5 *
6 * Licensed under the Academic Free License version 2.1
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#ifndef DBUS_HASH_H
25#define DBUS_HASH_H
26
27#include <dbus/dbus-memory.h>
28#include <dbus/dbus-types.h>
29
30DBUS_BEGIN_DECLS
31
32/**
33 * @addtogroup DBusHashTable
34 * @{
35 */
36
37/** Hash iterator object. The iterator is on the stack, but its real
38 * fields are hidden privately.
39 */
40struct DBusHashIter
41{
42  void *dummy1; /**< Do not use. */
43  void *dummy2; /**< Do not use. */
44  void *dummy3; /**< Do not use. */
45  void *dummy4; /**< Do not use. */
46  int   dummy5; /**< Do not use. */
47  int   dummy6; /**< Do not use. */
48};
49
50typedef struct DBusHashTable DBusHashTable;
51typedef struct DBusHashIter  DBusHashIter;
52
53/* Allowing an arbitrary function as with GLib
54 * would be nicer for a public API, but for
55 * an internal API this saves typing, we can add
56 * more whenever we feel like it.
57 */
58typedef enum
59{
60  DBUS_HASH_STRING,        /**< Hash keys are strings. */
61  DBUS_HASH_TWO_STRINGS,   /**< Hash key is two strings in one memory block, i.e. foo\\0bar\\0 */
62  DBUS_HASH_INT,           /**< Hash keys are integers. */
63  DBUS_HASH_POINTER,       /**< Hash keys are pointers. */
64  DBUS_HASH_ULONG          /**< Hash keys are unsigned long. */
65} DBusHashType;
66
67DBusHashTable* _dbus_hash_table_new                (DBusHashType      type,
68                                                    DBusFreeFunction  key_free_function,
69                                                    DBusFreeFunction  value_free_function);
70DBusHashTable* _dbus_hash_table_ref                (DBusHashTable    *table);
71void           _dbus_hash_table_unref              (DBusHashTable    *table);
72void           _dbus_hash_table_remove_all         (DBusHashTable    *table);
73void           _dbus_hash_iter_init                (DBusHashTable    *table,
74                                                    DBusHashIter     *iter);
75dbus_bool_t    _dbus_hash_iter_next                (DBusHashIter     *iter);
76void           _dbus_hash_iter_remove_entry        (DBusHashIter     *iter);
77void*          _dbus_hash_iter_get_value           (DBusHashIter     *iter);
78void           _dbus_hash_iter_set_value           (DBusHashIter     *iter,
79                                                    void             *value);
80int            _dbus_hash_iter_get_int_key         (DBusHashIter     *iter);
81const char*    _dbus_hash_iter_get_string_key      (DBusHashIter     *iter);
82const char*    _dbus_hash_iter_get_two_strings_key (DBusHashIter     *iter);
83unsigned long  _dbus_hash_iter_get_ulong_key       (DBusHashIter     *iter);
84dbus_bool_t    _dbus_hash_iter_lookup              (DBusHashTable    *table,
85                                                    void             *key,
86                                                    dbus_bool_t       create_if_not_found,
87                                                    DBusHashIter     *iter);
88void*          _dbus_hash_table_lookup_string      (DBusHashTable    *table,
89                                                    const char       *key);
90void*          _dbus_hash_table_lookup_two_strings (DBusHashTable    *table,
91                                                    const char       *key);
92void*          _dbus_hash_table_lookup_int         (DBusHashTable    *table,
93                                                    int               key);
94void*          _dbus_hash_table_lookup_pointer     (DBusHashTable    *table,
95                                                    void             *key);
96void*          _dbus_hash_table_lookup_ulong       (DBusHashTable    *table,
97                                                    unsigned long     key);
98dbus_bool_t    _dbus_hash_table_remove_string      (DBusHashTable    *table,
99                                                    const char       *key);
100dbus_bool_t    _dbus_hash_table_remove_two_strings (DBusHashTable    *table,
101                                                    const char       *key);
102dbus_bool_t    _dbus_hash_table_remove_int         (DBusHashTable    *table,
103                                                    int               key);
104dbus_bool_t    _dbus_hash_table_remove_pointer     (DBusHashTable    *table,
105                                                    void             *key);
106dbus_bool_t    _dbus_hash_table_remove_ulong       (DBusHashTable    *table,
107                                                    unsigned long     key);
108dbus_bool_t    _dbus_hash_table_insert_string      (DBusHashTable    *table,
109                                                    char             *key,
110                                                    void             *value);
111dbus_bool_t    _dbus_hash_table_insert_two_strings (DBusHashTable    *table,
112                                                    char             *key,
113                                                    void             *value);
114dbus_bool_t    _dbus_hash_table_insert_int         (DBusHashTable    *table,
115                                                    int               key,
116                                                    void             *value);
117dbus_bool_t    _dbus_hash_table_insert_pointer     (DBusHashTable    *table,
118                                                    void             *key,
119                                                    void             *value);
120dbus_bool_t    _dbus_hash_table_insert_ulong       (DBusHashTable    *table,
121                                                    unsigned long     key,
122                                                    void             *value);
123int            _dbus_hash_table_get_n_entries      (DBusHashTable    *table);
124
125/* Preallocation */
126
127/** A preallocated hash entry */
128typedef struct DBusPreallocatedHash DBusPreallocatedHash;
129
130DBusPreallocatedHash *_dbus_hash_table_preallocate_entry          (DBusHashTable        *table);
131void                  _dbus_hash_table_free_preallocated_entry    (DBusHashTable        *table,
132                                                                   DBusPreallocatedHash *preallocated);
133void                  _dbus_hash_table_insert_string_preallocated (DBusHashTable        *table,
134                                                                   DBusPreallocatedHash *preallocated,
135                                                                   char                 *key,
136                                                                   void                 *value);
137
138/** @} */
139
140DBUS_END_DECLS
141
142#endif /* DBUS_HASH_H */
143