1 /* lazy.c generated by valac 0.18.0, the Vala compiler
2 * generated from lazy.vala, do not modify */
6 * Copyright (C) 2011 Maciej Piechotka
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.
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.
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
23 * Maciej Piechotka <uzytkownik2@gmail.com>
27 #include <glib-object.h>
28 #include <gobject/gvaluecollector.h>
31 #define GEE_TYPE_LAZY (gee_lazy_get_type ())
32 #define GEE_LAZY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEE_TYPE_LAZY, GeeLazy))
33 #define GEE_LAZY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GEE_TYPE_LAZY, GeeLazyClass))
34 #define GEE_IS_LAZY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEE_TYPE_LAZY))
35 #define GEE_IS_LAZY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEE_TYPE_LAZY))
36 #define GEE_LAZY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GEE_TYPE_LAZY, GeeLazyClass))
38 typedef struct _GeeLazy GeeLazy;
39 typedef struct _GeeLazyClass GeeLazyClass;
40 typedef struct _GeeLazyPrivate GeeLazyPrivate;
41 #define _g_destroy_func0(var) (((var == NULL) || (g_destroy_func == NULL)) ? NULL : (var = (g_destroy_func (var), NULL)))
42 typedef struct _GeeParamSpecLazy GeeParamSpecLazy;
44 typedef gpointer (*GeeLazyFunc) (void* user_data);
46 GTypeInstance parent_instance;
47 volatile int ref_count;
48 GeeLazyPrivate * priv;
51 struct _GeeLazyClass {
52 GTypeClass parent_class;
53 void (*finalize) (GeeLazy *self);
56 struct _GeeLazyPrivate {
58 GBoxedCopyFunc g_dup_func;
59 GDestroyNotify g_destroy_func;
61 gpointer _func_target;
62 GDestroyNotify _func_target_destroy_notify;
66 struct _GeeParamSpecLazy {
67 GParamSpec parent_instance;
71 static gpointer gee_lazy_parent_class = NULL;
73 gpointer gee_lazy_ref (gpointer instance);
74 void gee_lazy_unref (gpointer instance);
75 GParamSpec* gee_param_spec_lazy (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
76 void gee_value_set_lazy (GValue* value, gpointer v_object);
77 void gee_value_take_lazy (GValue* value, gpointer v_object);
78 gpointer gee_value_get_lazy (const GValue* value);
79 GType gee_lazy_get_type (void) G_GNUC_CONST;
80 #define GEE_LAZY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GEE_TYPE_LAZY, GeeLazyPrivate))
82 GEE_LAZY_DUMMY_PROPERTY
84 GeeLazy* gee_lazy_new (GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, GeeLazyFunc func, void* func_target, GDestroyNotify func_target_destroy_notify);
85 GeeLazy* gee_lazy_construct (GType object_type, GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, GeeLazyFunc func, void* func_target, GDestroyNotify func_target_destroy_notify);
86 GeeLazy* gee_lazy_new_from_value (GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, gconstpointer item);
87 GeeLazy* gee_lazy_construct_from_value (GType object_type, GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, gconstpointer item);
88 void gee_lazy_eval (GeeLazy* self);
89 gpointer gee_lazy_get (GeeLazy* self);
90 gconstpointer gee_lazy_get_value (GeeLazy* self);
91 static void gee_lazy_finalize (GeeLazy* obj);
94 GeeLazy* gee_lazy_construct (GType object_type, GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, GeeLazyFunc func, void* func_target, GDestroyNotify func_target_destroy_notify) {
98 GDestroyNotify _tmp0__target_destroy_notify;
99 self = (GeeLazy*) g_type_create_instance (object_type);
100 self->priv->g_type = g_type;
101 self->priv->g_dup_func = g_dup_func;
102 self->priv->g_destroy_func = g_destroy_func;
104 _tmp0__target = func_target;
105 _tmp0__target_destroy_notify = func_target_destroy_notify;
106 func_target_destroy_notify = NULL;
107 (self->priv->_func_target_destroy_notify == NULL) ? NULL : (self->priv->_func_target_destroy_notify (self->priv->_func_target), NULL);
108 self->priv->_func = NULL;
109 self->priv->_func_target = NULL;
110 self->priv->_func_target_destroy_notify = NULL;
111 self->priv->_func = _tmp0_;
112 self->priv->_func_target = _tmp0__target;
113 self->priv->_func_target_destroy_notify = _tmp0__target_destroy_notify;
114 (func_target_destroy_notify == NULL) ? NULL : (func_target_destroy_notify (func_target), NULL);
117 func_target_destroy_notify = NULL;
122 GeeLazy* gee_lazy_new (GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, GeeLazyFunc func, void* func_target, GDestroyNotify func_target_destroy_notify) {
123 return gee_lazy_construct (GEE_TYPE_LAZY, g_type, g_dup_func, g_destroy_func, func, func_target, func_target_destroy_notify);
127 GeeLazy* gee_lazy_construct_from_value (GType object_type, GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, gconstpointer item) {
128 GeeLazy* self = NULL;
129 gconstpointer _tmp0_;
131 self = (GeeLazy*) g_type_create_instance (object_type);
132 self->priv->g_type = g_type;
133 self->priv->g_dup_func = g_dup_func;
134 self->priv->g_destroy_func = g_destroy_func;
136 _tmp1_ = ((_tmp0_ != NULL) && (g_dup_func != NULL)) ? g_dup_func ((gpointer) _tmp0_) : ((gpointer) _tmp0_);
137 _g_destroy_func0 (self->priv->_value);
138 self->priv->_value = _tmp1_;
143 GeeLazy* gee_lazy_new_from_value (GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, gconstpointer item) {
144 return gee_lazy_construct_from_value (GEE_TYPE_LAZY, g_type, g_dup_func, g_destroy_func, item);
148 void gee_lazy_eval (GeeLazy* self) {
151 g_return_if_fail (self != NULL);
152 _tmp0_ = self->priv->_func;
153 _tmp0__target = self->priv->_func_target;
154 if (_tmp0_ != NULL) {
157 gpointer _tmp2_ = NULL;
158 _tmp1_ = self->priv->_func;
159 _tmp1__target = self->priv->_func_target;
160 _tmp2_ = _tmp1_ (_tmp1__target);
161 ((self->priv->_value == NULL) || (self->priv->g_destroy_func == NULL)) ? NULL : (self->priv->_value = (self->priv->g_destroy_func (self->priv->_value), NULL));
162 self->priv->_value = _tmp2_;
163 (self->priv->_func_target_destroy_notify == NULL) ? NULL : (self->priv->_func_target_destroy_notify (self->priv->_func_target), NULL);
164 self->priv->_func = NULL;
165 self->priv->_func_target = NULL;
166 self->priv->_func_target_destroy_notify = NULL;
167 self->priv->_func = NULL;
168 self->priv->_func_target = NULL;
169 self->priv->_func_target_destroy_notify = NULL;
174 gpointer gee_lazy_get (GeeLazy* self) {
175 gpointer result = NULL;
176 gconstpointer _tmp0_;
178 g_return_val_if_fail (self != NULL, NULL);
179 gee_lazy_eval (self);
180 _tmp0_ = self->priv->_value;
181 _tmp1_ = ((_tmp0_ != NULL) && (self->priv->g_dup_func != NULL)) ? self->priv->g_dup_func ((gpointer) _tmp0_) : ((gpointer) _tmp0_);
187 gconstpointer gee_lazy_get_value (GeeLazy* self) {
188 gconstpointer result;
189 gconstpointer _tmp0_;
190 g_return_val_if_fail (self != NULL, NULL);
191 gee_lazy_eval (self);
192 _tmp0_ = self->priv->_value;
198 static void gee_value_lazy_init (GValue* value) {
199 value->data[0].v_pointer = NULL;
203 static void gee_value_lazy_free_value (GValue* value) {
204 if (value->data[0].v_pointer) {
205 gee_lazy_unref (value->data[0].v_pointer);
210 static void gee_value_lazy_copy_value (const GValue* src_value, GValue* dest_value) {
211 if (src_value->data[0].v_pointer) {
212 dest_value->data[0].v_pointer = gee_lazy_ref (src_value->data[0].v_pointer);
214 dest_value->data[0].v_pointer = NULL;
219 static gpointer gee_value_lazy_peek_pointer (const GValue* value) {
220 return value->data[0].v_pointer;
224 static gchar* gee_value_lazy_collect_value (GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) {
225 if (collect_values[0].v_pointer) {
227 object = collect_values[0].v_pointer;
228 if (object->parent_instance.g_class == NULL) {
229 return g_strconcat ("invalid unclassed object pointer for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
230 } else if (!g_value_type_compatible (G_TYPE_FROM_INSTANCE (object), G_VALUE_TYPE (value))) {
231 return g_strconcat ("invalid object type `", g_type_name (G_TYPE_FROM_INSTANCE (object)), "' for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
233 value->data[0].v_pointer = gee_lazy_ref (object);
235 value->data[0].v_pointer = NULL;
241 static gchar* gee_value_lazy_lcopy_value (const GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) {
243 object_p = collect_values[0].v_pointer;
245 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
247 if (!value->data[0].v_pointer) {
249 } else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
250 *object_p = value->data[0].v_pointer;
252 *object_p = gee_lazy_ref (value->data[0].v_pointer);
258 GParamSpec* gee_param_spec_lazy (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags) {
259 GeeParamSpecLazy* spec;
260 g_return_val_if_fail (g_type_is_a (object_type, GEE_TYPE_LAZY), NULL);
261 spec = g_param_spec_internal (G_TYPE_PARAM_OBJECT, name, nick, blurb, flags);
262 G_PARAM_SPEC (spec)->value_type = object_type;
263 return G_PARAM_SPEC (spec);
267 gpointer gee_value_get_lazy (const GValue* value) {
268 g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, GEE_TYPE_LAZY), NULL);
269 return value->data[0].v_pointer;
273 void gee_value_set_lazy (GValue* value, gpointer v_object) {
275 g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, GEE_TYPE_LAZY));
276 old = value->data[0].v_pointer;
278 g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, GEE_TYPE_LAZY));
279 g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
280 value->data[0].v_pointer = v_object;
281 gee_lazy_ref (value->data[0].v_pointer);
283 value->data[0].v_pointer = NULL;
286 gee_lazy_unref (old);
291 void gee_value_take_lazy (GValue* value, gpointer v_object) {
293 g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, GEE_TYPE_LAZY));
294 old = value->data[0].v_pointer;
296 g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, GEE_TYPE_LAZY));
297 g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
298 value->data[0].v_pointer = v_object;
300 value->data[0].v_pointer = NULL;
303 gee_lazy_unref (old);
308 static void gee_lazy_class_init (GeeLazyClass * klass) {
309 gee_lazy_parent_class = g_type_class_peek_parent (klass);
310 GEE_LAZY_CLASS (klass)->finalize = gee_lazy_finalize;
311 g_type_class_add_private (klass, sizeof (GeeLazyPrivate));
315 static void gee_lazy_instance_init (GeeLazy * self) {
316 self->priv = GEE_LAZY_GET_PRIVATE (self);
321 static void gee_lazy_finalize (GeeLazy* obj) {
323 self = G_TYPE_CHECK_INSTANCE_CAST (obj, GEE_TYPE_LAZY, GeeLazy);
324 (self->priv->_func_target_destroy_notify == NULL) ? NULL : (self->priv->_func_target_destroy_notify (self->priv->_func_target), NULL);
325 self->priv->_func = NULL;
326 self->priv->_func_target = NULL;
327 self->priv->_func_target_destroy_notify = NULL;
328 ((self->priv->_value == NULL) || (self->priv->g_destroy_func == NULL)) ? NULL : (self->priv->_value = (self->priv->g_destroy_func (self->priv->_value), NULL));
333 * Represents a lazy value. I.e. value that is computed on demand.
335 GType gee_lazy_get_type (void) {
336 static volatile gsize gee_lazy_type_id__volatile = 0;
337 if (g_once_init_enter (&gee_lazy_type_id__volatile)) {
338 static const GTypeValueTable g_define_type_value_table = { gee_value_lazy_init, gee_value_lazy_free_value, gee_value_lazy_copy_value, gee_value_lazy_peek_pointer, "p", gee_value_lazy_collect_value, "p", gee_value_lazy_lcopy_value };
339 static const GTypeInfo g_define_type_info = { sizeof (GeeLazyClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gee_lazy_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GeeLazy), 0, (GInstanceInitFunc) gee_lazy_instance_init, &g_define_type_value_table };
340 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) };
341 GType gee_lazy_type_id;
342 gee_lazy_type_id = g_type_register_fundamental (g_type_fundamental_next (), "GeeLazy", &g_define_type_info, &g_define_type_fundamental_info, 0);
343 g_once_init_leave (&gee_lazy_type_id__volatile, gee_lazy_type_id);
345 return gee_lazy_type_id__volatile;
349 gpointer gee_lazy_ref (gpointer instance) {
352 g_atomic_int_inc (&self->ref_count);
357 void gee_lazy_unref (gpointer instance) {
360 if (g_atomic_int_dec_and_test (&self->ref_count)) {
361 GEE_LAZY_GET_CLASS (self)->finalize (self);
362 g_type_free_instance ((GTypeInstance *) self);