Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rewrite/simplify the C++ flag declaration
[simgrid.git] / include / xbt / lib.h
1 /* xbt/lib.h - api to a generic library                                     */
2
3 /* Copyright (c) 2011, 2013-2014. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #ifndef _XBT_LIB_H
10 #define _XBT_LIB_H
11
12 #include <xbt/dict.h>
13
14 SG_BEGIN_DECL()
15
16 /** Container for all the objects of a given type
17  *
18  *  * each item is identified by a string name/identifier;
19  *
20  *  * the lib itself is a dictionary from the element id to the element;
21  *
22  *  * the element itself is represented aby the xbt_dictelm_t;
23  *
24  *  * the element can store any number of associated facets/data structures (corresponding to the different layers of
25  *    SimGrid or its extensions) in ((void**)dictelt->content)[level];
26  *
27  *  * each level is allocated in the lib with `xbt_lib_add_level`.
28  *
29  *  <pre>
30  *  // Define a collection for the foo objects and two associated facets:
31  *  typedef xbt_dictelm_t foo_t;
32  *  xbt_lib_t foo_lib = xbt_lib_new();
33  *  int BAR_FOO_LEVEL  = xbt_lib_add_level(foo_lib, free_bar);
34  *  int AUTH_FOO_LEVEL = xbt_lib_add_level(foo_lib, free_auth);
35  *
36  *  // Store a bar:
37  *  bar_t bar = bar_new();
38  *  char* id = bar_name(bar);
39  *  xbt_lib_set(id, id, BAR_FOO_LEVEL, bar);
40  *
41  *  // Find the corresponding foo and the facet again:
42  *  foo_t foo = xbt_lib_get_elm_or_null(foo_lib, id);
43  *  bar_t bar2 = (bar_t) xbt_lib_get_level(foo, BAR_FOO_LEVEL);
44  *  assert(bar == bar2);
45  *
46  *  // Add authentication facet for the previous object:
47  *  auth_t auth = auth_new();
48  *  xbt_lib_set(foo_lib, id, AUTH_FOO_LEVEL, auth);
49  *  </pre>
50  */
51 typedef struct s_xbt_lib {
52   xbt_dict_t dict;
53   int levels;
54   void_f_pvoid_t *free_f;       /* This is actually a table */
55 } s_xbt_lib_t, *xbt_lib_t;
56
57 #define xbt_lib_cursor_t xbt_dict_cursor_t
58
59 XBT_PUBLIC(xbt_lib_t) xbt_lib_new(void);
60 XBT_PUBLIC(void) xbt_lib_free(xbt_lib_t * lib);
61 XBT_PUBLIC(int) xbt_lib_add_level(xbt_lib_t lib, void_f_pvoid_t free_f);
62 XBT_PUBLIC(void) xbt_lib_set(xbt_lib_t lib, const char *name, int level, void *obj);
63 XBT_PUBLIC(void) xbt_lib_unset(xbt_lib_t lib, const char *key, int level, int invoke_callback);
64 XBT_PUBLIC(void *) xbt_lib_get_or_null(xbt_lib_t lib, const char *name, int level);
65 XBT_PUBLIC(xbt_dictelm_t) xbt_lib_get_elm_or_null(xbt_lib_t lib, const char *key);
66 XBT_PUBLIC(void *) xbt_lib_get_level(xbt_dictelm_t elm, int level);
67 XBT_PUBLIC(void) xbt_lib_remove(xbt_lib_t lib, const char *key);
68
69 #define xbt_lib_length(lib) xbt_dict_length((lib)->dict)
70
71 /** @def xbt_lib_foreach
72     @hideinitializer */
73 #define xbt_lib_foreach(lib, cursor, key, data)         \
74   xbt_dict_foreach((lib)->dict, cursor, key, data)
75
76 SG_END_DECL()
77
78 #ifdef __cplusplus
79 namespace simgrid {
80 namespace xbt {
81   inline void destroy(xbt_lib_t l)
82   {
83     xbt_lib_free(&l);
84   }
85 }
86 }
87 #endif
88
89 #endif                          /* _XBT_LIB_H */