1 /* map.c generated by valac, the Vala compiler
2 * generated from map.vala, do not modify */
3
4 /* map.vala
5 *
6 * Copyright (C) 2007 Jürg Billeter
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * Author:
23 * Jürg Billeter <j@bitron.ch>
24 */
25
26 #include "valagee.h"
27 #include <glib-object.h>
28 #include <glib.h>
29 #include <gobject/gvaluecollector.h>
30
31 typedef struct _ValaParamSpecMap ValaParamSpecMap;
32
33 struct _ValaMapPrivate {
34 GType k_type;
35 GBoxedCopyFunc k_dup_func;
36 GDestroyNotify k_destroy_func;
37 GType v_type;
38 GBoxedCopyFunc v_dup_func;
39 GDestroyNotify v_destroy_func;
40 };
41
42 struct _ValaParamSpecMap {
43 GParamSpec parent_instance;
44 };
45
46 static gint ValaMap_private_offset;
47 static gpointer vala_map_parent_class = NULL;
48
49 static ValaSet* vala_map_real_get_keys (ValaMap* self);
50 static ValaCollection* vala_map_real_get_values (ValaMap* self);
51 static gboolean vala_map_real_contains (ValaMap* self,
52 gconstpointer key);
53 static gpointer vala_map_real_get (ValaMap* self,
54 gconstpointer key);
55 static void vala_map_real_set (ValaMap* self,
56 gconstpointer key,
57 gconstpointer value);
58 static gboolean vala_map_real_remove (ValaMap* self,
59 gconstpointer key);
60 static void vala_map_real_clear (ValaMap* self);
61 static ValaMapIterator* vala_map_real_map_iterator (ValaMap* self);
62 static void vala_map_finalize (ValaMap * obj);
63 static GType vala_map_get_type_once (void);
64
65 static inline gpointer
vala_map_get_instance_private(ValaMap * self)66 vala_map_get_instance_private (ValaMap* self)
67 {
68 return G_STRUCT_MEMBER_P (self, ValaMap_private_offset);
69 }
70
71 gint
vala_map_get_size(ValaMap * self)72 vala_map_get_size (ValaMap* self)
73 {
74 g_return_val_if_fail (self != NULL, 0);
75 return VALA_MAP_GET_CLASS (self)->get_size (self);
76 }
77
78 /**
79 * Returns the keys of this map as a read-only set.
80 *
81 * @return the keys of the map
82 */
83 static ValaSet*
vala_map_real_get_keys(ValaMap * self)84 vala_map_real_get_keys (ValaMap* self)
85 {
86 g_critical ("Type `%s' does not implement abstract method `vala_map_get_keys'", g_type_name (G_TYPE_FROM_INSTANCE (self)));
87 return NULL;
88 }
89
90 ValaSet*
vala_map_get_keys(ValaMap * self)91 vala_map_get_keys (ValaMap* self)
92 {
93 g_return_val_if_fail (self != NULL, NULL);
94 return VALA_MAP_GET_CLASS (self)->get_keys (self);
95 }
96
97 /**
98 * Returns the values of this map as a read-only collection.
99 *
100 * @return the values of the map
101 */
102 static ValaCollection*
vala_map_real_get_values(ValaMap * self)103 vala_map_real_get_values (ValaMap* self)
104 {
105 g_critical ("Type `%s' does not implement abstract method `vala_map_get_values'", g_type_name (G_TYPE_FROM_INSTANCE (self)));
106 return NULL;
107 }
108
109 ValaCollection*
vala_map_get_values(ValaMap * self)110 vala_map_get_values (ValaMap* self)
111 {
112 g_return_val_if_fail (self != NULL, NULL);
113 return VALA_MAP_GET_CLASS (self)->get_values (self);
114 }
115
116 /**
117 * Determines whether this map contains the specified key.
118 *
119 * @param key the key to locate in the map
120 *
121 * @return true if key is found, false otherwise
122 */
123 static gboolean
vala_map_real_contains(ValaMap * self,gconstpointer key)124 vala_map_real_contains (ValaMap* self,
125 gconstpointer key)
126 {
127 gboolean _tmp0_ = FALSE;
128 g_critical ("Type `%s' does not implement abstract method `vala_map_contains'", g_type_name (G_TYPE_FROM_INSTANCE (self)));
129 return _tmp0_;
130 }
131
132 gboolean
vala_map_contains(ValaMap * self,gconstpointer key)133 vala_map_contains (ValaMap* self,
134 gconstpointer key)
135 {
136 g_return_val_if_fail (self != NULL, FALSE);
137 return VALA_MAP_GET_CLASS (self)->contains (self, key);
138 }
139
140 /**
141 * Returns the value of the specified key in this map.
142 *
143 * @param key the key whose value is to be retrieved
144 *
145 * @return the value associated with the key, or null if the key
146 * couldn't be found
147 */
148 static gpointer
vala_map_real_get(ValaMap * self,gconstpointer key)149 vala_map_real_get (ValaMap* self,
150 gconstpointer key)
151 {
152 g_critical ("Type `%s' does not implement abstract method `vala_map_get'", g_type_name (G_TYPE_FROM_INSTANCE (self)));
153 return NULL;
154 }
155
156 gpointer
vala_map_get(ValaMap * self,gconstpointer key)157 vala_map_get (ValaMap* self,
158 gconstpointer key)
159 {
160 g_return_val_if_fail (self != NULL, NULL);
161 return VALA_MAP_GET_CLASS (self)->get (self, key);
162 }
163
164 /**
165 * Inserts a new key and value into this map.
166 *
167 * @param key the key to insert
168 * @param value the value to associate with the key
169 */
170 static void
vala_map_real_set(ValaMap * self,gconstpointer key,gconstpointer value)171 vala_map_real_set (ValaMap* self,
172 gconstpointer key,
173 gconstpointer value)
174 {
175 g_critical ("Type `%s' does not implement abstract method `vala_map_set'", g_type_name (G_TYPE_FROM_INSTANCE (self)));
176 return;
177 }
178
179 void
vala_map_set(ValaMap * self,gconstpointer key,gconstpointer value)180 vala_map_set (ValaMap* self,
181 gconstpointer key,
182 gconstpointer value)
183 {
184 g_return_if_fail (self != NULL);
185 VALA_MAP_GET_CLASS (self)->set (self, key, value);
186 }
187
188 /**
189 * Removes the specified key from this map.
190 *
191 * @param key the key to remove from the map
192 *
193 * @return true if the map has been changed, false otherwise
194 */
195 static gboolean
vala_map_real_remove(ValaMap * self,gconstpointer key)196 vala_map_real_remove (ValaMap* self,
197 gconstpointer key)
198 {
199 gboolean _tmp0_ = FALSE;
200 g_critical ("Type `%s' does not implement abstract method `vala_map_remove'", g_type_name (G_TYPE_FROM_INSTANCE (self)));
201 return _tmp0_;
202 }
203
204 gboolean
vala_map_remove(ValaMap * self,gconstpointer key)205 vala_map_remove (ValaMap* self,
206 gconstpointer key)
207 {
208 g_return_val_if_fail (self != NULL, FALSE);
209 return VALA_MAP_GET_CLASS (self)->remove (self, key);
210 }
211
212 /**
213 * Removes all items from this collection. Must not be called on
214 * read-only collections.
215 */
216 static void
vala_map_real_clear(ValaMap * self)217 vala_map_real_clear (ValaMap* self)
218 {
219 g_critical ("Type `%s' does not implement abstract method `vala_map_clear'", g_type_name (G_TYPE_FROM_INSTANCE (self)));
220 return;
221 }
222
223 void
vala_map_clear(ValaMap * self)224 vala_map_clear (ValaMap* self)
225 {
226 g_return_if_fail (self != NULL);
227 VALA_MAP_GET_CLASS (self)->clear (self);
228 }
229
230 /**
231 * Returns a Iterator that can be used for simple iteration over a
232 * map.
233 *
234 * @return a Iterator that can be used for simple iteration over a
235 * map
236 */
237 static ValaMapIterator*
vala_map_real_map_iterator(ValaMap * self)238 vala_map_real_map_iterator (ValaMap* self)
239 {
240 g_critical ("Type `%s' does not implement abstract method `vala_map_map_iterator'", g_type_name (G_TYPE_FROM_INSTANCE (self)));
241 return NULL;
242 }
243
244 ValaMapIterator*
vala_map_map_iterator(ValaMap * self)245 vala_map_map_iterator (ValaMap* self)
246 {
247 g_return_val_if_fail (self != NULL, NULL);
248 return VALA_MAP_GET_CLASS (self)->map_iterator (self);
249 }
250
251 ValaMap*
vala_map_construct(GType object_type,GType k_type,GBoxedCopyFunc k_dup_func,GDestroyNotify k_destroy_func,GType v_type,GBoxedCopyFunc v_dup_func,GDestroyNotify v_destroy_func)252 vala_map_construct (GType object_type,
253 GType k_type,
254 GBoxedCopyFunc k_dup_func,
255 GDestroyNotify k_destroy_func,
256 GType v_type,
257 GBoxedCopyFunc v_dup_func,
258 GDestroyNotify v_destroy_func)
259 {
260 ValaMap* self = NULL;
261 self = (ValaMap*) g_type_create_instance (object_type);
262 self->priv->k_type = k_type;
263 self->priv->k_dup_func = k_dup_func;
264 self->priv->k_destroy_func = k_destroy_func;
265 self->priv->v_type = v_type;
266 self->priv->v_dup_func = v_dup_func;
267 self->priv->v_destroy_func = v_destroy_func;
268 return self;
269 }
270
271 static void
vala_value_map_init(GValue * value)272 vala_value_map_init (GValue* value)
273 {
274 value->data[0].v_pointer = NULL;
275 }
276
277 static void
vala_value_map_free_value(GValue * value)278 vala_value_map_free_value (GValue* value)
279 {
280 if (value->data[0].v_pointer) {
281 vala_map_unref (value->data[0].v_pointer);
282 }
283 }
284
285 static void
vala_value_map_copy_value(const GValue * src_value,GValue * dest_value)286 vala_value_map_copy_value (const GValue* src_value,
287 GValue* dest_value)
288 {
289 if (src_value->data[0].v_pointer) {
290 dest_value->data[0].v_pointer = vala_map_ref (src_value->data[0].v_pointer);
291 } else {
292 dest_value->data[0].v_pointer = NULL;
293 }
294 }
295
296 static gpointer
vala_value_map_peek_pointer(const GValue * value)297 vala_value_map_peek_pointer (const GValue* value)
298 {
299 return value->data[0].v_pointer;
300 }
301
302 static gchar*
vala_value_map_collect_value(GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)303 vala_value_map_collect_value (GValue* value,
304 guint n_collect_values,
305 GTypeCValue* collect_values,
306 guint collect_flags)
307 {
308 if (collect_values[0].v_pointer) {
309 ValaMap * object;
310 object = collect_values[0].v_pointer;
311 if (object->parent_instance.g_class == NULL) {
312 return g_strconcat ("invalid unclassed object pointer for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
313 } else if (!g_value_type_compatible (G_TYPE_FROM_INSTANCE (object), G_VALUE_TYPE (value))) {
314 return g_strconcat ("invalid object type `", g_type_name (G_TYPE_FROM_INSTANCE (object)), "' for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
315 }
316 value->data[0].v_pointer = vala_map_ref (object);
317 } else {
318 value->data[0].v_pointer = NULL;
319 }
320 return NULL;
321 }
322
323 static gchar*
vala_value_map_lcopy_value(const GValue * value,guint n_collect_values,GTypeCValue * collect_values,guint collect_flags)324 vala_value_map_lcopy_value (const GValue* value,
325 guint n_collect_values,
326 GTypeCValue* collect_values,
327 guint collect_flags)
328 {
329 ValaMap ** object_p;
330 object_p = collect_values[0].v_pointer;
331 if (!object_p) {
332 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
333 }
334 if (!value->data[0].v_pointer) {
335 *object_p = NULL;
336 } else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
337 *object_p = value->data[0].v_pointer;
338 } else {
339 *object_p = vala_map_ref (value->data[0].v_pointer);
340 }
341 return NULL;
342 }
343
344 GParamSpec*
vala_param_spec_map(const gchar * name,const gchar * nick,const gchar * blurb,GType object_type,GParamFlags flags)345 vala_param_spec_map (const gchar* name,
346 const gchar* nick,
347 const gchar* blurb,
348 GType object_type,
349 GParamFlags flags)
350 {
351 ValaParamSpecMap* spec;
352 g_return_val_if_fail (g_type_is_a (object_type, VALA_TYPE_MAP), NULL);
353 spec = g_param_spec_internal (G_TYPE_PARAM_OBJECT, name, nick, blurb, flags);
354 G_PARAM_SPEC (spec)->value_type = object_type;
355 return G_PARAM_SPEC (spec);
356 }
357
358 gpointer
vala_value_get_map(const GValue * value)359 vala_value_get_map (const GValue* value)
360 {
361 g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, VALA_TYPE_MAP), NULL);
362 return value->data[0].v_pointer;
363 }
364
365 void
vala_value_set_map(GValue * value,gpointer v_object)366 vala_value_set_map (GValue* value,
367 gpointer v_object)
368 {
369 ValaMap * old;
370 g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, VALA_TYPE_MAP));
371 old = value->data[0].v_pointer;
372 if (v_object) {
373 g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, VALA_TYPE_MAP));
374 g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
375 value->data[0].v_pointer = v_object;
376 vala_map_ref (value->data[0].v_pointer);
377 } else {
378 value->data[0].v_pointer = NULL;
379 }
380 if (old) {
381 vala_map_unref (old);
382 }
383 }
384
385 void
vala_value_take_map(GValue * value,gpointer v_object)386 vala_value_take_map (GValue* value,
387 gpointer v_object)
388 {
389 ValaMap * old;
390 g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, VALA_TYPE_MAP));
391 old = value->data[0].v_pointer;
392 if (v_object) {
393 g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, VALA_TYPE_MAP));
394 g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
395 value->data[0].v_pointer = v_object;
396 } else {
397 value->data[0].v_pointer = NULL;
398 }
399 if (old) {
400 vala_map_unref (old);
401 }
402 }
403
404 static void
vala_map_class_init(ValaMapClass * klass,gpointer klass_data)405 vala_map_class_init (ValaMapClass * klass,
406 gpointer klass_data)
407 {
408 vala_map_parent_class = g_type_class_peek_parent (klass);
409 ((ValaMapClass *) klass)->finalize = vala_map_finalize;
410 g_type_class_adjust_private_offset (klass, &ValaMap_private_offset);
411 ((ValaMapClass *) klass)->get_keys = (ValaSet* (*) (ValaMap*)) vala_map_real_get_keys;
412 ((ValaMapClass *) klass)->get_values = (ValaCollection* (*) (ValaMap*)) vala_map_real_get_values;
413 ((ValaMapClass *) klass)->contains = (gboolean (*) (ValaMap*, gconstpointer)) vala_map_real_contains;
414 ((ValaMapClass *) klass)->get = (gpointer (*) (ValaMap*, gconstpointer)) vala_map_real_get;
415 ((ValaMapClass *) klass)->set = (void (*) (ValaMap*, gconstpointer, gconstpointer)) vala_map_real_set;
416 ((ValaMapClass *) klass)->remove = (gboolean (*) (ValaMap*, gconstpointer)) vala_map_real_remove;
417 ((ValaMapClass *) klass)->clear = (void (*) (ValaMap*)) vala_map_real_clear;
418 ((ValaMapClass *) klass)->map_iterator = (ValaMapIterator* (*) (ValaMap*)) vala_map_real_map_iterator;
419 }
420
421 static void
vala_map_instance_init(ValaMap * self,gpointer klass)422 vala_map_instance_init (ValaMap * self,
423 gpointer klass)
424 {
425 self->priv = vala_map_get_instance_private (self);
426 self->ref_count = 1;
427 }
428
429 static void
vala_map_finalize(ValaMap * obj)430 vala_map_finalize (ValaMap * obj)
431 {
432 ValaMap * self;
433 self = G_TYPE_CHECK_INSTANCE_CAST (obj, VALA_TYPE_MAP, ValaMap);
434 g_signal_handlers_destroy (self);
435 }
436
437 /**
438 * A map is a generic collection of key/value pairs.
439 */
440 static GType
vala_map_get_type_once(void)441 vala_map_get_type_once (void)
442 {
443 static const GTypeValueTable g_define_type_value_table = { vala_value_map_init, vala_value_map_free_value, vala_value_map_copy_value, vala_value_map_peek_pointer, "p", vala_value_map_collect_value, "p", vala_value_map_lcopy_value };
444 static const GTypeInfo g_define_type_info = { sizeof (ValaMapClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) vala_map_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ValaMap), 0, (GInstanceInitFunc) vala_map_instance_init, &g_define_type_value_table };
445 static const GTypeFundamentalInfo g_define_type_fundamental_info = { (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE) };
446 GType vala_map_type_id;
447 vala_map_type_id = g_type_register_fundamental (g_type_fundamental_next (), "ValaMap", &g_define_type_info, &g_define_type_fundamental_info, G_TYPE_FLAG_ABSTRACT);
448 ValaMap_private_offset = g_type_add_instance_private (vala_map_type_id, sizeof (ValaMapPrivate));
449 return vala_map_type_id;
450 }
451
452 GType
vala_map_get_type(void)453 vala_map_get_type (void)
454 {
455 static volatile gsize vala_map_type_id__volatile = 0;
456 if (g_once_init_enter (&vala_map_type_id__volatile)) {
457 GType vala_map_type_id;
458 vala_map_type_id = vala_map_get_type_once ();
459 g_once_init_leave (&vala_map_type_id__volatile, vala_map_type_id);
460 }
461 return vala_map_type_id__volatile;
462 }
463
464 gpointer
vala_map_ref(gpointer instance)465 vala_map_ref (gpointer instance)
466 {
467 ValaMap * self;
468 self = instance;
469 g_atomic_int_inc (&self->ref_count);
470 return instance;
471 }
472
473 void
vala_map_unref(gpointer instance)474 vala_map_unref (gpointer instance)
475 {
476 ValaMap * self;
477 self = instance;
478 if (g_atomic_int_dec_and_test (&self->ref_count)) {
479 VALA_MAP_GET_CLASS (self)->finalize (self);
480 g_type_free_instance ((GTypeInstance *) self);
481 }
482 }
483
484