1 /* ide-debugger-thread.c
2  *
3  * Copyright 2017-2019 Christian Hergert <chergert@redhat.com>
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 #define G_LOG_DOMAIN "ide-debugger-thread"
22 
23 #include "config.h"
24 
25 #include "ide-debugger-thread.h"
26 
27 typedef struct
28 {
29   gchar *id;
30   gchar *group;
31 } IdeDebuggerThreadPrivate;
32 
33 enum {
34   PROP_0,
35   PROP_ID,
36   PROP_GROUP,
37   N_PROPS
38 };
39 
G_DEFINE_TYPE_WITH_PRIVATE(IdeDebuggerThread,ide_debugger_thread,G_TYPE_OBJECT)40 G_DEFINE_TYPE_WITH_PRIVATE (IdeDebuggerThread, ide_debugger_thread, G_TYPE_OBJECT)
41 
42 static GParamSpec *properties [N_PROPS];
43 
44 static void
45 ide_debugger_thread_finalize (GObject *object)
46 {
47   IdeDebuggerThread *self = (IdeDebuggerThread *)object;
48   IdeDebuggerThreadPrivate *priv = ide_debugger_thread_get_instance_private (self);
49 
50   g_clear_pointer (&priv->id, g_free);
51   g_clear_pointer (&priv->group, g_free);
52 
53   G_OBJECT_CLASS (ide_debugger_thread_parent_class)->finalize (object);
54 }
55 
56 static void
ide_debugger_thread_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)57 ide_debugger_thread_get_property (GObject    *object,
58                                   guint       prop_id,
59                                   GValue     *value,
60                                   GParamSpec *pspec)
61 {
62   IdeDebuggerThread *self = IDE_DEBUGGER_THREAD (object);
63 
64   switch (prop_id)
65     {
66     case PROP_ID:
67       g_value_set_string (value, ide_debugger_thread_get_id (self));
68       break;
69 
70     case PROP_GROUP:
71       g_value_set_string (value, ide_debugger_thread_get_group (self));
72       break;
73 
74     default:
75       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
76     }
77 }
78 
79 static void
ide_debugger_thread_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)80 ide_debugger_thread_set_property (GObject      *object,
81                                   guint         prop_id,
82                                   const GValue *value,
83                                   GParamSpec   *pspec)
84 {
85   IdeDebuggerThread *self = IDE_DEBUGGER_THREAD (object);
86   IdeDebuggerThreadPrivate *priv = ide_debugger_thread_get_instance_private (self);
87 
88   switch (prop_id)
89     {
90     case PROP_ID:
91       priv->id = g_value_dup_string (value);
92       break;
93 
94     case PROP_GROUP:
95       ide_debugger_thread_set_group (self, g_value_get_string (value));
96       break;
97 
98     default:
99       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
100     }
101 }
102 
103 static void
ide_debugger_thread_class_init(IdeDebuggerThreadClass * klass)104 ide_debugger_thread_class_init (IdeDebuggerThreadClass *klass)
105 {
106   GObjectClass *object_class = G_OBJECT_CLASS (klass);
107 
108   object_class->finalize = ide_debugger_thread_finalize;
109   object_class->get_property = ide_debugger_thread_get_property;
110   object_class->set_property = ide_debugger_thread_set_property;
111 
112   properties [PROP_ID] =
113     g_param_spec_string ("id",
114                          "Id",
115                          "The thread identifier",
116                          NULL,
117                          (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
118 
119   properties [PROP_GROUP] =
120     g_param_spec_string ("group",
121                          "Group",
122                          "The thread group, if any",
123                          NULL,
124                          (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
125 
126   g_object_class_install_properties (object_class, N_PROPS, properties);
127 }
128 
129 static void
ide_debugger_thread_init(IdeDebuggerThread * self)130 ide_debugger_thread_init (IdeDebuggerThread *self)
131 {
132 }
133 
134 IdeDebuggerThread *
ide_debugger_thread_new(const gchar * id)135 ide_debugger_thread_new (const gchar *id)
136 {
137   return g_object_new (IDE_TYPE_DEBUGGER_THREAD,
138                        "id", id,
139                        NULL);
140 }
141 
142 const gchar *
ide_debugger_thread_get_id(IdeDebuggerThread * self)143 ide_debugger_thread_get_id (IdeDebuggerThread *self)
144 {
145   IdeDebuggerThreadPrivate *priv = ide_debugger_thread_get_instance_private (self);
146 
147   g_return_val_if_fail (IDE_IS_DEBUGGER_THREAD (self), NULL);
148 
149   return priv->id;
150 }
151 
152 const gchar *
ide_debugger_thread_get_group(IdeDebuggerThread * self)153 ide_debugger_thread_get_group (IdeDebuggerThread *self)
154 {
155   IdeDebuggerThreadPrivate *priv = ide_debugger_thread_get_instance_private (self);
156 
157   g_return_val_if_fail (IDE_IS_DEBUGGER_THREAD (self), NULL);
158 
159   return priv->group;
160 }
161 
162 void
ide_debugger_thread_set_group(IdeDebuggerThread * self,const gchar * group)163 ide_debugger_thread_set_group (IdeDebuggerThread *self,
164                                const gchar       *group)
165 {
166   IdeDebuggerThreadPrivate *priv = ide_debugger_thread_get_instance_private (self);
167 
168   g_return_if_fail (IDE_IS_DEBUGGER_THREAD (self));
169 
170   if (g_strcmp0 (priv->group, group) != 0)
171     {
172       g_free (priv->group);
173       priv->group = g_strdup (group);
174       g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_GROUP]);
175     }
176 }
177 
178 gint
ide_debugger_thread_compare(IdeDebuggerThread * a,IdeDebuggerThread * b)179 ide_debugger_thread_compare (IdeDebuggerThread *a,
180                              IdeDebuggerThread *b)
181 {
182   IdeDebuggerThreadPrivate *priv_a = ide_debugger_thread_get_instance_private (a);
183   IdeDebuggerThreadPrivate *priv_b = ide_debugger_thread_get_instance_private (b);
184 
185   g_return_val_if_fail (IDE_IS_DEBUGGER_THREAD (a), 0);
186   g_return_val_if_fail (IDE_IS_DEBUGGER_THREAD (b), 0);
187 
188   if (priv_a->id && priv_b->id)
189     {
190       if (g_ascii_isdigit (*priv_a->id) && g_ascii_isdigit (*priv_b->id))
191         return g_ascii_strtoll (priv_a->id, NULL, 10) -
192                g_ascii_strtoll (priv_b->id, NULL, 10);
193     }
194 
195   return g_strcmp0 (priv_a->id, priv_b->id);
196 }
197