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,
20 #include <lttv/iattribute.h>
23 lttv_iattribute_base_init (gpointer klass
)
25 static gboolean initialized
= FALSE
;
34 lttv_iattribute_get_type (void)
36 static GType type
= 0;
38 static const GTypeInfo info
= {
39 sizeof (LttvIAttributeClass
),
40 lttv_iattribute_base_init
, /* base_init */
41 NULL
, /* base_finalize */
42 NULL
, /* class_init */
43 NULL
, /* class_finalize */
44 NULL
, /* class_data */
47 NULL
/* instance_init */
49 type
= g_type_register_static (G_TYPE_INTERFACE
, "LttvIAttribute",
56 unsigned int lttv_iattribute_get_number(LttvIAttribute
*self
)
58 return LTTV_IATTRIBUTE_GET_CLASS (self
)->get_number (self
);
62 gboolean
lttv_iattribute_named(LttvIAttribute
*self
, gboolean
*homogeneous
)
64 return LTTV_IATTRIBUTE_GET_CLASS (self
)->named (self
, homogeneous
);
68 LttvAttributeType
lttv_iattribute_get(LttvIAttribute
*self
, unsigned i
,
69 LttvAttributeName
*name
, LttvAttributeValue
*v
)
71 return LTTV_IATTRIBUTE_GET_CLASS (self
)->get (self
, i
, name
, v
);
75 LttvAttributeType
lttv_iattribute_get_by_name(LttvIAttribute
*self
,
76 LttvAttributeName name
, LttvAttributeValue
*v
)
78 return LTTV_IATTRIBUTE_GET_CLASS (self
)->get_by_name (self
, name
, v
);
82 LttvAttributeValue
lttv_iattribute_add(LttvIAttribute
*self
,
83 LttvAttributeName name
, LttvAttributeType t
)
85 return LTTV_IATTRIBUTE_GET_CLASS (self
)->add (self
, name
, t
);
89 void lttv_iattribute_remove(LttvIAttribute
*self
, unsigned i
)
91 return LTTV_IATTRIBUTE_GET_CLASS (self
)->remove (self
, i
);
95 void lttv_iattribute_remove_by_name(LttvIAttribute
*self
,
96 LttvAttributeName name
)
98 return LTTV_IATTRIBUTE_GET_CLASS (self
)->remove_by_name (self
, name
);
101 LttvIAttribute
* lttv_iattribute_find_subdir(LttvIAttribute
*self
,
102 LttvAttributeName name
)
104 return LTTV_IATTRIBUTE_GET_CLASS (self
)->find_subdir (self
, name
);
108 /* Find the named attribute in the table, which must be of the specified type.
109 If it does not exist, it is created with a default value of 0 (NULL for
110 pointer types). Since the address of the value is obtained, it may be
111 changed easily afterwards. The function returns false when the attribute
112 exists but is of incorrect type. */
114 gboolean
lttv_iattribute_find(LttvIAttribute
*self
, LttvAttributeName name
,
115 LttvAttributeType t
, LttvAttributeValue
*v
)
117 LttvAttributeType found_type
;
119 found_type
= lttv_iattribute_get_by_name(self
, name
, v
);
120 if(found_type
== t
) return TRUE
;
122 if(found_type
== LTTV_NONE
) {
123 *v
= lttv_iattribute_add(self
, name
, t
);
131 /* Trees of attribute tables may be accessed using a hierarchical path with
132 components separated by /, like in filesystems */
134 gboolean
lttv_iattribute_find_by_path(LttvIAttribute
*self
, char *path
,
135 LttvAttributeType t
, LttvAttributeValue
*v
)
137 LttvIAttribute
*node
= self
;
139 LttvAttributeType found_type
;
141 LttvAttributeName name
;
143 gchar
**components
, **cursor
;
145 components
= g_strsplit(path
, "\"", G_MAXINT
);
147 if(components
== NULL
|| *components
== NULL
) {
148 g_strfreev(components
);
152 for(cursor
= components
;;) {
153 name
= g_quark_from_string(*cursor
);
156 if(*cursor
== NULL
) {
157 g_strfreev(components
);
158 return lttv_iattribute_find(node
, name
, t
, v
);
161 found_type
= lttv_iattribute_get_by_name(node
, name
, v
);
162 if(found_type
== LTTV_NONE
) {
163 node
= lttv_iattribute_find_subdir(node
, name
);
165 else if(found_type
== LTTV_GOBJECT
&&
166 LTTV_IS_IATTRIBUTE(*(v
->v_gobject
))) {
167 node
= LTTV_IATTRIBUTE(*(v
->v_gobject
));
170 g_strfreev(components
);
177 /* Shallow and deep copies */
179 LttvIAttribute
*lttv_iattribute_shallow_copy(LttvIAttribute
*self
)
181 LttvIAttribute
*copy
;
185 LttvAttributeValue v
, v_copy
;
187 LttvAttributeName name
;
191 int nb_attributes
= lttv_iattribute_get_number(self
);
193 copy
= LTTV_IATTRIBUTE(g_object_new(G_OBJECT_TYPE(self
),NULL
));
195 for(i
= 0 ; i
< nb_attributes
; i
++) {
196 t
= lttv_iattribute_get(self
, i
, &name
, &v
);
197 v_copy
= lttv_iattribute_add(copy
, name
, t
);
198 lttv_iattribute_copy_value(t
, v_copy
, v
);
202 LttvIAttribute
*lttv_iattribute_deep_copy(LttvIAttribute
*self
)
204 LttvIAttribute
*copy
, *child
;
208 LttvAttributeValue v
, v_copy
;
210 LttvAttributeName name
;
214 int nb_attributes
= lttv_iattribute_get_number(self
);
216 copy
= LTTV_IATTRIBUTE(g_object_new(G_OBJECT_TYPE(self
), NULL
));
218 for(i
= 0 ; i
< nb_attributes
; i
++) {
219 t
= lttv_iattribute_get(self
, i
, &name
, &v
);
220 v_copy
= lttv_iattribute_add(copy
, name
, t
);
221 if(t
== LTTV_GOBJECT
&& LTTV_IS_IATTRIBUTE(*(v
.v_gobject
))) {
222 child
= LTTV_IATTRIBUTE(*(v
.v_gobject
));
223 *(v_copy
.v_gobject
) = G_OBJECT(lttv_iattribute_deep_copy(child
));
225 else lttv_iattribute_copy_value(t
, v_copy
, v
);
229 void lttv_iattribute_copy_value(LttvAttributeType t
, LttvAttributeValue dest
,
230 LttvAttributeValue src
)
234 *(dest
.v_int
) = *(src
.v_int
);
238 *(dest
.v_uint
) = *(src
.v_uint
);
242 *(dest
.v_long
) = *(src
.v_long
);
246 *(dest
.v_ulong
) = *(src
.v_ulong
);
250 *(dest
.v_float
) = *(src
.v_float
);
254 *(dest
.v_double
) = *(src
.v_double
);
258 *(dest
.v_time
) = *(src
.v_time
);
262 *(dest
.v_pointer
) = *(src
.v_pointer
);
266 *(dest
.v_string
) = *(src
.v_string
);
270 *(dest
.v_gobject
) = *(src
.v_gobject
);