/* async-owned-delegate.c generated by valac, the Vala compiler
 * generated from async-owned-delegate.vala, do not modify */


#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>
#include <gobject/gvaluecollector.h>


#define TYPE_ASYNC_DELEGATE (async_delegate_get_type ())
#define ASYNC_DELEGATE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_ASYNC_DELEGATE, AsyncDelegate))
#define ASYNC_DELEGATE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_ASYNC_DELEGATE, AsyncDelegateClass))
#define IS_ASYNC_DELEGATE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_ASYNC_DELEGATE))
#define IS_ASYNC_DELEGATE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_ASYNC_DELEGATE))
#define ASYNC_DELEGATE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_ASYNC_DELEGATE, AsyncDelegateClass))

typedef struct _AsyncDelegate AsyncDelegate;
typedef struct _AsyncDelegateClass AsyncDelegateClass;
typedef struct _AsyncDelegatePrivate AsyncDelegatePrivate;
typedef struct _AsyncDelegateTestAsyncData AsyncDelegateTestAsyncData;
#define _g_main_loop_unref0(var) ((var == NULL) ? NULL : (var = (g_main_loop_unref (var), NULL)))
#define _async_delegate_unref0(var) ((var == NULL) ? NULL : (var = (async_delegate_unref (var), NULL)))
typedef struct _ParamSpecAsyncDelegate ParamSpecAsyncDelegate;

struct _AsyncDelegate {
	GTypeInstance parent_instance;
	volatile int ref_count;
	AsyncDelegatePrivate * priv;
};

struct _AsyncDelegateClass {
	GTypeClass parent_class;
	void (*finalize) (AsyncDelegate *self);
};

struct _AsyncDelegateTestAsyncData {
	int _state_;
	GAsyncResult* _res_;
	GSimpleAsyncResult* _async_result;
	AsyncDelegate* self;
	AsyncDelegateTestFunc f;
	gpointer f_target;
};

typedef void (*AsyncDelegateTestFunc) (void* user_data);
struct _ParamSpecAsyncDelegate {
	GParamSpec parent_instance;
};


static gpointer async_delegate_parent_class = NULL;

gpointer async_delegate_ref (gpointer instance);
void async_delegate_unref (gpointer instance);
GParamSpec* param_spec_async_delegate (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void value_set_async_delegate (GValue* value, gpointer v_object);
gpointer value_get_async_delegate (const GValue* value);
GType async_delegate_get_type (void);
enum  {
	ASYNC_DELEGATE_DUMMY_PROPERTY
};
static void async_delegate_test_func (AsyncDelegate* self);
static void async_delegate_test_async_data_free (gpointer _data);
static void async_delegate_test_async_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_);
static void async_delegate_test_async (AsyncDelegate* self, AsyncDelegateTestFunc f, void* f_target, GDestroyNotify f_target_destroy_notify, GAsyncReadyCallback _callback_, gpointer _user_data_);
static void async_delegate_test_finish (AsyncDelegate* self, GAsyncResult* _res_);
static gboolean _async_delegate_test_async_co_gsource_func (gpointer self);
static gboolean async_delegate_test_async_co (AsyncDelegateTestAsyncData* data);
AsyncDelegate* async_delegate_new (void);
AsyncDelegate* async_delegate_construct (GType object_type);
static void _async_delegate_test_func_async_delegate_test_func (gpointer self);
static gint async_delegate_main (char** args, int args_length1);
static void async_delegate_finalize (AsyncDelegate* obj);



static void async_delegate_test_func (AsyncDelegate* self) {
	g_return_if_fail (self != NULL);
	g_print ("delegate\n");
}


static void async_delegate_test_async_data_free (gpointer _data) {
	AsyncDelegateTestAsyncData* data;
	data = _data;
	(data->f_target_destroy_notify == NULL) ? NULL : data->f_target_destroy_notify (data->f_target);
	data->f = NULL;
	data->f_target = NULL;
	data->f_target_destroy_notify = NULL;
	g_slice_free (AsyncDelegateTestAsyncData, data);
}


static void async_delegate_test_async (AsyncDelegate* self, AsyncDelegateTestFunc f, void* f_target, GDestroyNotify f_target_destroy_notify, GAsyncReadyCallback _callback_, gpointer _user_data_) {
	AsyncDelegateTestAsyncData* _data_;
	_data_ = g_slice_new0 (AsyncDelegateTestAsyncData);
	_data_->_async_result = g_simple_async_result_new (NULL, _callback_, _user_data_, async_delegate_test_async);
	g_simple_async_result_set_op_res_gpointer (_data_->_async_result, _data_, async_delegate_test_async_data_free);
	_data_->self = self;
	_data_->f = f;
	_data_->f_target = f_target;
	async_delegate_test_async_co (_data_);
}


static void async_delegate_test_finish (AsyncDelegate* self, GAsyncResult* _res_) {
	AsyncDelegateTestAsyncData* _data_;
	_data_ = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (_res_));
}


static void async_delegate_test_async_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_) {
	AsyncDelegateTestAsyncData* data;
	data = _user_data_;
	data->_res_ = _res_;
	async_delegate_test_async_co (data);
}


static gboolean _async_delegate_test_async_co_gsource_func (gpointer self) {
	return async_delegate_test_async_co (self);
}


static gboolean async_delegate_test_async_co (AsyncDelegateTestAsyncData* data) {
	switch (data->_state_) {
		default:
		g_assert_not_reached ();
		case 0:
		{
			g_print ("async enter\n");
			g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, _async_delegate_test_async_co_gsource_func, data, NULL);
			data->_state_ = 1;
			return FALSE;
			case 1:
			;
			data->f (data->f_target);
			g_print ("async leave\n");
			(data->f_target_destroy_notify == NULL) ? NULL : data->f_target_destroy_notify (data->f_target);
			data->f = NULL;
			data->f_target = NULL;
			data->f_target_destroy_notify = NULL;
		}
		{
			if (data->_state_ == 0) {
				g_simple_async_result_complete_in_idle (data->_async_result);
			} else {
				g_simple_async_result_complete (data->_async_result);
			}
			g_object_unref (data->_async_result);
			return FALSE;
		}
	}
}


static void _async_delegate_test_func_async_delegate_test_func (gpointer self) {
	async_delegate_test_func (self);
}


static gint async_delegate_main (char** args, int args_length1) {
	gint result;
	GMainLoop* loop;
	AsyncDelegate* app;
	loop = g_main_loop_new (NULL, FALSE);
	app = async_delegate_new ();
	async_delegate_test_async (app, _async_delegate_test_func_async_delegate_test_func, async_delegate_ref (app), async_delegate_unref, NULL, NULL);
	g_print ("loop\n");
	g_main_loop_run (loop);
	result = 0;
	_g_main_loop_unref0 (loop);
	_async_delegate_unref0 (app);
	return result;
}


int main (int argc, char ** argv) {
	g_type_init ();
	return async_delegate_main (argv, argc);
}


AsyncDelegate* async_delegate_construct (GType object_type) {
	AsyncDelegate* self;
	self = (AsyncDelegate*) g_type_create_instance (object_type);
	return self;
}


AsyncDelegate* async_delegate_new (void) {
	return async_delegate_construct (TYPE_ASYNC_DELEGATE);
}


static void value_async_delegate_init (GValue* value) {
	value->data[0].v_pointer = NULL;
}


static void value_async_delegate_free_value (GValue* value) {
	if (value->data[0].v_pointer) {
		async_delegate_unref (value->data[0].v_pointer);
	}
}


static void value_async_delegate_copy_value (const GValue* src_value, GValue* dest_value) {
	if (src_value->data[0].v_pointer) {
		dest_value->data[0].v_pointer = async_delegate_ref (src_value->data[0].v_pointer);
	} else {
		dest_value->data[0].v_pointer = NULL;
	}
}


static gpointer value_async_delegate_peek_pointer (const GValue* value) {
	return value->data[0].v_pointer;
}


static gchar* value_async_delegate_collect_value (GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) {
	if (collect_values[0].v_pointer) {
		AsyncDelegate* object;
		object = collect_values[0].v_pointer;
		if (object->parent_instance.g_class == NULL) {
			return g_strconcat ("invalid unclassed object pointer for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		} else if (!g_value_type_compatible (G_TYPE_FROM_INSTANCE (object), G_VALUE_TYPE (value))) {
			return g_strconcat ("invalid object type `", g_type_name (G_TYPE_FROM_INSTANCE (object)), "' for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		}
		value->data[0].v_pointer = async_delegate_ref (object);
	} else {
		value->data[0].v_pointer = NULL;
	}
	return NULL;
}


static gchar* value_async_delegate_lcopy_value (const GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) {
	AsyncDelegate** object_p;
	object_p = collect_values[0].v_pointer;
	if (!object_p) {
		return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
	}
	if (!value->data[0].v_pointer) {
		*object_p = NULL;
	} else if (collect_flags && G_VALUE_NOCOPY_CONTENTS) {
		*object_p = value->data[0].v_pointer;
	} else {
		*object_p = async_delegate_ref (value->data[0].v_pointer);
	}
	return NULL;
}


GParamSpec* param_spec_async_delegate (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags) {
	ParamSpecAsyncDelegate* spec;
	g_return_val_if_fail (g_type_is_a (object_type, TYPE_ASYNC_DELEGATE), NULL);
	spec = g_param_spec_internal (G_TYPE_PARAM_OBJECT, name, nick, blurb, flags);
	G_PARAM_SPEC (spec)->value_type = object_type;
	return G_PARAM_SPEC (spec);
}


gpointer value_get_async_delegate (const GValue* value) {
	g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_ASYNC_DELEGATE), NULL);
	return value->data[0].v_pointer;
}


void value_set_async_delegate (GValue* value, gpointer v_object) {
	AsyncDelegate* old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_ASYNC_DELEGATE));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_ASYNC_DELEGATE));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
		async_delegate_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		async_delegate_unref (old);
	}
}


static void async_delegate_class_init (AsyncDelegateClass * klass) {
	async_delegate_parent_class = g_type_class_peek_parent (klass);
	ASYNC_DELEGATE_CLASS (klass)->finalize = async_delegate_finalize;
}


static void async_delegate_instance_init (AsyncDelegate * self) {
	self->ref_count = 1;
}


static void async_delegate_finalize (AsyncDelegate* obj) {
	AsyncDelegate * self;
	self = ASYNC_DELEGATE (obj);
}


GType async_delegate_get_type (void) {
	static GType async_delegate_type_id = 0;
	if (async_delegate_type_id == 0) {
		static const GTypeValueTable g_define_type_value_table = { value_async_delegate_init, value_async_delegate_free_value, value_async_delegate_copy_value, value_async_delegate_peek_pointer, "p", value_async_delegate_collect_value, "p", value_async_delegate_lcopy_value };
		static const GTypeInfo g_define_type_info = { sizeof (AsyncDelegateClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) async_delegate_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (AsyncDelegate), 0, (GInstanceInitFunc) async_delegate_instance_init, &g_define_type_value_table };
		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) };
		async_delegate_type_id = g_type_register_fundamental (g_type_fundamental_next (), "AsyncDelegate", &g_define_type_info, &g_define_type_fundamental_info, 0);
	}
	return async_delegate_type_id;
}


gpointer async_delegate_ref (gpointer instance) {
	AsyncDelegate* self;
	self = instance;
	g_atomic_int_inc (&self->ref_count);
	return instance;
}


void async_delegate_unref (gpointer instance) {
	AsyncDelegate* self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		ASYNC_DELEGATE_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}




