| 1 | #ifndef IATTRIBUTE_H |
| 2 | #define IATTRIBUTE_H |
| 3 | |
| 4 | |
| 5 | #include <glib-object.h> |
| 6 | #include <time.h> |
| 7 | |
| 8 | /* The content of a data structure may be seen as an array of pairs of |
| 9 | attribute name and value. This simple model allows generic navigation |
| 10 | and access functions over a wide range of structures. The names are |
| 11 | represented by unique integer identifiers, GQuarks. */ |
| 12 | |
| 13 | typedef GQuark LttvAttributeName; |
| 14 | |
| 15 | typedef struct timespec LttvTime; |
| 16 | |
| 17 | typedef enum _LttvAttributeType { |
| 18 | LTTV_INT, LTTV_UINT, LTTV_LONG, LTTV_ULONG, LTTV_FLOAT, LTTV_DOUBLE, |
| 19 | LTTV_TIME, LTTV_POINTER, LTTV_STRING, LTTV_GOBJECT, LTTV_NONE |
| 20 | } LttvAttributeType; |
| 21 | |
| 22 | typedef union LttvAttributeValue { |
| 23 | int *v_int; |
| 24 | unsigned *v_uint; |
| 25 | long *v_long; |
| 26 | unsigned long *v_ulong; |
| 27 | float *v_float; |
| 28 | double *v_double; |
| 29 | LttvTime *v_time; |
| 30 | gpointer *v_pointer; |
| 31 | char **v_string; |
| 32 | GObject **v_gobject; |
| 33 | } LttvAttributeValue; |
| 34 | |
| 35 | |
| 36 | /* GObject interface type macros */ |
| 37 | |
| 38 | #define LTTV_IATTRIBUTE_TYPE (lttv_iattribute_get_type ()) |
| 39 | #define LTTV_IATTRIBUTE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), LTTV_IATTRIBUTE_TYPE, LttvIAttribute)) |
| 40 | #define LTTV_IATTRIBUTE_CLASS(vtable) (G_TYPE_CHECK_CLASS_CAST ((vtable), LTTV_IATTRIBUTE_TYPE, LttvIAttributeClass)) |
| 41 | #define LTTV_IS_IATTRIBUTE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), LTTV_IATTRIBUTE_TYPE)) |
| 42 | #define LTTV_IS_IATTRIBUTE_CLASS(vtable) (G_TYPE_CHECK_CLASS_TYPE ((vtable), LTTV_IATTRIBUTE_TYPE)) |
| 43 | #define LTTV_IATTRIBUTE_GET_CLASS(inst) (G_TYPE_INSTANCE_GET_INTERFACE ((inst), LTTV_IATTRIBUTE_TYPE, LttvIAttributeClass)) |
| 44 | |
| 45 | |
| 46 | typedef struct _LttvIattribute LttvIAttribute; /* dummy object */ |
| 47 | typedef struct _LttvIAttributeClass LttvIAttributeClass; |
| 48 | |
| 49 | |
| 50 | struct _LttvIAttributeClass { |
| 51 | GTypeInterface parent; |
| 52 | |
| 53 | unsigned int (*get_number) (LttvIAttribute *self); |
| 54 | |
| 55 | gboolean (*named) (LttvIAttribute *self, gboolean *homogeneous); |
| 56 | |
| 57 | LttvAttributeType (*get) (LttvIAttribute *self, unsigned i, |
| 58 | LttvAttributeName *name, LttvAttributeValue *v); |
| 59 | |
| 60 | LttvAttributeType (*get_by_name) (LttvIAttribute *self, |
| 61 | LttvAttributeName name, LttvAttributeValue *v); |
| 62 | |
| 63 | LttvAttributeValue (*add) (LttvIAttribute *self, LttvAttributeName name, |
| 64 | LttvAttributeType t); |
| 65 | |
| 66 | void (*remove) (LttvIAttribute *self, unsigned i); |
| 67 | |
| 68 | void (*remove_by_name) (LttvIAttribute *self, |
| 69 | LttvAttributeName name); |
| 70 | |
| 71 | LttvIAttribute* (*find_subdir) (LttvIAttribute *self, |
| 72 | LttvAttributeName name); |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | GType lttv_iattribute_get_type(void); |
| 77 | |
| 78 | |
| 79 | /* Total number of attributes */ |
| 80 | |
| 81 | unsigned int lttv_iattribute_get_number(LttvIAttribute *self); |
| 82 | |
| 83 | |
| 84 | /* Container type. Named (fields in struct or elements in a hash table) |
| 85 | or unnamed (elements in an array) attributes, homogeneous type or not. */ |
| 86 | |
| 87 | gboolean lttv_iattribute_named(LttvIAttribute *self, gboolean *homogeneous); |
| 88 | |
| 89 | |
| 90 | /* Get the i th attribute along with its type and a pointer to its value. */ |
| 91 | |
| 92 | LttvAttributeType lttv_iattribute_get(LttvIAttribute *self, unsigned i, |
| 93 | LttvAttributeName *name, LttvAttributeValue *v); |
| 94 | |
| 95 | |
| 96 | /* Get the named attribute in the table along with its type and a pointer to |
| 97 | its value. If the named attribute does not exist, the type is LTTV_NONE. */ |
| 98 | |
| 99 | LttvAttributeType lttv_iattribute_get_by_name(LttvIAttribute *self, |
| 100 | LttvAttributeName name, LttvAttributeValue *v); |
| 101 | |
| 102 | |
| 103 | /* Add an attribute, which must not exist. The name is an empty string for |
| 104 | containers with unnamed attributes. Its value is initialized to 0 or NULL |
| 105 | and its pointer returned. */ |
| 106 | |
| 107 | LttvAttributeValue lttv_iattribute_add(LttvIAttribute *self, |
| 108 | LttvAttributeName name, LttvAttributeType t); |
| 109 | |
| 110 | |
| 111 | /* Remove an attribute */ |
| 112 | |
| 113 | void lttv_iattribute_remove(LttvIAttribute *self, unsigned i); |
| 114 | |
| 115 | void lttv_iattribute_remove_by_name(LttvIAttribute *self, |
| 116 | LttvAttributeName name); |
| 117 | |
| 118 | |
| 119 | /* Create an empty iattribute object and add it as an attribute under the |
| 120 | specified name, or return an existing iattribute attribute. If an |
| 121 | attribute of that name already exists but is not a GObject supporting the |
| 122 | iattribute interface, return NULL. */ |
| 123 | |
| 124 | LttvIAttribute* lttv_iattribute_find_subdir(LttvIAttribute *self, |
| 125 | LttvAttributeName name); |
| 126 | |
| 127 | |
| 128 | /* The remaining utility functions are not part of the LttvIAttribute |
| 129 | interface but operate on objects implementing it. */ |
| 130 | |
| 131 | /* Find the named attribute in the table, which must be of the specified type. |
| 132 | If it does not exist, it is created with a default value of 0 (NULL for |
| 133 | pointer types). Since the address of the value is obtained, it may be |
| 134 | changed easily afterwards. The function returns false when the attribute |
| 135 | exists but is of incorrect type. */ |
| 136 | |
| 137 | gboolean lttv_iattribute_find(LttvIAttribute *self, LttvAttributeName name, |
| 138 | LttvAttributeType t, LttvAttributeValue *v); |
| 139 | |
| 140 | |
| 141 | /* Trees of attribute tables may be accessed using a hierarchical path with |
| 142 | components separated by /, like in filesystems */ |
| 143 | |
| 144 | gboolean lttv_iattribute_find_by_path(LttvIAttribute *self, char *path, |
| 145 | LttvAttributeType t, LttvAttributeValue *v); |
| 146 | |
| 147 | |
| 148 | /* Shallow and deep copies */ |
| 149 | |
| 150 | void lttv_iattribute_copy_value(LttvAttributeType t, LttvAttributeValue dest, |
| 151 | LttvAttributeValue src); |
| 152 | |
| 153 | LttvIAttribute *lttv_iattribute_shallow_copy(LttvIAttribute *self); |
| 154 | |
| 155 | LttvIAttribute *lttv_iattribute_deep_copy(LttvIAttribute *self); |
| 156 | |
| 157 | #endif // IATTRIBUTE_H |