2 #include <lttv/iattribute.h>
5 lttv_iattribute_base_init (gpointer klass
)
7 static gboolean initialized
= FALSE
;
16 lttv_iattribute_get_type (void)
18 static GType type
= 0;
20 static const GTypeInfo info
= {
21 sizeof (LttvIAttributeClass
),
22 lttv_iattribute_base_init
, /* base_init */
23 NULL
, /* base_finalize */
24 NULL
, /* class_init */
25 NULL
, /* class_finalize */
26 NULL
, /* class_data */
29 NULL
/* instance_init */
31 type
= g_type_register_static (G_TYPE_INTERFACE
, "LttvIAttribute",
38 unsigned int lttv_iattribute_get_number(LttvIAttribute
*self
)
40 return LTTV_IATTRIBUTE_GET_CLASS (self
)->get_number (self
);
44 gboolean
lttv_iattribute_named(LttvIAttribute
*self
, gboolean
*homogeneous
)
46 return LTTV_IATTRIBUTE_GET_CLASS (self
)->named (self
, homogeneous
);
50 LttvAttributeType
lttv_iattribute_get(LttvIAttribute
*self
, unsigned i
,
51 LttvAttributeName
*name
, LttvAttributeValue
*v
)
53 return LTTV_IATTRIBUTE_GET_CLASS (self
)->get (self
, i
, name
, v
);
57 LttvAttributeType
lttv_iattribute_get_by_name(LttvIAttribute
*self
,
58 LttvAttributeName name
, LttvAttributeValue
*v
)
60 return LTTV_IATTRIBUTE_GET_CLASS (self
)->get_by_name (self
, name
, v
);
64 LttvAttributeValue
lttv_iattribute_add(LttvIAttribute
*self
,
65 LttvAttributeName name
, LttvAttributeType t
)
67 return LTTV_IATTRIBUTE_GET_CLASS (self
)->add (self
, name
, t
);
71 void lttv_iattribute_remove(LttvIAttribute
*self
, unsigned i
)
73 return LTTV_IATTRIBUTE_GET_CLASS (self
)->remove (self
, i
);
77 void lttv_iattribute_remove_by_name(LttvIAttribute
*self
,
78 LttvAttributeName name
)
80 return LTTV_IATTRIBUTE_GET_CLASS (self
)->remove_by_name (self
, name
);
83 LttvIAttribute
* lttv_iattribute_create_subdir(LttvIAttribute
*self
,
84 LttvAttributeName name
)
86 return LTTV_IATTRIBUTE_GET_CLASS (self
)->create_subdir (self
, name
);
90 /* Find the named attribute in the table, which must be of the specified type.
91 If it does not exist, it is created with a default value of 0 (NULL for
92 pointer types). Since the address of the value is obtained, it may be
93 changed easily afterwards. The function returns false when the attribute
94 exists but is of incorrect type. */
96 gboolean
lttv_iattribute_find(LttvIAttribute
*self
, LttvAttributeName name
,
97 LttvAttributeType t
, LttvAttributeValue
*v
)
99 LttvAttributeType found_type
;
101 found_type
= lttv_iattribute_get_by_name(self
, name
, v
);
102 if(found_type
== t
) return TRUE
;
104 if(found_type
== LTTV_NONE
) {
105 *v
= lttv_iattribute_add(self
, name
, t
);
113 /* Trees of attribute tables may be accessed using a hierarchical path with
114 components separated by /, like in filesystems */
116 gboolean
lttv_iattribute_find_by_path(LttvIAttribute
*self
, char *path
,
117 LttvAttributeType t
, LttvAttributeValue
*v
)
119 LttvIAttribute
*node
= self
;
121 LttvAttributeType found_type
;
123 LttvAttributeName name
;
125 gchar
**components
, **cursor
;
127 components
= g_strsplit(path
, "\"", G_MAXINT
);
129 if(components
== NULL
|| *components
== NULL
) {
130 g_strfreev(components
);
134 for(cursor
= components
;;) {
135 name
= g_quark_from_string(*cursor
);
138 if(*cursor
== NULL
) {
139 g_strfreev(components
);
140 return lttv_iattribute_find(node
, name
, t
, v
);
143 found_type
= lttv_iattribute_get_by_name(node
, name
, v
);
144 if(found_type
== LTTV_NONE
) {
145 node
= lttv_iattribute_create_subdir(node
, name
);
147 else if(found_type
== LTTV_GOBJECT
&&
148 LTTV_IS_IATTRIBUTE(*(v
->v_gobject
))) {
149 node
= LTTV_IATTRIBUTE(*(v
->v_gobject
));
152 g_strfreev(components
);
159 /* Shallow and deep copies */
161 LttvIAttribute
*lttv_iattribute_shallow_copy(LttvIAttribute
*self
)
163 LttvIAttribute
*copy
;
167 LttvAttributeValue v
, v_copy
;
169 LttvAttributeName name
;
173 int nb_attributes
= lttv_iattribute_get_number(self
);
175 copy
= LTTV_IATTRIBUTE(g_object_new(G_OBJECT_TYPE(self
),NULL
));
177 for(i
= 0 ; i
< nb_attributes
; i
++) {
178 t
= lttv_iattribute_get(self
, i
, &name
, &v
);
179 v_copy
= lttv_iattribute_add(copy
, name
, t
);
180 lttv_iattribute_copy_value(t
, v_copy
, v
);
184 LttvIAttribute
*lttv_iattribute_deep_copy(LttvIAttribute
*self
)
186 LttvIAttribute
*copy
, *child
;
190 LttvAttributeValue v
, v_copy
;
192 LttvAttributeName name
;
196 int nb_attributes
= lttv_iattribute_get_number(self
);
198 copy
= LTTV_IATTRIBUTE(g_object_new(G_OBJECT_TYPE(self
), NULL
));
200 for(i
= 0 ; i
< nb_attributes
; i
++) {
201 t
= lttv_iattribute_get(self
, i
, &name
, &v
);
202 v_copy
= lttv_iattribute_add(copy
, name
, t
);
203 if(t
== LTTV_GOBJECT
&& LTTV_IS_IATTRIBUTE(*(v
.v_gobject
))) {
204 child
= LTTV_IATTRIBUTE(*(v
.v_gobject
));
205 *(v_copy
.v_gobject
) = G_OBJECT(lttv_iattribute_deep_copy(child
));
207 else lttv_iattribute_copy_value(t
, v_copy
, v
);
211 void lttv_iattribute_copy_value(LttvAttributeType t
, LttvAttributeValue dest
,
212 LttvAttributeValue src
)
216 *(dest
.v_int
) = *(src
.v_int
);
220 *(dest
.v_uint
) = *(src
.v_uint
);
224 *(dest
.v_long
) = *(src
.v_long
);
228 *(dest
.v_ulong
) = *(src
.v_ulong
);
232 *(dest
.v_float
) = *(src
.v_float
);
236 *(dest
.v_double
) = *(src
.v_double
);
240 *(dest
.v_time
) = *(src
.v_time
);
244 *(dest
.v_pointer
) = *(src
.v_pointer
);
248 *(dest
.v_string
) = *(src
.v_string
);
252 *(dest
.v_gobject
) = *(src
.v_gobject
);