1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 #include <lttv/iattribute.h>
26 lttv_iattribute_base_init (gpointer klass
)
28 static gboolean initialized
= FALSE
;
37 lttv_iattribute_get_type (void)
39 static GType type
= 0;
41 static const GTypeInfo info
= {
42 sizeof (LttvIAttributeClass
),
43 lttv_iattribute_base_init
, /* base_init */
44 NULL
, /* base_finalize */
45 NULL
, /* class_init */
46 NULL
, /* class_finalize */
47 NULL
, /* class_data */
50 NULL
/* instance_init */
52 type
= g_type_register_static (G_TYPE_INTERFACE
, "LttvIAttribute",
59 unsigned int lttv_iattribute_get_number(LttvIAttribute
*self
)
61 return LTTV_IATTRIBUTE_GET_CLASS (self
)->get_number (self
);
65 gboolean
lttv_iattribute_named(LttvIAttribute
*self
, gboolean
*homogeneous
)
67 return LTTV_IATTRIBUTE_GET_CLASS (self
)->named (self
, homogeneous
);
71 LttvAttributeType
lttv_iattribute_get(LttvIAttribute
*self
, unsigned i
,
72 LttvAttributeName
*name
, LttvAttributeValue
*v
, gboolean
*is_named
)
74 return LTTV_IATTRIBUTE_GET_CLASS (self
)->get (self
, i
, name
, v
, is_named
);
78 LttvAttributeType
lttv_iattribute_get_by_name(LttvIAttribute
*self
,
79 LttvAttributeName name
, LttvAttributeValue
*v
)
81 return LTTV_IATTRIBUTE_GET_CLASS (self
)->get_by_name (self
, name
, v
);
85 LttvAttributeValue
lttv_iattribute_add(LttvIAttribute
*self
,
86 LttvAttributeName name
, LttvAttributeType t
)
88 return LTTV_IATTRIBUTE_GET_CLASS (self
)->add (self
, name
, t
);
91 LttvAttributeValue
lttv_iattribute_add_unnamed(LttvIAttribute
*self
,
92 LttvAttributeName name
, LttvAttributeType t
)
94 return LTTV_IATTRIBUTE_GET_CLASS (self
)->add_unnamed (self
, name
, t
);
97 void lttv_iattribute_remove(LttvIAttribute
*self
, unsigned i
)
99 return LTTV_IATTRIBUTE_GET_CLASS (self
)->remove (self
, i
);
103 void lttv_iattribute_remove_by_name(LttvIAttribute
*self
,
104 LttvAttributeName name
)
106 return LTTV_IATTRIBUTE_GET_CLASS (self
)->remove_by_name (self
, name
);
109 LttvIAttribute
* lttv_iattribute_find_subdir(LttvIAttribute
*self
,
110 LttvAttributeName name
)
112 return LTTV_IATTRIBUTE_GET_CLASS (self
)->find_subdir (self
, name
);
115 LttvIAttribute
* lttv_iattribute_find_subdir_unnamed(LttvIAttribute
*self
,
116 LttvAttributeName name
)
118 return LTTV_IATTRIBUTE_GET_CLASS (self
)->find_subdir_unnamed (self
, name
);
123 /* Find the named attribute in the table, which must be of the specified type.
124 If it does not exist, it is created with a default value of 0 (NULL for
125 pointer types). Since the address of the value is obtained, it may be
126 changed easily afterwards. The function returns false when the attribute
127 exists but is of incorrect type. */
129 gboolean
lttv_iattribute_find(LttvIAttribute
*self
, LttvAttributeName name
,
130 LttvAttributeType t
, LttvAttributeValue
*v
)
132 LttvAttributeType found_type
;
134 found_type
= lttv_iattribute_get_by_name(self
, name
, v
);
135 if(found_type
== t
) return TRUE
;
137 if(found_type
== LTTV_NONE
) {
138 *v
= lttv_iattribute_add(self
, name
, t
);
146 /* Trees of attribute tables may be accessed using a hierarchical path with
147 components separated by /, like in filesystems */
149 gboolean
lttv_iattribute_find_by_path(LttvIAttribute
*self
, const char *path
,
150 LttvAttributeType t
, LttvAttributeValue
*v
)
152 LttvIAttribute
*node
= self
;
154 LttvAttributeType found_type
;
156 LttvAttributeName name
;
158 gchar
**components
, **cursor
;
160 components
= g_strsplit(path
, "\"", G_MAXINT
);
162 if(components
== NULL
|| *components
== NULL
) {
163 g_strfreev(components
);
167 for(cursor
= components
;;) {
168 name
= g_quark_from_string(*cursor
);
171 if(*cursor
== NULL
) {
172 g_strfreev(components
);
173 return lttv_iattribute_find(node
, name
, t
, v
);
176 found_type
= lttv_iattribute_get_by_name(node
, name
, v
);
177 if(found_type
== LTTV_NONE
) {
178 node
= lttv_iattribute_find_subdir(node
, name
);
180 else if(found_type
== LTTV_GOBJECT
&&
181 LTTV_IS_IATTRIBUTE(*(v
->v_gobject
))) {
182 node
= LTTV_IATTRIBUTE(*(v
->v_gobject
));
185 g_strfreev(components
);
193 /* Shallow and deep copies */
195 LttvIAttribute
*lttv_iattribute_shallow_copy(LttvIAttribute
*self
)
197 LttvIAttribute
*copy
;
201 LttvAttributeValue v
, v_copy
;
203 LttvAttributeName name
;
209 int nb_attributes
= lttv_iattribute_get_number(self
);
211 copy
= LTTV_IATTRIBUTE_GET_CLASS(self
)->new_attribute(NULL
);
213 for(i
= 0 ; i
< nb_attributes
; i
++) {
214 t
= lttv_iattribute_get(self
, i
, &name
, &v
, &is_named
);
216 v_copy
= lttv_iattribute_add(copy
, name
, t
);
218 v_copy
= lttv_iattribute_add_unnamed(copy
, name
, t
);
219 lttv_iattribute_copy_value(t
, v_copy
, v
);
224 LttvIAttribute
*lttv_iattribute_deep_copy(LttvIAttribute
*self
)
226 LttvIAttribute
*copy
, *child
;
230 LttvAttributeValue v
, v_copy
;
232 LttvAttributeName name
;
238 int nb_attributes
= lttv_iattribute_get_number(self
);
240 copy
= LTTV_IATTRIBUTE_GET_CLASS(self
)->new_attribute(NULL
);
242 for(i
= 0 ; i
< nb_attributes
; i
++) {
243 t
= lttv_iattribute_get(self
, i
, &name
, &v
, &is_named
);
245 v_copy
= lttv_iattribute_add(copy
, name
, t
);
247 v_copy
= lttv_iattribute_add_unnamed(copy
, name
, t
);
248 if(t
== LTTV_GOBJECT
&& LTTV_IS_IATTRIBUTE(*(v
.v_gobject
))) {
249 child
= LTTV_IATTRIBUTE(*(v
.v_gobject
));
250 *(v_copy
.v_gobject
) = G_OBJECT(lttv_iattribute_deep_copy(child
));
252 else lttv_iattribute_copy_value(t
, v_copy
, v
);
257 void lttv_iattribute_copy_value(LttvAttributeType t
, LttvAttributeValue dest
,
258 LttvAttributeValue src
)
262 *(dest
.v_int
) = *(src
.v_int
);
266 *(dest
.v_uint
) = *(src
.v_uint
);
270 *(dest
.v_long
) = *(src
.v_long
);
274 *(dest
.v_ulong
) = *(src
.v_ulong
);
278 *(dest
.v_float
) = *(src
.v_float
);
282 *(dest
.v_double
) = *(src
.v_double
);
286 *(dest
.v_time
) = *(src
.v_time
);
290 *(dest
.v_pointer
) = *(src
.v_pointer
);
294 *(dest
.v_string
) = *(src
.v_string
);
298 *(dest
.v_gobject
) = *(src
.v_gobject
);
299 if(*(dest
.v_gobject
) != NULL
) g_object_ref(*(dest
.v_gobject
));