File: foundry-intent.c

package info (click to toggle)
foundry 1.1~beta-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,552 kB
  • sloc: ansic: 167,487; xml: 417; makefile: 21; sh: 19; javascript: 10
file content (350 lines) | stat: -rw-r--r-- 9,578 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* foundry-intent.c
 *
 * Copyright 2025 Christian Hergert
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include <gobject/gvaluecollector.h>

#include "foundry-intent.h"

/**
 * FoundryIntent:
 *
 * Abstract base class for representing intents.
 *
 * FoundryIntent provides the core interface for representing user intents
 * and actions within the development environment. It supports attribute
 * storage and provides a unified interface for intent handling across
 * different parts of the development environment.
 */

typedef struct
{
  GHashTable *attributes;
} FoundryIntentPrivate;

G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (FoundryIntent, foundry_intent, G_TYPE_OBJECT)

static void
_g_value_free (gpointer data)
{
  GValue *value = data;

  g_value_unset (value);
  g_free (value);
}

static void
foundry_intent_finalize (GObject *object)
{
  FoundryIntent *self = (FoundryIntent *)object;
  FoundryIntentPrivate *priv = foundry_intent_get_instance_private (self);

  g_clear_pointer (&priv->attributes, g_hash_table_unref);

  G_OBJECT_CLASS (foundry_intent_parent_class)->finalize (object);
}

static void
foundry_intent_class_init (FoundryIntentClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = foundry_intent_finalize;
}

static void
foundry_intent_init (FoundryIntent *self)
{
  FoundryIntentPrivate *priv = foundry_intent_get_instance_private (self);

  priv->attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, _g_value_free);
}

/**
 * foundry_intent_has_attribute:
 * @self: a [class@Foundry.Intent]
 * @attribute: the attribute name to check
 *
 * Checks if the intent has an attribute with the given name.
 *
 * Returns: %TRUE if the attribute exists, %FALSE otherwise
 *
 * Since: 1.1
 */
gboolean
foundry_intent_has_attribute (FoundryIntent *self,
                              const char    *attribute)
{
  FoundryIntentPrivate *priv = foundry_intent_get_instance_private (self);

  g_return_val_if_fail (FOUNDRY_IS_INTENT (self), FALSE);
  g_return_val_if_fail (attribute != NULL, FALSE);

  return g_hash_table_contains (priv->attributes, attribute);
}

/**
 * foundry_intent_set_attribute_value:
 * @self: a [class@Foundry.Intent]
 * @attribute: the attribute name
 * @value: the #GValue to set
 *
 * Sets an attribute value for the intent. The value is copied.
 *
 * Since: 1.1
 */
void
foundry_intent_set_attribute_value (FoundryIntent *self,
                                    const char    *attribute,
                                    const GValue  *value)
{
  FoundryIntentPrivate *priv = foundry_intent_get_instance_private (self);
  GValue *copy;

  g_return_if_fail (FOUNDRY_IS_INTENT (self));
  g_return_if_fail (attribute != NULL);
  g_return_if_fail (value != NULL);

  copy = g_new0 (GValue, 1);
  g_value_init (copy, G_VALUE_TYPE (value));
  g_value_copy (value, copy);

  g_hash_table_insert (priv->attributes, g_strdup (attribute), copy);
}

/**
 * foundry_intent_get_attribute_type:
 * @self: a [class@Foundry.Intent]
 * @attribute: the attribute name
 *
 * Gets the #GType of the attribute value.
 *
 * Returns: the #GType of the attribute, or %G_TYPE_INVALID if not found
 *
 * Since: 1.1
 */
GType
foundry_intent_get_attribute_type (FoundryIntent *self,
                                   const char    *attribute)
{
  FoundryIntentPrivate *priv = foundry_intent_get_instance_private (self);
  GValue *value;

  g_return_val_if_fail (FOUNDRY_IS_INTENT (self), G_TYPE_INVALID);
  g_return_val_if_fail (attribute != NULL, G_TYPE_INVALID);

  if (!(value = g_hash_table_lookup (priv->attributes, attribute)))
    return G_TYPE_INVALID;

  return G_VALUE_TYPE (value);
}

/**
 * foundry_intent_get_attribute_value:
 * @self: a [class@Foundry.Intent]
 * @attribute: the attribute name
 *
 * Gets the attribute value. The returned value is owned by the intent
 * and should not be modified or freed.
 *
 * Returns: (transfer none): the #GValue, or %NULL if not found
 *
 * Since: 1.1
 */
const GValue *
foundry_intent_get_attribute_value (FoundryIntent *self,
                                    const char    *attribute)
{
  FoundryIntentPrivate *priv = foundry_intent_get_instance_private (self);

  g_return_val_if_fail (FOUNDRY_IS_INTENT (self), NULL);
  g_return_val_if_fail (attribute != NULL, NULL);

  return g_hash_table_lookup (priv->attributes, attribute);
}

/**
 * foundry_intent_dup_attribute_string:
 * @self: a [class@Foundry.Intent]
 * @attribute: the attribute name
 *
 * Gets the attribute value as a string. The returned string is owned
 * by the intent and should not be modified or freed.
 *
 * Returns: (transfer full) (nullable): the string value, or %NULL if not found or not a string
 *
 * Since: 1.1
 */
char *
foundry_intent_dup_attribute_string (FoundryIntent *self,
                                     const char    *attribute)
{
  const GValue *value;

  g_return_val_if_fail (FOUNDRY_IS_INTENT (self), NULL);
  g_return_val_if_fail (attribute != NULL, NULL);

  value = foundry_intent_get_attribute_value (self, attribute);
  if (value == NULL || !G_VALUE_HOLDS_STRING (value))
    return NULL;

  return g_value_dup_string (value);
}

/**
 * foundry_intent_dup_attribute_strv:
 * @self: a [class@Foundry.Intent]
 * @attribute: the attribute name
 *
 * Gets the attribute value as a string. The returned string is owned
 * by the intent and should not be modified or freed.
 *
 * Returns: (transfer full): the string value, or %NULL if not found or not a string
 *
 * Since: 1.1
 */
char **
foundry_intent_dup_attribute_strv (FoundryIntent *self,
                                   const char    *attribute)
{
  const GValue *value;

  g_return_val_if_fail (FOUNDRY_IS_INTENT (self), NULL);
  g_return_val_if_fail (attribute != NULL, NULL);

  value = foundry_intent_get_attribute_value (self, attribute);
  if (value == NULL || !G_VALUE_HOLDS (value, G_TYPE_STRV))
    return NULL;

  return g_value_dup_boxed (value);
}

/**
 * foundry_intent_get_attribute_boolean:
 * @self: a [class@Foundry.Intent]
 * @attribute: the attribute name
 *
 * Gets the attribute value as a boolean.
 *
 * Returns: the boolean value, or %FALSE if not found or not a boolean
 *
 * Since: 1.1
 */
gboolean
foundry_intent_get_attribute_boolean (FoundryIntent *self,
                                      const char    *attribute)
{
  const GValue *value;

  g_return_val_if_fail (FOUNDRY_IS_INTENT (self), FALSE);
  g_return_val_if_fail (attribute != NULL, FALSE);

  value = foundry_intent_get_attribute_value (self, attribute);
  if (value == NULL || !G_VALUE_HOLDS_BOOLEAN (value))
    return FALSE;

  return g_value_get_boolean (value);
}

/**
 * foundry_intent_dup_attribute_object:
 * @self: a [class@Foundry.Intent]
 * @attribute: the attribute name
 *
 * Gets the attribute value as a [class@GObject.Object]. The returned object is
 * owned by the intent and should not be unref'd.
 *
 * Returns: (transfer full) (nullable): the [class@GObject.Object], or %NULL
 *   if not found or not an object
 *
 * Since: 1.1
 */
gpointer
foundry_intent_dup_attribute_object (FoundryIntent *self,
                                     const char    *attribute)
{
  const GValue *value;

  g_return_val_if_fail (FOUNDRY_IS_INTENT (self), NULL);
  g_return_val_if_fail (attribute != NULL, NULL);

  value = foundry_intent_get_attribute_value (self, attribute);
  if (value == NULL || !G_VALUE_HOLDS_OBJECT (value))
    return NULL;

  return g_value_dup_object (value);
}

void
foundry_intent_set_attribute (FoundryIntent *self,
                              const char    *attribute,
                              GType          type,
                              ...)
{
  FoundryIntentPrivate *priv = foundry_intent_get_instance_private (self);
  g_autofree char *errmsg = NULL;
  GValue *value;
  va_list args;

  g_return_if_fail (FOUNDRY_IS_INTENT (self));
  g_return_if_fail (attribute != NULL);
  g_return_if_fail (type != G_TYPE_INVALID);

  value = g_new0 (GValue, 1);
  g_value_init (value, type);

  va_start (args, type);
  G_VALUE_COLLECT (value, args, 0, &errmsg);
  va_end (args);

  if (errmsg != NULL)
    {
      g_warning ("Failed to collect value of type `%s`: %s",
                 g_type_name (type), errmsg);
      g_value_unset (value);
      g_free (value);
      return;
    }

  g_hash_table_replace (priv->attributes,
                        g_strdup (attribute),
                        g_steal_pointer (&value));
}

gboolean
foundry_intent_get_attribute_uint (FoundryIntent *self,
                                   const char    *attribute,
                                   guint         *value)
{
  const GValue *gvalue;

  *value = 0;

  if (!(gvalue = foundry_intent_get_attribute_value (self, attribute)))
    return FALSE;

  if (!G_VALUE_HOLDS_UINT (gvalue))
    return FALSE;

  *value = g_value_get_uint (gvalue);

  return TRUE;
}