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

/*
 * This file is part of gitg
 *
 * Copyright (C) 2013 - Jesse van den Kieboom
 *
 * gitg is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * gitg 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gitg. If not, see <http://www.gnu.org/licenses/>.
 */

#include "libgitg.h"
#include <gio/gio.h>
#include <glib.h>
#include <glib-object.h>
#include <gobject/gvaluecollector.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

typedef struct _Block1Data Block1Data;
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_thread_unref0(var) ((var == NULL) ? NULL : (var = (g_thread_unref (var), NULL)))
typedef struct _GitgAsyncThreadData GitgAsyncThreadData;
typedef struct _GitgAsyncThreadTryData GitgAsyncThreadTryData;
typedef struct _GitgParamSpecAsync GitgParamSpecAsync;

struct _Block1Data {
	int _ref_count_;
	GSourceFunc callback;
	gpointer callback_target;
	GDestroyNotify callback_target_destroy_notify;
	GError* err;
	GitgAsyncThreadFunc func;
	gpointer func_target;
	gpointer _async_data_;
};

struct _GitgAsyncThreadData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	GitgAsyncThreadFunc func;
	gpointer func_target;
	Block1Data* _data1_;
	GThread* t;
	GThread* _tmp0_;
	GThread* _tmp1_;
	GError* _tmp2_;
	GError* _tmp3_;
	GError* _tmp4_;
	GError* _inner_error0_;
};

struct _GitgAsyncThreadTryData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	GitgAsyncThreadFunc func;
	gpointer func_target;
	GError* _inner_error0_;
};

struct _GitgParamSpecAsync {
	GParamSpec parent_instance;
};

static gpointer gitg_async_parent_class = NULL;

static void gitg_async_thread_data_free (gpointer _data);
static gboolean gitg_async_thread_co (GitgAsyncThreadData* _data_);
static Block1Data* block1_data_ref (Block1Data* _data1_);
static void block1_data_unref (void * _userdata_);
static gboolean _gitg_async_thread_co_gsource_func (gpointer self);
static void* __lambda4_ (Block1Data* _data1_);
static gpointer ___lambda4__gthread_func (gpointer self);
static void gitg_async_thread_try_data_free (gpointer _data);
static gboolean gitg_async_thread_try_co (GitgAsyncThreadTryData* _data_);
static void gitg_async_thread_try_ready (GObject* source_object,
                                  GAsyncResult* _res_,
                                  gpointer _user_data_);
static void gitg_async_finalize (GitgAsync * obj);
static GType gitg_async_get_type_once (void);

static void
gitg_async_thread_data_free (gpointer _data)
{
	GitgAsyncThreadData* _data_;
	_data_ = _data;
	g_slice_free (GitgAsyncThreadData, _data_);
}

void
gitg_async_thread (GitgAsyncThreadFunc func,
                   gpointer func_target,
                   GAsyncReadyCallback _callback_,
                   gpointer _user_data_)
{
	GitgAsyncThreadData* _data_;
	_data_ = g_slice_new0 (GitgAsyncThreadData);
	_data_->_async_result = g_task_new (NULL, NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, gitg_async_thread_data_free);
	_data_->func = func;
	_data_->func_target = func_target;
	gitg_async_thread_co (_data_);
}

void
gitg_async_thread_finish (GAsyncResult* _res_,
                          GError** error)
{
	GitgAsyncThreadData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		return;
	}
}

static Block1Data*
block1_data_ref (Block1Data* _data1_)
{
	g_atomic_int_inc (&_data1_->_ref_count_);
	return _data1_;
}

static void
block1_data_unref (void * _userdata_)
{
	Block1Data* _data1_;
	_data1_ = (Block1Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data1_->_ref_count_)) {
		_g_error_free0 (_data1_->err);
		(_data1_->callback_target_destroy_notify == NULL) ? NULL : (_data1_->callback_target_destroy_notify (_data1_->callback_target), NULL);
		_data1_->callback = NULL;
		_data1_->callback_target = NULL;
		_data1_->callback_target_destroy_notify = NULL;
		g_slice_free (Block1Data, _data1_);
	}
}

static gboolean
_gitg_async_thread_co_gsource_func (gpointer self)
{
	gboolean result;
	result = gitg_async_thread_co (self);
	return result;
}

static gpointer
_g_error_copy0 (gpointer self)
{
	return self ? g_error_copy (self) : NULL;
}

static void*
__lambda4_ (Block1Data* _data1_)
{
	GSourceFunc _tmp2_;
	gpointer _tmp2__target;
	GDestroyNotify _tmp2__target_destroy_notify;
	GError* _inner_error0_ = NULL;
	void* result;
	{
		_data1_->func (_data1_->func_target, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp0_;
		GError* _tmp1_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp0_ = e;
		_tmp1_ = _g_error_copy0 (_tmp0_);
		_g_error_free0 (_data1_->err);
		_data1_->err = _tmp1_;
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return NULL;
	}
	_tmp2_ = _data1_->callback;
	_tmp2__target = _data1_->callback_target;
	_tmp2__target_destroy_notify = _data1_->callback_target_destroy_notify;
	_data1_->callback = NULL;
	_data1_->callback_target = NULL;
	_data1_->callback_target_destroy_notify = NULL;
	g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, _tmp2_, _tmp2__target, _tmp2__target_destroy_notify);
	result = NULL;
	return result;
}

static gpointer
___lambda4__gthread_func (gpointer self)
{
	gpointer result;
	result = __lambda4_ (self);
	block1_data_unref (self);
	return result;
}

static gboolean
gitg_async_thread_co (GitgAsyncThreadData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_data1_ = g_slice_new0 (Block1Data);
	_data_->_data1_->_ref_count_ = 1;
	_data_->_data1_->func = _data_->func;
	_data_->_data1_->func_target = _data_->func_target;
	_data_->_data1_->_async_data_ = _data_;
	_data_->_data1_->callback = _gitg_async_thread_co_gsource_func;
	_data_->_data1_->callback_target = _data_;
	_data_->_data1_->callback_target_destroy_notify = NULL;
	_data_->_data1_->err = NULL;
	_data_->_tmp0_ = g_thread_try_new ("gitg-async", ___lambda4__gthread_func, block1_data_ref (_data_->_data1_), &_data_->_inner_error0_);
	_data_->t = _data_->_tmp0_;
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
		block1_data_unref (_data_->_data1_);
		_data_->_data1_ = NULL;
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_state_ = 1;
	return FALSE;
	_state_1:
	;
	_data_->_tmp1_ = _data_->t;
	_data_->t = NULL;
	g_thread_join (_data_->_tmp1_);
	_data_->_tmp2_ = _data_->_data1_->err;
	if (_data_->_tmp2_ != NULL) {
		_data_->_tmp3_ = _data_->_data1_->err;
		_data_->_tmp4_ = _g_error_copy0 (_data_->_tmp3_);
		_data_->_inner_error0_ = _data_->_tmp4_;
		g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
		_g_thread_unref0 (_data_->t);
		block1_data_unref (_data_->_data1_);
		_data_->_data1_ = NULL;
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_g_thread_unref0 (_data_->t);
	block1_data_unref (_data_->_data1_);
	_data_->_data1_ = NULL;
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

static void
gitg_async_thread_try_data_free (gpointer _data)
{
	GitgAsyncThreadTryData* _data_;
	_data_ = _data;
	g_slice_free (GitgAsyncThreadTryData, _data_);
}

void
gitg_async_thread_try (GitgAsyncThreadFunc func,
                       gpointer func_target,
                       GAsyncReadyCallback _callback_,
                       gpointer _user_data_)
{
	GitgAsyncThreadTryData* _data_;
	_data_ = g_slice_new0 (GitgAsyncThreadTryData);
	_data_->_async_result = g_task_new (NULL, NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, gitg_async_thread_try_data_free);
	_data_->func = func;
	_data_->func_target = func_target;
	gitg_async_thread_try_co (_data_);
}

void
gitg_async_thread_try_finish (GAsyncResult* _res_)
{
	GitgAsyncThreadTryData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
}

static void
gitg_async_thread_try_ready (GObject* source_object,
                             GAsyncResult* _res_,
                             gpointer _user_data_)
{
	GitgAsyncThreadTryData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	gitg_async_thread_try_co (_data_);
}

static gboolean
gitg_async_thread_try_co (GitgAsyncThreadTryData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	{
		_data_->_state_ = 1;
		gitg_async_thread (_data_->func, _data_->func_target, gitg_async_thread_try_ready, _data_);
		return FALSE;
		_state_1:
		gitg_async_thread_finish (_data_->_res_, &_data_->_inner_error0_);
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_data_->_inner_error0_);
	}
	__finally0:
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error0_->message, g_quark_to_string (_data_->_inner_error0_->domain), _data_->_inner_error0_->code);
		g_clear_error (&_data_->_inner_error0_);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

GitgAsync*
gitg_async_construct (GType object_type)
{
	GitgAsync* self = NULL;
	self = (GitgAsync*) g_type_create_instance (object_type);
	return self;
}

GitgAsync*
gitg_async_new (void)
{
	return gitg_async_construct (GITG_TYPE_ASYNC);
}

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

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

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

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

static gchar*
gitg_value_async_collect_value (GValue* value,
                                guint n_collect_values,
                                GTypeCValue* collect_values,
                                guint collect_flags)
{
	if (collect_values[0].v_pointer) {
		GitgAsync * 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 = gitg_async_ref (object);
	} else {
		value->data[0].v_pointer = NULL;
	}
	return NULL;
}

static gchar*
gitg_value_async_lcopy_value (const GValue* value,
                              guint n_collect_values,
                              GTypeCValue* collect_values,
                              guint collect_flags)
{
	GitgAsync ** 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 = gitg_async_ref (value->data[0].v_pointer);
	}
	return NULL;
}

GParamSpec*
gitg_param_spec_async (const gchar* name,
                       const gchar* nick,
                       const gchar* blurb,
                       GType object_type,
                       GParamFlags flags)
{
	GitgParamSpecAsync* spec;
	g_return_val_if_fail (g_type_is_a (object_type, GITG_TYPE_ASYNC), 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
gitg_value_get_async (const GValue* value)
{
	g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, GITG_TYPE_ASYNC), NULL);
	return value->data[0].v_pointer;
}

void
gitg_value_set_async (GValue* value,
                      gpointer v_object)
{
	GitgAsync * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, GITG_TYPE_ASYNC));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, GITG_TYPE_ASYNC));
		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;
		gitg_async_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		gitg_async_unref (old);
	}
}

void
gitg_value_take_async (GValue* value,
                       gpointer v_object)
{
	GitgAsync * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, GITG_TYPE_ASYNC));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, GITG_TYPE_ASYNC));
		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;
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		gitg_async_unref (old);
	}
}

static void
gitg_async_class_init (GitgAsyncClass * klass,
                       gpointer klass_data)
{
	gitg_async_parent_class = g_type_class_peek_parent (klass);
	((GitgAsyncClass *) klass)->finalize = gitg_async_finalize;
}

static void
gitg_async_instance_init (GitgAsync * self,
                          gpointer klass)
{
	self->ref_count = 1;
}

static void
gitg_async_finalize (GitgAsync * obj)
{
	GitgAsync * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GITG_TYPE_ASYNC, GitgAsync);
	g_signal_handlers_destroy (self);
}

 G_GNUC_NO_INLINE static GType
gitg_async_get_type_once (void)
{
	static const GTypeValueTable g_define_type_value_table = { gitg_value_async_init, gitg_value_async_free_value, gitg_value_async_copy_value, gitg_value_async_peek_pointer, "p", gitg_value_async_collect_value, "p", gitg_value_async_lcopy_value };
	static const GTypeInfo g_define_type_info = { sizeof (GitgAsyncClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gitg_async_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GitgAsync), 0, (GInstanceInitFunc) gitg_async_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) };
	GType gitg_async_type_id;
	gitg_async_type_id = g_type_register_fundamental (g_type_fundamental_next (), "GitgAsync", &g_define_type_info, &g_define_type_fundamental_info, 0);
	return gitg_async_type_id;
}

GType
gitg_async_get_type (void)
{
	static gsize gitg_async_type_id__once = 0;
	if (g_once_init_enter (&gitg_async_type_id__once)) {
		GType gitg_async_type_id;
		gitg_async_type_id = gitg_async_get_type_once ();
		g_once_init_leave (&gitg_async_type_id__once, gitg_async_type_id);
	}
	return gitg_async_type_id__once;
}

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

void
gitg_async_unref (gpointer instance)
{
	GitgAsync * self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		GITG_ASYNC_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}

