[go: up one dir, main page]

File: tmpl-scope.c

package info (click to toggle)
template-glib 3.36.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 792 kB
  • sloc: ansic: 6,565; yacc: 288; lex: 237; python: 12; javascript: 11; makefile: 9
file content (526 lines) | stat: -rw-r--r-- 13,447 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/* tmpl-scope.c
 *
 * Copyright (C) 2016 Christian Hergert <chergert@redhat.com>
 *
 * This file 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 file 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/>.
 */

#include "config.h"

#include "tmpl-gi-private.h"
#include "tmpl-scope.h"
#include "tmpl-symbol.h"

struct _TmplScope
{
  volatile gint      ref_count;
  TmplScope         *parent;
  GHashTable        *symbols;
  TmplScopeResolver  resolver;
  gpointer           resolver_data;
  GDestroyNotify     resolver_destroy;
};

G_DEFINE_BOXED_TYPE (TmplScope, tmpl_scope, tmpl_scope_ref, tmpl_scope_unref)

TmplScope *
tmpl_scope_ref (TmplScope *self)
{
  g_return_val_if_fail (self != NULL, NULL);
  g_return_val_if_fail (self->ref_count > 0, NULL);

  g_atomic_int_inc (&self->ref_count);

  return self;
}

void
tmpl_scope_unref (TmplScope *self)
{
  g_return_if_fail (self != NULL);
  g_return_if_fail (self->ref_count > 0);

  if (g_atomic_int_dec_and_test (&self->ref_count))
    {
      if (self->resolver_destroy)
        g_clear_pointer (&self->resolver_data, self->resolver_destroy);
      self->resolver = NULL;
      self->resolver_destroy = NULL;
      g_clear_pointer (&self->symbols, g_hash_table_unref);
      g_clear_pointer (&self->parent, tmpl_scope_unref);
      g_slice_free (TmplScope, self);
    }
}

/**
 * tmpl_scope_new:
 *
 * Creates a new scope to contain variables and custom expressions,
 *
 * Returns: (transfer full): A newly created #TmplScope.
 */
TmplScope *
tmpl_scope_new (void)
{
  TmplScope *self;

  self = g_slice_new0 (TmplScope);
  self->ref_count = 1;
  self->parent = NULL;

  return self;
}

/**
 * tmpl_scope_new_with_parent:
 * @parent: (nullable): An optional parent scope
 *
 * Creates a new scope to contain variables and custom expressions,
 * If @parent is set, the parent scope will be inherited.
 *
 * Returns: (transfer full): A newly created #TmplScope.
 */
TmplScope *
tmpl_scope_new_with_parent (TmplScope *parent)
{
  TmplScope *self;

  self = g_slice_new0 (TmplScope);
  self->ref_count = 1;
  self->parent = parent != NULL ? tmpl_scope_ref (parent) : NULL;

  return self;
}

static TmplSymbol *
tmpl_scope_get_full (TmplScope   *self,
                     const gchar *name,
                     gboolean     create)
{
  TmplSymbol *symbol = NULL;
  TmplScope *parent;

  g_return_val_if_fail (self != NULL, NULL);

  /* See if this scope has the symbol */
  if (self->symbols != NULL)
    {
      if ((symbol = g_hash_table_lookup (self->symbols, name)))
        return symbol;
    }

  /* Try to locate the symbol in a parent scope */
  for (parent = self->parent; parent != NULL; parent = parent->parent)
    {
      if (parent->symbols != NULL)
        {
          if ((symbol = g_hash_table_lookup (parent->symbols, name)))
            return symbol;
        }
    }

  /* Call our resolver helper to locate the symbol */
  for (parent = self; parent != NULL; parent = parent->parent)
    {
      if (parent->resolver != NULL)
        {
          if (parent->resolver (parent, name, &symbol, parent->resolver_data) && symbol)
            {
              /* Pass ownership to our scope, and return a weak ref */
              tmpl_scope_take (self, name, symbol);
              return symbol;
            }
        }
    }

  if (create)
    {
      /* Define the symbol in this scope */
      symbol = tmpl_symbol_new ();
      tmpl_scope_take (self, name, symbol);
    }

  return symbol;
}

/**
 * tmpl_scope_get:
 *
 * If the symbol could not be found, it will be allocated.
 *
 * Returns: (transfer none): A #TmplSymbol.
 */
TmplSymbol *
tmpl_scope_get (TmplScope   *self,
                const gchar *name)
{
  return tmpl_scope_get_full (self, name, TRUE);
}

/**
 * tmpl_scope_take:
 * @self: A #TmplScope
 * @name: The name of the symbol
 * @symbol: (nullable) (transfer full): A #TmplSymbol or %NULL
 *
 * Sets the symbol named @name to @symbol in @scope.
 *
 * This differs from tmpl_scope_set() in that it takes ownership
 * of @symbol.
 */
void
tmpl_scope_take (TmplScope   *self,
                 const gchar *name,
                 TmplSymbol  *symbol)
{
  g_return_if_fail (self != NULL);
  g_return_if_fail (name != NULL);

  if G_UNLIKELY (symbol == NULL)
    {
      if G_LIKELY (self->symbols != NULL)
        g_hash_table_remove (self->symbols, name);
      return;
    }

  if (self->symbols == NULL)
    self->symbols = g_hash_table_new_full (g_str_hash,
                                           g_str_equal,
                                           g_free,
                                           (GDestroyNotify) tmpl_symbol_unref);

  g_hash_table_insert (self->symbols, g_strdup (name), symbol);
}

/**
 * tmpl_scope_set:
 * @self: A #TmplScope
 * @name: the name of the symbol
 * @symbol: (nullable) (transfer none): An #TmplSymbol or %NULL.
 *
 * If the symbol already exists, it will be overwritten.
 *
 * If @symbol is %NULL, the symbol will be removed from scope.
 */
void
tmpl_scope_set (TmplScope   *self,
                const gchar *name,
                TmplSymbol  *symbol)
{
  g_return_if_fail (self != NULL);

  if (symbol != NULL)
    tmpl_symbol_ref (symbol);

  tmpl_scope_take (self, name, symbol);
}

/**
 * tmpl_scope_set_value:
 * @self: A #TmplScope
 * @name: a name for the symbol
 * @value: (nullable): A #GValue or %NULL
 *
 * Sets the contents of the symbol named @name to the value @value.
 */
void
tmpl_scope_set_value (TmplScope    *self,
                      const gchar  *name,
                      const GValue *value)
{
  g_return_if_fail (self != NULL);
  g_return_if_fail (name != NULL);

  tmpl_symbol_assign_value (tmpl_scope_get_full (self, name, TRUE), value);
}

/**
 * tmpl_scope_set_boolean:
 * @self: A #TmplScope
 * @name: a name for the symbol
 * @value: a #gboolean
 *
 * Sets the value of the symbol named @name to a gboolean value of @value.
 */
void
tmpl_scope_set_boolean (TmplScope   *self,
                        const gchar *name,
                        gboolean     value)
{
  g_return_if_fail (self != NULL);
  g_return_if_fail (name != NULL);

  tmpl_symbol_assign_boolean (tmpl_scope_get_full (self, name, TRUE), value);
}

/**
 * tmpl_scope_set_double:
 * @self: A #TmplScope
 * @name: a name for the symbol
 * @value: a #gdouble
 *
 * Sets the value of the symbol named @name to a gdouble value of @value.
 */
void
tmpl_scope_set_double (TmplScope   *self,
                       const gchar *name,
                       gdouble      value)
{
  g_return_if_fail (self != NULL);
  g_return_if_fail (name != NULL);

  tmpl_symbol_assign_double (tmpl_scope_get_full (self, name, TRUE), value);
}

/**
 * tmpl_scope_set_object:
 * @self: A #TmplScope
 * @name: a name for the symbol
 * @value: (type GObject.Object) (nullable): a #GObject or %NULL.
 *
 * Sets the value of the symbol named @name to the object @value.
 */
void
tmpl_scope_set_object (TmplScope   *self,
                       const gchar *name,
                       gpointer     value)
{
  g_return_if_fail (self != NULL);
  g_return_if_fail (name != NULL);
  g_return_if_fail (!value || G_IS_OBJECT (value));

  tmpl_symbol_assign_object (tmpl_scope_get_full (self, name, TRUE), value);
}

/**
 * tmpl_scope_set_variant:
 * @self: A #TmplScope
 * @name: a name for the symbol
 * @value: (nullable): the variant to set it to, or %NULL
 *
 * Sets the value of the symbol named @name to the variant @value.
 *
 * If @value has a floating reference, it is consumed.
 */
void
tmpl_scope_set_variant (TmplScope   *self,
                        const gchar *name,
                        GVariant    *value)
{
  g_return_if_fail (self != NULL);
  g_return_if_fail (name != NULL);

  tmpl_symbol_assign_variant (tmpl_scope_get_full (self, name, TRUE),
                              value);
}

/**
 * tmpl_scope_set_strv:
 * @self: A #TmplScope
 * @name: a name for the symbol
 * @value: (nullable) (array zero-terminated=1): the value to set it to, or %NULL
 *
 * Sets the value of the symbol named @name to the strv @value.
 */
void
tmpl_scope_set_strv (TmplScope   *self,
                     const gchar *name,
                     const gchar **value)
{
  g_return_if_fail (self != NULL);
  g_return_if_fail (name != NULL);

  tmpl_symbol_assign_variant (tmpl_scope_get_full (self, name, TRUE),
                              g_variant_new_strv (value, -1));
}

/**
 * tmpl_scope_set_string:
 * @self: A #TmplScope
 * @name: a name for the symbol
 * @value: (nullable): A string or %NULL.
 *
 * Sets the value of the symbol named @name to a string matching @value.
 */
void
tmpl_scope_set_string (TmplScope   *self,
                       const gchar *name,
                       const gchar *value)
{
  g_return_if_fail (self != NULL);
  g_return_if_fail (name != NULL);

  tmpl_symbol_assign_string (tmpl_scope_get_full (self, name, TRUE), value);
}

/**
 * tmpl_scope_peek:
 *
 * If the symbol could not be found, %NULL is returned.
 *
 * Returns: (transfer none) (nullable): A #TmplSymbol or %NULL.
 */
TmplSymbol *
tmpl_scope_peek (TmplScope   *self,
                 const gchar *name)
{
  g_return_val_if_fail (self != NULL, NULL);
  g_return_val_if_fail (name != NULL, NULL);

  return tmpl_scope_get_full (self, name, FALSE);
}

void
tmpl_scope_set_resolver (TmplScope         *self,
                         TmplScopeResolver  resolver,
                         gpointer           user_data,
                         GDestroyNotify     destroy)
{
  g_return_if_fail (self != NULL);

  if (resolver != self->resolver ||
      user_data != self->resolver_data ||
      destroy != self->resolver_destroy)
    {
      if (self->resolver && self->resolver_destroy && self->resolver_data)
        {
          g_clear_pointer (&self->resolver_data, self->resolver_destroy);
          self->resolver_destroy = NULL;
          self->resolver = NULL;
        }

      self->resolver = resolver;
      self->resolver_data = user_data;
      self->resolver_destroy = destroy;
    }
}

/**
 * tmpl_scope_require:
 * @self: a #TmplScope
 * @namespace_: the namespace to import into the scope
 * @version: (nullable): the version of @namespace_ to import
 *
 * Imports @namespace_ into @self so it can be used by expressions.
 *
 * Returns: %TRUE if successful; otherwise %FALSE
 */
gboolean
tmpl_scope_require (TmplScope    *self,
                    const char   *namespace_,
                    const char   *version)
{
  GITypelib *typelib;
  GValue value = G_VALUE_INIT;

  g_return_val_if_fail (self != NULL, FALSE);
  g_return_val_if_fail (namespace_ != NULL, FALSE);

  if (!(typelib = g_irepository_require (NULL, namespace_, version, 0, NULL)))
    return FALSE;

  g_value_init (&value, TMPL_TYPE_TYPELIB);
  g_value_set_pointer (&value, typelib);
  tmpl_scope_set_value (self, namespace_, &value);
  g_value_unset (&value);

  return TRUE;
}

static void
tmpl_scope_list_symbols_internal (TmplScope *self,
                                  GPtrArray *ar,
                                  gboolean   recursive)
{
  GHashTableIter iter;
  const char *key;

  g_assert (self != NULL);
  g_assert (ar != NULL);

  g_hash_table_iter_init (&iter, self->symbols);
  while (g_hash_table_iter_next (&iter, (gpointer *)&key, NULL))
    g_ptr_array_add (ar, g_strdup (key));

  if (recursive && self->parent)
    tmpl_scope_list_symbols_internal (self->parent, ar, recursive);
}

/**
 * tmpl_scope_list_symbols:
 * @self: a #TmplScope
 * @recursive: if the parent scopes should be included
 *
 * Gets the names of all symbols within the scope.
 *
 * Returns: (array zero-terminated=1) (element-type utf8) (transfer full):
 *   an array containing the names of all symbols within the scope.
 */
char **
tmpl_scope_list_symbols (TmplScope *self,
                         gboolean   recursive)
{
  GPtrArray *ar;

  g_return_val_if_fail (self != NULL, NULL);

  ar = g_ptr_array_new ();
  tmpl_scope_list_symbols_internal (self, ar, recursive);
  g_ptr_array_add (ar, NULL);

  return (char **)g_ptr_array_free (ar, FALSE);
}

void
tmpl_scope_set_null (TmplScope  *self,
                     const char *name)
{
  GValue value = G_VALUE_INIT;

  g_value_init (&value, G_TYPE_POINTER);
  g_value_set_pointer (&value, NULL);

  tmpl_scope_set_value (self, name, &value);
}

/**
 * tmpl_scope_dup_string:
 * @self: a #TmplScope
 *
 * Gets a string if the symbol @name is a string.
 *
 * Otherwise, %NULL is returned.
 *
 * Returns: (transfer full) (nullable): a string or %NULL
 *
 * Since: 3.36
 */
char *
tmpl_scope_dup_string (TmplScope  *self,
                       const char *name)
{
  GValue value = G_VALUE_INIT;
  TmplSymbol *symbol;
  char *ret = NULL;

  if (!(symbol = tmpl_scope_peek (self, name)))
    return NULL;

  tmpl_symbol_get_value (symbol, &value);
  if (G_VALUE_HOLDS_STRING (&value))
    ret = g_value_dup_string (&value);
  g_value_unset (&value);

  return ret;
}