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

/* -*- Mode: vala; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
/*
 *
 * Copyright (C) 2016  Yannick Inizan <inizan.yannick@gmail.com>
 * Copyright (C) 2016  Daniel Espinosa <esodan@gmail.com>
 *
 * 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 2.1 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.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *     Yannick Inizan <inizan.yannick@gmail.com>
 *     Daniel Espinosa <esodan@gmail.com>
 */

#include "gxml/gxml.h"
#include <libxml/xpath.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.h>
#include <glib-object.h>
#include <gee.h>
#include <libxml/tree.h>
#include "gxml.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

enum  {
	GXML_LX_PATH_OBJECT_0_PROPERTY,
	GXML_LX_PATH_OBJECT_OBJECT_TYPE_PROPERTY,
	GXML_LX_PATH_OBJECT_BOOLEAN_VALUE_PROPERTY,
	GXML_LX_PATH_OBJECT_STRING_VALUE_PROPERTY,
	GXML_LX_PATH_OBJECT_NUMBER_VALUE_PROPERTY,
	GXML_LX_PATH_OBJECT_NODESET_PROPERTY,
	GXML_LX_PATH_OBJECT_NUM_PROPERTIES
};
static GParamSpec* gxml_lx_path_object_properties[GXML_LX_PATH_OBJECT_NUM_PROPERTIES];
#define _xmlXPathFreeObject0(var) ((var == NULL) ? NULL : (var = (xmlXPathFreeObject (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _GXmlLXPathObjectPrivate {
	GXmlHTMLCollection* _collection;
	GXmlXPathObjectType _object_type;
	xmlXPathObject* _pointer;
	gboolean _boolean_value;
	gchar* _string_value;
	gdouble _number_value;
};

static gint GXmlLXPathObject_private_offset;
static gpointer gxml_lx_path_object_parent_class = NULL;
static GXmlXPathObjectIface * gxml_lx_path_object_gxml_xpath_object_parent_iface = NULL;

static void gxml_lx_path_object_finalize (GObject * obj);
static GType gxml_lx_path_object_get_type_once (void);
static void _vala_gxml_lx_path_object_get_property (GObject * object,
                                             guint property_id,
                                             GValue * value,
                                             GParamSpec * pspec);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
gxml_lx_path_object_get_instance_private (GXmlLXPathObject* self)
{
	return G_STRUCT_MEMBER_P (self, GXmlLXPathObject_private_offset);
}

GXmlLXPathObject*
gxml_lx_path_object_construct (GType object_type,
                               GXmlXDocument* document,
                               xmlXPathObject* pointer)
{
	GXmlLXPathObject * self = NULL;
	GXmlHTMLCollection* _tmp0_;
	xmlXPathObject* _tmp1_;
	g_return_val_if_fail (document != NULL, NULL);
	self = (GXmlLXPathObject*) g_object_new (object_type, NULL);
	_tmp0_ = gxml_html_collection_new ();
	_g_object_unref0 (self->priv->_collection);
	self->priv->_collection = _tmp0_;
	self->priv->_pointer = pointer;
	_tmp1_ = self->priv->_pointer;
	self->priv->_object_type = (GXmlXPathObjectType) _tmp1_->type;
	if (self->priv->_object_type == GXML_XPATH_OBJECT_TYPE_NODESET) {
		{
			gint i = 0;
			i = 0;
			{
				gboolean _tmp2_ = FALSE;
				_tmp2_ = TRUE;
				while (TRUE) {
					xmlXPathObject* _tmp4_;
					xmlNodeSet* _tmp5_;
					GXmlHTMLCollection* _tmp6_;
					xmlXPathObject* _tmp7_;
					xmlNodeSet* _tmp8_;
					xmlNode* _tmp9_;
					GXmlXElement* _tmp10_;
					GXmlXElement* _tmp11_;
					if (!_tmp2_) {
						gint _tmp3_;
						_tmp3_ = i;
						i = _tmp3_ + 1;
					}
					_tmp2_ = FALSE;
					_tmp4_ = self->priv->_pointer;
					_tmp5_ = _tmp4_->nodesetval;
					if (!(i < xmlXPathNodeSetGetLength (_tmp5_))) {
						break;
					}
					_tmp6_ = self->priv->_collection;
					_tmp7_ = self->priv->_pointer;
					_tmp8_ = _tmp7_->nodesetval;
					_tmp9_ = xmlXPathNodeSetItem (_tmp8_, i);
					_tmp10_ = gxml_xelement_new (document, _tmp9_);
					_tmp11_ = _tmp10_;
					gee_abstract_collection_add ((GeeAbstractCollection*) _tmp6_, (GXmlDomElement*) _tmp11_);
					_g_object_unref0 (_tmp11_);
				}
			}
		}
	} else {
		if (self->priv->_object_type == GXML_XPATH_OBJECT_TYPE_BOOLEAN) {
			xmlXPathObject* _tmp12_;
			_tmp12_ = self->priv->_pointer;
			self->priv->_boolean_value = _tmp12_->boolval == 1;
		} else {
			if (self->priv->_object_type == GXML_XPATH_OBJECT_TYPE_STRING) {
				xmlXPathObject* _tmp13_;
				const gchar* _tmp14_;
				gchar* _tmp15_;
				_tmp13_ = self->priv->_pointer;
				_tmp14_ = _tmp13_->stringval;
				_tmp15_ = g_strdup (_tmp14_);
				_g_free0 (self->priv->_string_value);
				self->priv->_string_value = _tmp15_;
			} else {
				GXmlXPathObjectType _tmp16_;
				GXmlXPathObjectType _tmp17_;
				_tmp16_ = gxml_xpath_object_get_object_type ((GXmlXPathObject*) self);
				_tmp17_ = _tmp16_;
				if (_tmp17_ == GXML_XPATH_OBJECT_TYPE_NUMBER) {
					xmlXPathObject* _tmp18_;
					_tmp18_ = self->priv->_pointer;
					self->priv->_number_value = _tmp18_->floatval;
				}
			}
		}
	}
	return self;
}

GXmlLXPathObject*
gxml_lx_path_object_new (GXmlXDocument* document,
                         xmlXPathObject* pointer)
{
	return gxml_lx_path_object_construct (GXML_TYPE_LX_PATH_OBJECT, document, pointer);
}

static GXmlXPathObjectType
gxml_lx_path_object_real_get_object_type (GXmlXPathObject* base)
{
	GXmlXPathObjectType result;
	GXmlLXPathObject* self;
	self = (GXmlLXPathObject*) base;
	result = self->priv->_object_type;
	return result;
}

static gboolean
gxml_lx_path_object_real_get_boolean_value (GXmlXPathObject* base)
{
	gboolean result;
	GXmlLXPathObject* self;
	self = (GXmlLXPathObject*) base;
	result = self->priv->_boolean_value;
	return result;
}

static const gchar*
gxml_lx_path_object_real_get_string_value (GXmlXPathObject* base)
{
	const gchar* result;
	GXmlLXPathObject* self;
	const gchar* _tmp0_;
	self = (GXmlLXPathObject*) base;
	_tmp0_ = self->priv->_string_value;
	result = _tmp0_;
	return result;
}

static gdouble
gxml_lx_path_object_real_get_number_value (GXmlXPathObject* base)
{
	gdouble result;
	GXmlLXPathObject* self;
	self = (GXmlLXPathObject*) base;
	result = self->priv->_number_value;
	return result;
}

static GXmlDomHTMLCollection*
gxml_lx_path_object_real_get_nodeset (GXmlXPathObject* base)
{
	GXmlDomHTMLCollection* result;
	GXmlLXPathObject* self;
	GXmlHTMLCollection* _tmp0_;
	self = (GXmlLXPathObject*) base;
	_tmp0_ = self->priv->_collection;
	result = (GXmlDomHTMLCollection*) _tmp0_;
	return result;
}

static void
gxml_lx_path_object_class_init (GXmlLXPathObjectClass * klass,
                                gpointer klass_data)
{
	gxml_lx_path_object_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GXmlLXPathObject_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_gxml_lx_path_object_get_property;
	G_OBJECT_CLASS (klass)->finalize = gxml_lx_path_object_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), GXML_LX_PATH_OBJECT_OBJECT_TYPE_PROPERTY, gxml_lx_path_object_properties[GXML_LX_PATH_OBJECT_OBJECT_TYPE_PROPERTY] = g_param_spec_enum ("object-type", "object-type", "object-type", GXML_TYPE_XPATH_OBJECT_TYPE, 0, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), GXML_LX_PATH_OBJECT_BOOLEAN_VALUE_PROPERTY, gxml_lx_path_object_properties[GXML_LX_PATH_OBJECT_BOOLEAN_VALUE_PROPERTY] = g_param_spec_boolean ("boolean-value", "boolean-value", "boolean-value", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), GXML_LX_PATH_OBJECT_STRING_VALUE_PROPERTY, gxml_lx_path_object_properties[GXML_LX_PATH_OBJECT_STRING_VALUE_PROPERTY] = g_param_spec_string ("string-value", "string-value", "string-value", NULL, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), GXML_LX_PATH_OBJECT_NUMBER_VALUE_PROPERTY, gxml_lx_path_object_properties[GXML_LX_PATH_OBJECT_NUMBER_VALUE_PROPERTY] = g_param_spec_double ("number-value", "number-value", "number-value", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), GXML_LX_PATH_OBJECT_NODESET_PROPERTY, gxml_lx_path_object_properties[GXML_LX_PATH_OBJECT_NODESET_PROPERTY] = g_param_spec_object ("nodeset", "nodeset", "nodeset", GXML_TYPE_DOM_HTML_COLLECTION, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
}

static void
gxml_lx_path_object_gxml_xpath_object_interface_init (GXmlXPathObjectIface * iface,
                                                      gpointer iface_data)
{
	gxml_lx_path_object_gxml_xpath_object_parent_iface = g_type_interface_peek_parent (iface);
	iface->get_object_type = gxml_lx_path_object_real_get_object_type;
	iface->get_boolean_value = gxml_lx_path_object_real_get_boolean_value;
	iface->get_string_value = gxml_lx_path_object_real_get_string_value;
	iface->get_number_value = gxml_lx_path_object_real_get_number_value;
	iface->get_nodeset = gxml_lx_path_object_real_get_nodeset;
}

static void
gxml_lx_path_object_instance_init (GXmlLXPathObject * self,
                                   gpointer klass)
{
	self->priv = gxml_lx_path_object_get_instance_private (self);
	self->priv->_pointer = NULL;
}

static void
gxml_lx_path_object_finalize (GObject * obj)
{
	GXmlLXPathObject * self;
	xmlXPathObject* _tmp0_;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GXML_TYPE_LX_PATH_OBJECT, GXmlLXPathObject);
	_tmp0_ = self->priv->_pointer;
	if (_tmp0_ != NULL) {
		xmlXPathObject* _tmp1_;
		_tmp1_ = self->priv->_pointer;
		_xmlXPathFreeObject0 (_tmp1_);
		self->priv->_pointer = NULL;
	}
	_g_object_unref0 (self->priv->_collection);
	_g_free0 (self->priv->_string_value);
	G_OBJECT_CLASS (gxml_lx_path_object_parent_class)->finalize (obj);
}

/**
 * An {@link GXml.XPathObject} implementation using
 * libxml2 engine.
 */
static GType
gxml_lx_path_object_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GXmlLXPathObjectClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gxml_lx_path_object_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GXmlLXPathObject), 0, (GInstanceInitFunc) gxml_lx_path_object_instance_init, NULL };
	static const GInterfaceInfo gxml_xpath_object_info = { (GInterfaceInitFunc) gxml_lx_path_object_gxml_xpath_object_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	GType gxml_lx_path_object_type_id;
	gxml_lx_path_object_type_id = g_type_register_static (G_TYPE_OBJECT, "GXmlLXPathObject", &g_define_type_info, 0);
	g_type_add_interface_static (gxml_lx_path_object_type_id, GXML_TYPE_XPATH_OBJECT, &gxml_xpath_object_info);
	GXmlLXPathObject_private_offset = g_type_add_instance_private (gxml_lx_path_object_type_id, sizeof (GXmlLXPathObjectPrivate));
	return gxml_lx_path_object_type_id;
}

GType
gxml_lx_path_object_get_type (void)
{
	static volatile gsize gxml_lx_path_object_type_id__once = 0;
	if (g_once_init_enter (&gxml_lx_path_object_type_id__once)) {
		GType gxml_lx_path_object_type_id;
		gxml_lx_path_object_type_id = gxml_lx_path_object_get_type_once ();
		g_once_init_leave (&gxml_lx_path_object_type_id__once, gxml_lx_path_object_type_id);
	}
	return gxml_lx_path_object_type_id__once;
}

static void
_vala_gxml_lx_path_object_get_property (GObject * object,
                                        guint property_id,
                                        GValue * value,
                                        GParamSpec * pspec)
{
	GXmlLXPathObject * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GXML_TYPE_LX_PATH_OBJECT, GXmlLXPathObject);
	switch (property_id) {
		case GXML_LX_PATH_OBJECT_OBJECT_TYPE_PROPERTY:
		g_value_set_enum (value, gxml_xpath_object_get_object_type ((GXmlXPathObject*) self));
		break;
		case GXML_LX_PATH_OBJECT_BOOLEAN_VALUE_PROPERTY:
		g_value_set_boolean (value, gxml_xpath_object_get_boolean_value ((GXmlXPathObject*) self));
		break;
		case GXML_LX_PATH_OBJECT_STRING_VALUE_PROPERTY:
		g_value_set_string (value, gxml_xpath_object_get_string_value ((GXmlXPathObject*) self));
		break;
		case GXML_LX_PATH_OBJECT_NUMBER_VALUE_PROPERTY:
		g_value_set_double (value, gxml_xpath_object_get_number_value ((GXmlXPathObject*) self));
		break;
		case GXML_LX_PATH_OBJECT_NODESET_PROPERTY:
		g_value_set_object (value, gxml_xpath_object_get_nodeset ((GXmlXPathObject*) self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

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;
}

