Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
s/_XBT_GNUC_FUNCTION/_XBT_FUNCTION/ (that's how to get __FUNCTION__ portably)
[simgrid.git] / include / xbt / set.h
1 /* $Id$ */
2
3 /* xbt/set.h -- api to a generic dictionary                                 */
4
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #ifndef _XBT_SET_H
11 #define _XBT_SET_H
12
13 #include "xbt/misc.h" /* BEGIN_DECL */
14
15 BEGIN_DECL()
16
17 /** @addtogroup XBT_set
18  * 
19  *  The elements stored in such a data structure can be retrieve both by
20  *  name and by ID. For this to work, the first fields of the structures
21  *  stored must begin with:
22  *  \verbatim unsigned int ID;
23  char        *name;
24  unsigned int name_len;\endverbatim
25  *
26  *  It is impossible to remove an element from such a data structure.
27  *
28  *  @todo
29  *  Such a datastructure was necessary/useful to store the GRAS type 
30  *  descriptions, but it should be reworked to become generic.
31  *
32  * @{
33 */
34
35
36 /** @name 1. Set and set elements, constructor/destructor
37  *
38  *  @{
39  */
40 /** \brief Opaque type representing a set */
41 typedef struct xbt_set_ *xbt_set_t; 
42 /** \brief It must be possible to cast set elements to this type */
43 struct xbt_set_elm_ {
44   unsigned int ID;      /**< Identificator (system assigned) */
45   char        *name;    /**< Name (user assigned) */
46   unsigned int name_len;/**< Length of the name */
47 };
48
49 /*####[ Functions ]##########################################################*/
50 xbt_set_t xbt_set_new (void);
51 void xbt_set_free(xbt_set_t *set);
52
53 /** @} */
54 typedef struct xbt_set_elm_  s_xbt_set_elm_t;
55 typedef struct xbt_set_elm_ *  xbt_set_elm_t;
56 /** @name 2. Main functions
57  *
58  *  @{
59  */
60
61 void xbt_set_add (xbt_set_t      set,
62                    xbt_set_elm_t  elm,
63                    void_f_pvoid_t *free_func);
64
65 xbt_error_t xbt_set_get_by_name    (xbt_set_t      set,
66                                     const char     *key,
67                                     /* OUT */xbt_set_elm_t *dst);
68 xbt_error_t xbt_set_get_by_name_ext(xbt_set_t      set,
69                                     const char     *name,
70                                     int             name_len,
71                                     /* OUT */xbt_set_elm_t *dst);
72 xbt_error_t xbt_set_get_by_id      (xbt_set_t      set,
73                                     int             id,
74                                     /* OUT */xbt_set_elm_t *dst);
75                                       
76 /** @} */
77 /** @name 3. Cursors
78  *
79  *  \warning Don't add or remove entries to the cache while traversing
80  *
81  *  @{
82  */
83
84 /** @brief Cursor type */
85 typedef struct xbt_set_cursor_ *xbt_set_cursor_t;
86
87 void         xbt_set_cursor_first       (xbt_set_t         set,
88                                           xbt_set_cursor_t *cursor);
89 void         xbt_set_cursor_step        (xbt_set_cursor_t  cursor);
90 int          xbt_set_cursor_get_or_free (xbt_set_cursor_t *cursor,
91                                           xbt_set_elm_t    *elm);
92
93 /** @brief Iterates over the whole set
94  *  @hideinitializer
95  */
96 #define xbt_set_foreach(set,cursor,elm)                       \
97   for ((cursor) = NULL, xbt_set_cursor_first((set),&(cursor)) ;   \
98        xbt_set_cursor_get_or_free(&(cursor),(xbt_set_elm_t*)&(elm));          \
99        xbt_set_cursor_step(cursor) )
100
101 /* @} */
102 /* @} */
103 END_DECL()
104
105 #endif /* _XBT_SET_H */