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
)
74 return LTTV_IATTRIBUTE_GET_CLASS (self
)->get (self
, i
, name
, v
);
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
);
92 void lttv_iattribute_remove(LttvIAttribute
*self
, unsigned i
)
94 return LTTV_IATTRIBUTE_GET_CLASS (self
)->remove (self
, i
);
98 void lttv_iattribute_remove_by_name(LttvIAttribute
*self
,
99 LttvAttributeName name
)
101 return LTTV_IATTRIBUTE_GET_CLASS (self
)->remove_by_name (self
, name
);
104 LttvIAttribute
* lttv_iattribute_find_subdir(LttvIAttribute
*self
,
105 LttvAttributeName name
)
107 return LTTV_IATTRIBUTE_GET_CLASS (self
)->find_subdir (self
, name
);
111 /* Find the named attribute in the table, which must be of the specified type.
112 If it does not exist, it is created with a default value of 0 (NULL for
113 pointer types). Since the address of the value is obtained, it may be
114 changed easily afterwards. The function returns false when the attribute
115 exists but is of incorrect type. */
117 gboolean
lttv_iattribute_find(LttvIAttribute
*self
, LttvAttributeName name
,
118 LttvAttributeType t
, LttvAttributeValue
*v
)
120 LttvAttributeType found_type
;
122 found_type
= lttv_iattribute_get_by_name(self
, name
, v
);
123 if(found_type
== t
) return TRUE
;
125 if(found_type
== LTTV_NONE
) {
126 *v
= lttv_iattribute_add(self
, name
, t
);
134 /* Trees of attribute tables may be accessed using a hierarchical path with
135 components separated by /, like in filesystems */
137 gboolean
lttv_iattribute_find_by_path(LttvIAttribute
*self
, char *path
,
138 LttvAttributeType t
, LttvAttributeValue
*v
)
140 LttvIAttribute
*node
= self
;
142 LttvAttributeType found_type
;
144 LttvAttributeName name
;
146 gchar
**components
, **cursor
;
148 components
= g_strsplit(path
, "\"", G_MAXINT
);
150 if(components
== NULL
|| *components
== NULL
) {
151 g_strfreev(components
);
155 for(cursor
= components
;;) {
156 name
= g_quark_from_string(*cursor
);
159 if(*cursor
== NULL
) {
160 g_strfreev(components
);
161 return lttv_iattribute_find(node
, name
, t
, v
);
164 found_type
= lttv_iattribute_get_by_name(node
, name
, v
);
165 if(found_type
== LTTV_NONE
) {
166 node
= lttv_iattribute_find_subdir(node
, name
);
168 else if(found_type
== LTTV_GOBJECT
&&
169 LTTV_IS_IATTRIBUTE(*(v
->v_gobject
))) {
170 node
= LTTV_IATTRIBUTE(*(v
->v_gobject
));
173 g_strfreev(components
);
181 /* Shallow and deep copies */
183 LttvIAttribute
*lttv_iattribute_shallow_copy(LttvIAttribute
*self
)
185 LttvIAttribute
*copy
;
189 LttvAttributeValue v
, v_copy
;
191 LttvAttributeName name
;
195 int nb_attributes
= lttv_iattribute_get_number(self
);
197 copy
= LTTV_IATTRIBUTE_GET_CLASS(self
)->new_attribute(NULL
);
199 for(i
= 0 ; i
< nb_attributes
; i
++) {
200 t
= lttv_iattribute_get(self
, i
, &name
, &v
);
201 v_copy
= lttv_iattribute_add(copy
, name
, t
);
202 lttv_iattribute_copy_value(t
, v_copy
, v
);
207 LttvIAttribute
*lttv_iattribute_deep_copy(LttvIAttribute
*self
)
209 LttvIAttribute
*copy
, *child
;
213 LttvAttributeValue v
, v_copy
;
215 LttvAttributeName name
;
219 int nb_attributes
= lttv_iattribute_get_number(self
);
221 copy
= LTTV_IATTRIBUTE_GET_CLASS(self
)->new_attribute(NULL
);
223 for(i
= 0 ; i
< nb_attributes
; i
++) {
224 t
= lttv_iattribute_get(self
, i
, &name
, &v
);
225 v_copy
= lttv_iattribute_add(copy
, name
, t
);
226 if(t
== LTTV_GOBJECT
&& LTTV_IS_IATTRIBUTE(*(v
.v_gobject
))) {
227 child
= LTTV_IATTRIBUTE(*(v
.v_gobject
));
228 *(v_copy
.v_gobject
) = G_OBJECT(lttv_iattribute_deep_copy(child
));
230 else lttv_iattribute_copy_value(t
, v_copy
, v
);
235 void lttv_iattribute_copy_value(LttvAttributeType t
, LttvAttributeValue dest
,
236 LttvAttributeValue src
)
240 *(dest
.v_int
) = *(src
.v_int
);
244 *(dest
.v_uint
) = *(src
.v_uint
);
248 *(dest
.v_long
) = *(src
.v_long
);
252 *(dest
.v_ulong
) = *(src
.v_ulong
);
256 *(dest
.v_float
) = *(src
.v_float
);
260 *(dest
.v_double
) = *(src
.v_double
);
264 *(dest
.v_time
) = *(src
.v_time
);
268 *(dest
.v_pointer
) = *(src
.v_pointer
);
272 *(dest
.v_string
) = *(src
.v_string
);
276 *(dest
.v_gobject
) = *(src
.v_gobject
);
277 if(*(dest
.v_gobject
) != NULL
) g_object_ref(*(dest
.v_gobject
));