hdy-value-object.c 6.22 KB
Newer Older
1
2
3
/*
 * Copyright (C) 2019 Red Hat Inc.
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
6
 */

Bastien Nocera's avatar
Bastien Nocera committed
7
8
#include "config.h"
#include <glib/gi18n-lib.h>
9
10
11
12
#include <gobject/gvaluecollector.h>
#include "hdy-value-object.h"

/**
13
 * HdyValueObject:
14
 *
15
 * An object representing a [struct@GObject.Value].
16
 *
17
18
19
20
 * The `HdyValueObject` object represents a [struct@GObject.Value], allowing it
 * to be used with [iface@Gio.ListModel].
 *
 * Since: 1.0
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 */

struct _HdyValueObject
{
  GObject parent_instance;

  GValue value;
};

G_DEFINE_TYPE (HdyValueObject, hdy_value_object, G_TYPE_OBJECT)

enum {
  PROP_0,
  PROP_VALUE,
  N_PROPS
};

static GParamSpec *props [N_PROPS];

/**
 * hdy_value_object_new:
42
43
44
 * @value: the value to store
 *
 * Creates a new `HdyValueObject`.
45
 *
46
 * Returns: a new `HdyValueObject`
47
 *
48
 * Since: 1.0
49
50
51
52
53
54
55
56
57
58
59
 */
HdyValueObject *
hdy_value_object_new (const GValue *value)
{
  return g_object_new (HDY_TYPE_VALUE_OBJECT,
                       "value", value,
                       NULL);
}

/**
 * hdy_value_object_new_collect: (skip)
60
 * @type: the type of the value
61
62
 * @...: the value to store
 *
63
64
65
66
 * Creates a new `HdyValueObject`.
 *
 * This is a convenience method which uses the `G_VALUE_COLLECT` macro
 * internally.
67
 *
68
69
70
 * Returns: a new `HdyValueObject`
 *
 * Since: 1.0
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
 */
HdyValueObject*
hdy_value_object_new_collect (GType type, ...)
{
  g_auto(GValue) value = G_VALUE_INIT;
  g_autofree gchar *error = NULL;
  va_list var_args;

  va_start (var_args, type);

  G_VALUE_COLLECT_INIT (&value, type, var_args, 0, &error);

  va_end (var_args);

  if (error)
    g_critical ("%s: %s", G_STRFUNC, error);

  return g_object_new (HDY_TYPE_VALUE_OBJECT,
                       "value", &value,
                       NULL);
}

/**
 * hdy_value_object_new_string: (skip)
 * @string: (transfer none): the string to store
 *
97
98
99
100
101
102
 * Creates a new `HdyValueObject`.
 *
 * This is a convenience method to create a [class@ValueObject] that stores a
 * string.
 *
 * Returns: a new `HdyValueObject`
103
 *
104
 * Since: 1.0
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
 */
HdyValueObject*
hdy_value_object_new_string (const gchar *string)
{
  g_auto(GValue) value = G_VALUE_INIT;

  g_value_init (&value, G_TYPE_STRING);
  g_value_set_string (&value, string);
  return hdy_value_object_new (&value);
}

/**
 * hdy_value_object_new_take_string: (skip)
 * @string: (transfer full): the string to store
 *
120
 * Creates a new `HdyValueObject`.
121
 *
122
123
124
125
126
127
 * This is a convenience method to create a [class@ValueObject] that stores a
 * string taking ownership of it.
 *
 * Returns: a new `HdyValueObject`
 *
 * Since: 1.0
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
 */
HdyValueObject*
hdy_value_object_new_take_string (gchar *string)
{
  g_auto(GValue) value = G_VALUE_INIT;

  g_value_init (&value, G_TYPE_STRING);
  g_value_take_string (&value, string);
  return hdy_value_object_new (&value);
}

static void
hdy_value_object_finalize (GObject *object)
{
  HdyValueObject *self = HDY_VALUE_OBJECT (object);

  g_value_unset (&self->value);

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

static void
hdy_value_object_get_property (GObject    *object,
                               guint       prop_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  HdyValueObject *self = HDY_VALUE_OBJECT (object);

  switch (prop_id)
    {
    case PROP_VALUE:
      g_value_set_boxed (value, &self->value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
hdy_value_object_set_property (GObject      *object,
                               guint         prop_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  HdyValueObject *self = HDY_VALUE_OBJECT (object);
  GValue *real_value;

  switch (prop_id)
    {
    case PROP_VALUE:
      /* construct only */
      real_value = g_value_get_boxed (value);
      g_value_init (&self->value, real_value->g_type);
      g_value_copy (real_value, &self->value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
hdy_value_object_class_init (HdyValueObjectClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = hdy_value_object_finalize;
  object_class->get_property = hdy_value_object_get_property;
  object_class->set_property = hdy_value_object_set_property;

200
201
202
203
204
205
206
  /**
   * HdyValueObject:value: (attributes org.gtk.Property.get=hdy_value_object_get_value)
   *
   * The contained value.
   *
   * Since: 1.0
   */
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  props[PROP_VALUE] =
    g_param_spec_boxed ("value", C_("HdyValueObjectClass", "Value"),
                        C_("HdyValueObjectClass", "The contained value"),
                        G_TYPE_VALUE,
                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class,
                                     N_PROPS,
                                     props);
}

static void
hdy_value_object_init (HdyValueObject *self)
{
}

/**
224
225
 * hdy_value_object_get_value: (attributes org.gtk.Method.get_property=value)
 * @value: the value
226
227
228
 *
 * Return the contained value.
 *
229
230
231
 * Returns: (transfer none): the contained [struct@GObject.Value]
 *
 * Since: 1.0
232
233
234
235
236
237
238
239
240
 */
const GValue*
hdy_value_object_get_value (HdyValueObject *value)
{
  return &value->value;
}

/**
 * hdy_value_object_copy_value:
241
242
 * @value: the value
 * @dest: value with correct type to copy into
243
 *
244
 * Copy data from the contained [struct@GObject.Value] into @dest.
245
 *
246
 * Since: 1.0
247
248
249
250
251
252
253
254
255
256
 */
void
hdy_value_object_copy_value (HdyValueObject *value,
                             GValue         *dest)
{
  g_value_copy (&value->value, dest);
}

/**
 * hdy_value_object_get_string:
257
 * @value: the value
258
 *
259
 * Returns the contained string if the value is of type `G_TYPE_STRING`.
260
261
 *
 * Returns: (transfer none): the contained string
262
263
 *
 * Since: 1.0
264
265
266
267
268
269
270
271
272
 */
const gchar*
hdy_value_object_get_string (HdyValueObject *value)
{
  return g_value_get_string (&value->value);
}

/**
 * hdy_value_object_dup_string:
273
 * @value: the value
274
 *
275
 * Gets a copy of the contained string if the value is of type `G_TYPE_STRING`.
276
277
 *
 * Returns: (transfer full): a copy of the contained string
278
279
 *
 * Since: 1.0
280
281
282
283
284
285
 */
gchar*
hdy_value_object_dup_string (HdyValueObject *value)
{
  return g_value_dup_string (&value->value);
}