Debian Codegraph @

src/libsysprof/sysprof-document-counter-value.c

raw
1 /* sysprof-document-counter-value.c
2 *
3 * Copyright 2023 Christian Hergert <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #include "config.h"
22
23 #include "sysprof-document-counter-private.h"
24 #include "sysprof-document-counter-value-private.h"
25
26 struct _SysprofDocumentCounterValue
27 {
28 GObject parent_instance;
29 SysprofDocumentCounter *counter;
30 SysprofDocumentTimedValue value;
31 guint type;
32 };
33
34 enum {
35 PROP_0,
36 PROP_COUNTER,
37 PROP_TIME,
38 PROP_TIME_OFFSET,
39 PROP_VALUE_DOUBLE,
40 PROP_VALUE_INT64,
41 PROP_VALUE_STRING,
42 N_PROPS
43 };
44
45 G_DEFINE_FINAL_TYPE (SysprofDocumentCounterValue, sysprof_document_counter_value, G_TYPE_OBJECT)
46
47 static GParamSpec *properties [N_PROPS];
48
49 static void
50 sysprof_document_counter_value_get_property (GObject *object,
51 guint prop_id,
52 GValue *value,
53 GParamSpec *pspec)
54 {
55 SysprofDocumentCounterValue *self = SYSPROF_DOCUMENT_COUNTER_VALUE (object);
56
57 switch (prop_id)
58 {
59 case PROP_COUNTER:
60 g_value_set_object (value, sysprof_document_counter_value_get_counter (self));
61 break;
62
63 case PROP_TIME:
64 g_value_set_int64 (value, sysprof_document_counter_value_get_time (self));
65 break;
66
67 case PROP_TIME_OFFSET:
68 g_value_set_int64 (value, sysprof_document_counter_value_get_time_offset (self));
69 break;
70
71 case PROP_VALUE_DOUBLE:
72 g_value_set_double (value, sysprof_document_counter_value_get_value_double (self));
73 break;
74
75 case PROP_VALUE_INT64:
76 g_value_set_int64 (value, sysprof_document_counter_value_get_value_int64 (self));
77 break;
78
79 case PROP_VALUE_STRING:
80 g_value_take_string (value, sysprof_document_counter_value_format (self));
81 break;
82
83 default:
84 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
85 }
86 }
87
88 static void
89 sysprof_document_counter_value_class_init (SysprofDocumentCounterValueClass *klass)
90 {
91 GObjectClass *object_class = G_OBJECT_CLASS (klass);
92
93 object_class->get_property = sysprof_document_counter_value_get_property;
94
95 properties [PROP_COUNTER] =
96 g_param_spec_object ("counter", NULL, NULL,
97 SYSPROF_TYPE_DOCUMENT_COUNTER,
98 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
99
100 properties [PROP_TIME] =
101 g_param_spec_int64 ("time", NULL, NULL,
102 G_MININT64, G_MAXINT64, 0,
103 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
104
105 properties [PROP_TIME_OFFSET] =
106 g_param_spec_int64 ("time-offset", NULL, NULL,
107 G_MININT64, G_MAXINT64, 0,
108 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
109
110 properties[PROP_VALUE_DOUBLE] =
111 g_param_spec_double ("value-double", NULL, NULL,
112 -G_MAXDOUBLE, G_MAXDOUBLE, 0,
113 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
114
115 properties[PROP_VALUE_INT64] =
116 g_param_spec_int64 ("value-int64", NULL, NULL,
117 G_MININT64, G_MAXINT64, 0,
118 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
119
120 properties[PROP_VALUE_STRING] =
121 g_param_spec_string ("value-string", NULL, NULL,
122 NULL,
123 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
124
125 g_object_class_install_properties (object_class, N_PROPS, properties);
126 }
127
128 static void
129 sysprof_document_counter_value_init (SysprofDocumentCounterValue *self)
130 {
131 }
132
133 SysprofDocumentCounterValue *
134 _sysprof_document_counter_value_new (guint type,
135 const SysprofDocumentTimedValue *value,
136 SysprofDocumentCounter *counter)
137 {
138 SysprofDocumentCounterValue *self;
139
140 self = g_object_new (SYSPROF_TYPE_DOCUMENT_COUNTER_VALUE, NULL);
141 self->type = type;
142 self->value = *value;
143 self->counter = g_object_ref (counter);
144
145 return self;
146 }
147
148 gint64
149 sysprof_document_counter_value_get_time (SysprofDocumentCounterValue *self)
150 {
151 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_COUNTER_VALUE (self), 0);
152
153 return self->value.time;
154 }
155
156 gint64
157 sysprof_document_counter_value_get_time_offset (SysprofDocumentCounterValue *self)
158 {
159 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_COUNTER_VALUE (self), 0);
160
161 return self->value.time - self->counter->begin_time;
162 }
163
164 void
165 sysprof_document_counter_value_get_value (SysprofDocumentCounterValue *self,
166 GValue *value)
167 {
168 g_return_if_fail (SYSPROF_IS_DOCUMENT_COUNTER_VALUE (self));
169 g_return_if_fail (G_IS_VALUE (value));
170
171 if (G_VALUE_HOLDS_INT64 (value))
172 g_value_set_int64 (value, self->value.v_int64);
173 else if (G_VALUE_HOLDS_DOUBLE (value))
174 g_value_set_double (value, self->value.v_double);
175 else
176 g_warning_once ("Unsupported value type %s", G_VALUE_TYPE_NAME (value));
177 }
178
179 gint64
180 sysprof_document_counter_value_get_value_int64 (SysprofDocumentCounterValue *self)
181 {
182 if (self->type == SYSPROF_CAPTURE_COUNTER_INT64)
183 return self->value.v_int64;
184 else
185 return (gint64)self->value.v_double;
186 }
187
188 double
189 sysprof_document_counter_value_get_value_double (SysprofDocumentCounterValue *self)
190 {
191 if (self->type == SYSPROF_CAPTURE_COUNTER_DOUBLE)
192 return self->value.v_double;
193 else
194 return (double)self->value.v_int64;
195 }
196
197 char *
198 sysprof_document_counter_value_format (SysprofDocumentCounterValue *self)
199 {
200 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_COUNTER_VALUE (self), NULL);
201
202 if (self->type == SYSPROF_CAPTURE_COUNTER_DOUBLE)
203 return g_strdup_printf ("%lf", self->value.v_double);
204 else
205 return g_strdup_printf ("%"G_GINT64_FORMAT, self->value.v_int64);
206 }
207
208 /**
209 * sysprof_document_counter_value_get_counter:
210 * @self: a #SysprofDocumentCounterValue
211 *
212 * Returns: (transfer none): a #SysprofDocumentCounter
213 */
214 SysprofDocumentCounter *
215 sysprof_document_counter_value_get_counter (SysprofDocumentCounterValue *self)
216 {
217 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_COUNTER_VALUE (self), NULL);
218
219 return self->counter;
220 }