[go: up one dir, main page]

File: tmpl-symbol.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 (308 lines) | stat: -rw-r--r-- 7,217 bytes parent folder | download | duplicates (3)
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
/* tmpl-symbol.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 "tmpl-expr.h"
#include "tmpl-symbol.h"

G_DEFINE_BOXED_TYPE (TmplSymbol, tmpl_symbol, tmpl_symbol_ref, tmpl_symbol_unref)

struct _TmplSymbol
{
  volatile gint  ref_count;
  TmplSymbolType type;
  union {
    GValue    value;
    struct {
      TmplExpr  *expr;
      GPtrArray *params;
    } expr;
  } u;
};

TmplSymbol *
tmpl_symbol_new (void)
{
  TmplSymbol *self;

  self = g_slice_new0 (TmplSymbol);
  self->ref_count = 1;
  self->type = TMPL_SYMBOL_VALUE;

  return self;
}

static inline void
tmpl_symbol_clear (TmplSymbol *self)
{
  if ((self->type == TMPL_SYMBOL_VALUE) &&
      (G_VALUE_TYPE (&self->u.value) != G_TYPE_INVALID))
    g_value_unset (&self->u.value);
  else if (self->type == TMPL_SYMBOL_EXPR)
    {
      g_clear_pointer (&self->u.expr.expr, tmpl_expr_unref);
      g_clear_pointer (&self->u.expr.params, g_ptr_array_unref);
    }
}

TmplSymbol *
tmpl_symbol_ref (TmplSymbol *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_symbol_unref (TmplSymbol *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))
    {
      tmpl_symbol_clear (self);
      g_slice_free (TmplSymbol, self);
    }
}

void
tmpl_symbol_assign_value (TmplSymbol   *self,
                          const GValue *value)
{
  g_return_if_fail (self != NULL);

  tmpl_symbol_clear (self);

  self->type = TMPL_SYMBOL_VALUE;

  if ((value != NULL) && (G_VALUE_TYPE (value) != G_TYPE_INVALID))
    {
      g_value_init (&self->u.value, G_VALUE_TYPE (value));
      g_value_copy (value, &self->u.value);
    }
}

/**
 * tmpl_symbol_assign_expr: (skip)
 * @self: A #TmplSymbol.
 * @expr: (nullable): An expression to assign, or %NULL.
 * @args: (element-type utf8): A #GPtrArray of strings.
 *
 * Sets the symbol as a %TMPL_SYMBOL_EXPR with the given ordered and
 * named parameters.
 */
void
tmpl_symbol_assign_expr (TmplSymbol *self,
                         TmplExpr   *expr,
                         GPtrArray  *args)
{
  g_return_if_fail (self != NULL);

  tmpl_symbol_clear (self);

  self->type = TMPL_SYMBOL_EXPR;

  if (expr != NULL)
    self->u.expr.expr = tmpl_expr_ref (expr);

  if (args != NULL)
    self->u.expr.params = g_ptr_array_ref (args);
}

TmplSymbolType
tmpl_symbol_get_symbol_type (TmplSymbol *self)
{
  g_return_val_if_fail (self != NULL, 0);

  return self->type;
}

/**
 * tmpl_symbol_get_expr:
 * @self: A #TmplSymbol
 * @params: (out) (element-type utf8) (transfer none) (nullable): A list of parameters
 *
 * Returns: (transfer none): A #TmplExpr.
 */
TmplExpr *
tmpl_symbol_get_expr (TmplSymbol  *self,
                      GPtrArray  **params)
{
  g_return_val_if_fail (self != NULL, 0);

  if (self->type != TMPL_SYMBOL_EXPR)
    {
      g_warning ("Attempt to fetch TmplExpr from a value symbol");
      return NULL;
    }

  if (params != NULL)
    *params = self->u.expr.params;

  return self->u.expr.expr;
}

void
tmpl_symbol_get_value (TmplSymbol *self,
                       GValue     *value)
{
  g_return_if_fail (self != NULL);
  g_return_if_fail (value != NULL);

  if (self->type != TMPL_SYMBOL_VALUE)
    {
      g_warning ("Attempt to fetch value from a expr symbol");
      return;
    }

  if (G_VALUE_TYPE (&self->u.value) != G_TYPE_INVALID)
    {
      g_value_init (value, G_VALUE_TYPE (&self->u.value));
      g_value_copy (&self->u.value, value);
    }
}

void
tmpl_symbol_assign_boolean (TmplSymbol *self,
                            gboolean    v_bool)
{
  GValue value = G_VALUE_INIT;

  g_return_if_fail (self != NULL);

  g_value_init (&value, G_TYPE_BOOLEAN);
  g_value_set_boolean (&value, v_bool);
  tmpl_symbol_assign_value (self, &value);
  g_value_unset (&value);
}

void
tmpl_symbol_assign_double (TmplSymbol *self,
                           gdouble     v_double)
{
  GValue value = G_VALUE_INIT;

  g_return_if_fail (self != NULL);

  g_value_init (&value, G_TYPE_DOUBLE);
  g_value_set_double (&value, v_double);
  tmpl_symbol_assign_value (self, &value);
  g_value_unset (&value);
}

void
tmpl_symbol_assign_string (TmplSymbol  *self,
                           const gchar *v_string)
{
  GValue value = G_VALUE_INIT;

  g_return_if_fail (self != NULL);

  g_value_init (&value, G_TYPE_STRING);
  g_value_set_string (&value, v_string);
  tmpl_symbol_assign_value (self, &value);
  g_value_unset (&value);
}

/**
 * tmpl_symbol_assign_object:
 * @self: A #TmplSymbol
 * @v_object: (type GObject.Object) (nullable): a #GObject or %NULL.
 *
 * Sets the value to the object @v_object.
 */
void
tmpl_symbol_assign_object (TmplSymbol *self,
                           gpointer    v_object)
{
  GValue value = G_VALUE_INIT;

  g_return_if_fail (self != NULL);

  g_value_init (&value, G_TYPE_OBJECT);
  g_value_set_object (&value, v_object);
  tmpl_symbol_assign_value (self, &value);
  g_value_unset (&value);
}

/**
 * tmpl_symbol_assign_variant:
 * @self: A #TmplSymbol
 * @v_variant: (nullable): a #GVariant or %NULL.
 *
 * Sets the value to the #GVariant @v_variant.
 *
 * If @v_variant has a floating reference, it is consumed.
 */
void
tmpl_symbol_assign_variant (TmplSymbol *self,
                            GVariant   *v_variant)
{
  GValue value = G_VALUE_INIT;

  g_return_if_fail (self != NULL);

  g_value_init (&value, G_TYPE_VARIANT);
  g_value_set_variant (&value, v_variant);
  tmpl_symbol_assign_value (self, &value);
  g_value_unset (&value);
}

/**
 * tmpl_symbol_assign_strv:
 * @self: A #TmplSymbol
 * @strv: (nullable) (array zero-terminated=1): the value to set, or %NULL
 *
 * Sets the value to the strv @strv.
 */
void
tmpl_symbol_assign_strv (TmplSymbol *self,
                         const gchar **strv)
{
  GValue value = G_VALUE_INIT;

  g_return_if_fail (self != NULL);

  g_value_init (&value, G_TYPE_VARIANT);
  g_value_set_variant (&value, g_variant_new_strv (strv, -1));
  tmpl_symbol_assign_value (self, &value);
  g_value_unset (&value);
}

gboolean
tmpl_symbol_holds (TmplSymbol *self,
                   GType       type)
{
  return self != NULL &&
         self->type == TMPL_SYMBOL_VALUE &&
         self->u.value.g_type == type;
}

gpointer
tmpl_symbol_get_boxed (TmplSymbol *self)
{
  if (self != NULL &&
      self->type == TMPL_SYMBOL_VALUE &&
      G_VALUE_HOLDS_BOXED (&self->u.value))
    return g_value_get_boxed (&self->u.value);

  return NULL;
}