/* FontCache.c generated by valac 0.56.17, the Vala compiler
 * generated from FontCache.vala, do not modify */

/*
Copyright (C) 2014 Johan Mattsson

This library is free software; you can redistribute it and/or modify 
it under the terms of the GNU Lesser General Public License as 
published by the Free Software Foundation; either version 3 of the 
License, or (at your option) any later version.

This library 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 
Lesser General Public License for more details.
*/

#include "birdfont.h"
#include <gee.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <stdio.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

#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _bird_font_font_cache_unref0(var) ((var == NULL) ? NULL : (var = (bird_font_font_cache_unref (var), NULL)))
typedef struct _BirdFontParamSpecFontCache BirdFontParamSpecFontCache;

struct _BirdFontFontCachePrivate {
	GeeHashMap* fonts;
	BirdFontCachedFont* fallback;
};

struct _BirdFontParamSpecFontCache {
	GParamSpec parent_instance;
};

static gint BirdFontFontCache_private_offset;
static gpointer bird_font_font_cache_parent_class = NULL;
BirdFontFallbackFont* bird_font_font_cache_fallback_font = NULL;
static BirdFontFontCache* bird_font_font_cache_default_cache;
static BirdFontFontCache* bird_font_font_cache_default_cache = NULL;

static void bird_font_font_cache_finalize (BirdFontFontCache * obj);
static GType bird_font_font_cache_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
bird_font_font_cache_get_instance_private (BirdFontFontCache* self)
{
	return G_STRUCT_MEMBER_P (self, BirdFontFontCache_private_offset);
}

BirdFontFontCache*
bird_font_font_cache_construct (GType object_type)
{
	BirdFontFontCache* self = NULL;
	BirdFontFallbackFont* _tmp0_;
	BirdFontCachedFont* _tmp2_;
	GeeHashMap* _tmp3_;
	self = (BirdFontFontCache*) g_type_create_instance (object_type);
	_tmp0_ = bird_font_font_cache_fallback_font;
	if (bird_font_is_null (_tmp0_)) {
		BirdFontFallbackFont* _tmp1_;
		_tmp1_ = bird_font_fallback_font_new ();
		_g_object_unref0 (bird_font_font_cache_fallback_font);
		bird_font_font_cache_fallback_font = _tmp1_;
	}
	_tmp2_ = bird_font_cached_font_new (NULL);
	_g_object_unref0 (self->priv->fallback);
	self->priv->fallback = _tmp2_;
	_tmp3_ = gee_hash_map_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, BIRD_FONT_TYPE_CACHED_FONT, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
	_g_object_unref0 (self->priv->fonts);
	self->priv->fonts = _tmp3_;
	return self;
}

BirdFontFontCache*
bird_font_font_cache_new (void)
{
	return bird_font_font_cache_construct (BIRD_FONT_TYPE_FONT_CACHE);
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

BirdFontCachedFont*
bird_font_font_cache_get_font (BirdFontFontCache* self,
                               const gchar* file_name)
{
	BirdFontCachedFont* c = NULL;
	BirdFontFont* f = NULL;
	gboolean ok = FALSE;
	GeeHashMap* _tmp2_;
	BirdFontFont* _tmp5_;
	BirdFontFont* _tmp6_;
	BirdFontFont* _tmp7_;
	BirdFontFont* _tmp10_;
	BirdFontCachedFont* _tmp11_;
	GeeHashMap* _tmp12_;
	BirdFontCachedFont* _tmp13_;
	BirdFontCachedFont* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (file_name != NULL, NULL);
	if (g_strcmp0 (file_name, "") == 0) {
		BirdFontCachedFont* _tmp0_;
		BirdFontCachedFont* _tmp1_;
		_tmp0_ = self->priv->fallback;
		_tmp1_ = _g_object_ref0 (_tmp0_);
		result = _tmp1_;
		_g_object_unref0 (f);
		_g_object_unref0 (c);
		return result;
	}
	_tmp2_ = self->priv->fonts;
	if (gee_abstract_map_has_key ((GeeAbstractMap*) _tmp2_, file_name)) {
		GeeHashMap* _tmp3_;
		gpointer _tmp4_;
		_tmp3_ = self->priv->fonts;
		_tmp4_ = gee_abstract_map_get ((GeeAbstractMap*) _tmp3_, file_name);
		_g_object_unref0 (c);
		c = (BirdFontCachedFont*) _tmp4_;
		result = c;
		_g_object_unref0 (f);
		return result;
	}
	_tmp5_ = bird_font_font_new ();
	_g_object_unref0 (f);
	f = _tmp5_;
	_tmp6_ = f;
	bird_font_font_set_file (_tmp6_, file_name);
	_tmp7_ = f;
	ok = bird_font_font_load (_tmp7_);
	if (!ok) {
		FILE* _tmp8_;
		BirdFontCachedFont* _tmp9_;
		_tmp8_ = stderr;
		fprintf (_tmp8_, "Can't load %s\n", file_name);
		_tmp9_ = bird_font_cached_font_new (NULL);
		result = _tmp9_;
		_g_object_unref0 (f);
		_g_object_unref0 (c);
		return result;
	}
	_tmp10_ = f;
	_tmp11_ = bird_font_cached_font_new (_tmp10_);
	_g_object_unref0 (c);
	c = _tmp11_;
	if (g_strcmp0 (file_name, "") == 0) {
		g_warning ("FontCache.vala:61: No file.");
		result = c;
		_g_object_unref0 (f);
		return result;
	}
	_tmp12_ = self->priv->fonts;
	_tmp13_ = c;
	gee_abstract_map_set ((GeeAbstractMap*) _tmp12_, file_name, _tmp13_);
	result = c;
	_g_object_unref0 (f);
	return result;
}

static gpointer
_bird_font_font_cache_ref0 (gpointer self)
{
	return self ? bird_font_font_cache_ref (self) : NULL;
}

BirdFontFontCache*
bird_font_font_cache_get_default_cache (void)
{
	BirdFontFontCache* _tmp0_;
	BirdFontFontCache* _tmp2_;
	BirdFontFontCache* _tmp3_;
	BirdFontFontCache* result;
	_tmp0_ = bird_font_font_cache_default_cache;
	if (_tmp0_ == NULL) {
		BirdFontFontCache* _tmp1_;
		_tmp1_ = bird_font_font_cache_new ();
		_bird_font_font_cache_unref0 (bird_font_font_cache_default_cache);
		bird_font_font_cache_default_cache = _tmp1_;
	}
	_tmp2_ = bird_font_font_cache_default_cache;
	_tmp3_ = _bird_font_font_cache_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp2_, BIRD_FONT_TYPE_FONT_CACHE, BirdFontFontCache));
	result = _tmp3_;
	return result;
}

BirdFontCachedFont*
bird_font_font_cache_get_fallback (BirdFontFontCache* self)
{
	BirdFontCachedFont* _tmp0_;
	BirdFontCachedFont* _tmp1_;
	BirdFontCachedFont* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->fallback;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	result = _tmp1_;
	return result;
}

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

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

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

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

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

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

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

void
bird_font_value_set_font_cache (GValue* value,
                                gpointer v_object)
{
	BirdFontFontCache * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, BIRD_FONT_TYPE_FONT_CACHE));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, BIRD_FONT_TYPE_FONT_CACHE));
		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;
		bird_font_font_cache_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		bird_font_font_cache_unref (old);
	}
}

void
bird_font_value_take_font_cache (GValue* value,
                                 gpointer v_object)
{
	BirdFontFontCache * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, BIRD_FONT_TYPE_FONT_CACHE));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, BIRD_FONT_TYPE_FONT_CACHE));
		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) {
		bird_font_font_cache_unref (old);
	}
}

static void
bird_font_font_cache_class_init (BirdFontFontCacheClass * klass,
                                 gpointer klass_data)
{
	bird_font_font_cache_parent_class = g_type_class_peek_parent (klass);
	((BirdFontFontCacheClass *) klass)->finalize = bird_font_font_cache_finalize;
	g_type_class_adjust_private_offset (klass, &BirdFontFontCache_private_offset);
}

static void
bird_font_font_cache_instance_init (BirdFontFontCache * self,
                                    gpointer klass)
{
	self->priv = bird_font_font_cache_get_instance_private (self);
	self->ref_count = 1;
}

static void
bird_font_font_cache_finalize (BirdFontFontCache * obj)
{
	BirdFontFontCache * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BIRD_FONT_TYPE_FONT_CACHE, BirdFontFontCache);
	g_signal_handlers_destroy (self);
	_g_object_unref0 (self->priv->fonts);
	_g_object_unref0 (self->priv->fallback);
}

/** Thread specific font cache. */
static GType
bird_font_font_cache_get_type_once (void)
{
	static const GTypeValueTable g_define_type_value_table = { bird_font_value_font_cache_init, bird_font_value_font_cache_free_value, bird_font_value_font_cache_copy_value, bird_font_value_font_cache_peek_pointer, "p", bird_font_value_font_cache_collect_value, "p", bird_font_value_font_cache_lcopy_value };
	static const GTypeInfo g_define_type_info = { sizeof (BirdFontFontCacheClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) bird_font_font_cache_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BirdFontFontCache), 0, (GInstanceInitFunc) bird_font_font_cache_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 bird_font_font_cache_type_id;
	bird_font_font_cache_type_id = g_type_register_fundamental (g_type_fundamental_next (), "BirdFontFontCache", &g_define_type_info, &g_define_type_fundamental_info, 0);
	BirdFontFontCache_private_offset = g_type_add_instance_private (bird_font_font_cache_type_id, sizeof (BirdFontFontCachePrivate));
	return bird_font_font_cache_type_id;
}

GType
bird_font_font_cache_get_type (void)
{
	static volatile gsize bird_font_font_cache_type_id__once = 0;
	if (g_once_init_enter (&bird_font_font_cache_type_id__once)) {
		GType bird_font_font_cache_type_id;
		bird_font_font_cache_type_id = bird_font_font_cache_get_type_once ();
		g_once_init_leave (&bird_font_font_cache_type_id__once, bird_font_font_cache_type_id);
	}
	return bird_font_font_cache_type_id__once;
}

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

void
bird_font_font_cache_unref (gpointer instance)
{
	BirdFontFontCache * self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		BIRD_FONT_FONT_CACHE_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

