/* gcalc-parameter.c generated by valac 0.56.0, the Vala compiler
 * generated from gcalc-parameter.vala, do not modify */

/* gcalc-gparameter.vala
 *
 * Copyright (C) 2019  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:
 *      Daniel Espinosa <esodan@gmail.com>
 */

#include "gcalc/gcalc.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib-object.h>
#include <float.h>
#include <math.h>
#include "gcalc.h"

enum  {
	GCALC_PARAMETER_0_PROPERTY,
	GCALC_PARAMETER_NUM_PROPERTIES
};
static GParamSpec* gcalc_parameter_properties[GCALC_PARAMETER_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

static gpointer gcalc_parameter_parent_class = NULL;
static GCalcMathParameterIface * gcalc_parameter_gcalc_math_parameter_parent_iface = NULL;

VALA_EXTERN gchar* gcalc_expression_to_string (GCalcExpression* self);
VALA_EXTERN GCalcVariable* gcalc_variable_new (const gchar* name);
VALA_EXTERN GCalcVariable* gcalc_variable_construct (GType object_type,
                                         const gchar* name);
static void gcalc_parameter_real_set_value (GCalcMathParameter* base,
                                     GValue* val,
                                     GError** error);
static GValue* gcalc_parameter_real_get_value (GCalcMathParameter* base);
static GValue* _g_value_dup (GValue* self);
static gchar* gcalc_parameter_real_to_string (GCalcExpression* base);
static GType gcalc_parameter_get_type_once (void);

GCalcParameter*
gcalc_parameter_construct (GType object_type,
                           const gchar* name)
{
	GCalcParameter * self = NULL;
	g_return_val_if_fail (name != NULL, NULL);
	self = (GCalcParameter*) gcalc_variable_construct (object_type, name);
	return self;
}

GCalcParameter*
gcalc_parameter_new (const gchar* name)
{
	return gcalc_parameter_construct (GCALC_TYPE_PARAMETER, name);
}

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

static void
gcalc_parameter_real_set_value (GCalcMathParameter* base,
                                GValue* val,
                                GError** error)
{
	GCalcParameter * self;
	GCalcMathConstant* c = NULL;
	GCalcConstant* _tmp0_;
	GCalcMathConstant* _tmp5_;
	self = (GCalcParameter*) base;
	if (((gpointer) val) == NULL) {
		gcalc_math_variable_set_value ((GCalcMathVariable*) self, NULL);
		return;
	}
	_tmp0_ = gcalc_constant_new_integer (0);
	c = (GCalcMathConstant*) _tmp0_;
	if (G_VALUE_HOLDS (val, G_TYPE_INT)) {
		GCalcConstant* _tmp1_;
		_tmp1_ = gcalc_constant_new_integer (g_value_get_int (val));
		_g_object_unref0 (c);
		c = (GCalcMathConstant*) _tmp1_;
	} else {
		if (G_VALUE_HOLDS (val, G_TYPE_DOUBLE)) {
			GCalcConstant* _tmp2_;
			_tmp2_ = gcalc_constant_new_double (g_value_get_double (val));
			_g_object_unref0 (c);
			c = (GCalcMathConstant*) _tmp2_;
		} else {
			if (G_VALUE_HOLDS (val, G_TYPE_FLOAT)) {
				GCalcConstant* _tmp3_;
				_tmp3_ = gcalc_constant_new_double ((gdouble) g_value_get_float (val));
				_g_object_unref0 (c);
				c = (GCalcMathConstant*) _tmp3_;
			} else {
				if (g_type_is_a (G_VALUE_TYPE (val), GCALC_TYPE_MATH_CONSTANT)) {
					GCalcMathConstant* _tmp4_;
					_tmp4_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (g_value_get_object (val), GCALC_TYPE_MATH_CONSTANT, GCalcMathConstant));
					_g_object_unref0 (c);
					c = _tmp4_;
				}
			}
		}
	}
	_tmp5_ = c;
	gcalc_math_variable_set_value ((GCalcMathVariable*) self, _tmp5_);
	_g_object_unref0 (c);
}

static GValue*
_g_value_dup (GValue* self)
{
	return g_boxed_copy (G_TYPE_VALUE, self);
}

static gpointer
__g_value_dup0 (gpointer self)
{
	return self ? _g_value_dup (self) : NULL;
}

static GValue*
gcalc_parameter_real_get_value (GCalcMathParameter* base)
{
	GCalcParameter * self;
	GCalcMathConstant* _tmp0_;
	GCalcMathConstant* _tmp1_;
	GValue v = {0};
	GValue _tmp2_ = {0};
	GCalcMathConstant* _tmp3_;
	GCalcMathConstant* _tmp4_;
	GValue _tmp5_ = {0};
	GValue _tmp6_;
	GValue* _tmp7_;
	GValue* _tmp8_;
	GValue* result;
	self = (GCalcParameter*) base;
	_tmp0_ = gcalc_math_variable_get_value ((GCalcMathVariable*) self);
	_tmp1_ = _tmp0_;
	if (_tmp1_ == NULL) {
		result = NULL;
		return result;
	}
	g_value_init (&_tmp2_, GCALC_TYPE_MATH_CONSTANT);
	v = _tmp2_;
	_tmp3_ = gcalc_math_variable_get_value ((GCalcMathVariable*) self);
	_tmp4_ = _tmp3_;
	g_value_init (&_tmp5_, GCALC_TYPE_MATH_CONSTANT);
	g_value_set_object (&_tmp5_, _tmp4_);
	G_IS_VALUE (&v) ? (g_value_unset (&v), NULL) : NULL;
	v = _tmp5_;
	_tmp6_ = v;
	_tmp7_ = __g_value_dup0 (&_tmp6_);
	_tmp8_ = _tmp7_;
	G_IS_VALUE (&_tmp6_) ? (g_value_unset (&_tmp6_), NULL) : NULL;
	result = _tmp8_;
	return result;
}

static gchar*
gcalc_parameter_real_to_string (GCalcExpression* base)
{
	GCalcParameter * self;
	GCalcMathConstant* _tmp0_;
	GCalcMathConstant* _tmp1_;
	GCalcMathConstant* _tmp5_;
	GCalcMathConstant* _tmp6_;
	gchar* _tmp7_;
	gchar* result;
	self = (GCalcParameter*) base;
	_tmp0_ = gcalc_math_variable_get_value ((GCalcMathVariable*) self);
	_tmp1_ = _tmp0_;
	if (_tmp1_ == NULL) {
		const gchar* _tmp2_;
		const gchar* _tmp3_;
		gchar* _tmp4_;
		_tmp2_ = gcalc_math_variable_get_name ((GCalcMathVariable*) self);
		_tmp3_ = _tmp2_;
		_tmp4_ = g_strconcat ("$", _tmp3_, NULL);
		result = _tmp4_;
		return result;
	}
	_tmp5_ = gcalc_math_variable_get_value ((GCalcMathVariable*) self);
	_tmp6_ = _tmp5_;
	_tmp7_ = gcalc_math_expression_to_string ((GCalcMathExpression*) _tmp6_);
	result = _tmp7_;
	return result;
}

static void
gcalc_parameter_class_init (GCalcParameterClass * klass,
                            gpointer klass_data)
{
	gcalc_parameter_parent_class = g_type_class_peek_parent (klass);
	((GCalcExpressionClass *) klass)->to_string = (gchar* (*) (GCalcExpression*)) gcalc_parameter_real_to_string;
}

static void
gcalc_parameter_gcalc_math_parameter_interface_init (GCalcMathParameterIface * iface,
                                                     gpointer iface_data)
{
	gcalc_parameter_gcalc_math_parameter_parent_iface = g_type_interface_peek_parent (iface);
	iface->set_value = (void (*) (GCalcMathParameter*, GValue*, GError**)) gcalc_parameter_real_set_value;
	iface->get_value = (GValue* (*) (GCalcMathParameter*)) gcalc_parameter_real_get_value;
}

static void
gcalc_parameter_instance_init (GCalcParameter * self,
                               gpointer klass)
{
}

/**
 * An implementation of {@link MathParameter}
 */
static GType
gcalc_parameter_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GCalcParameterClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gcalc_parameter_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GCalcParameter), 0, (GInstanceInitFunc) gcalc_parameter_instance_init, NULL };
	static const GInterfaceInfo gcalc_math_parameter_info = { (GInterfaceInitFunc) gcalc_parameter_gcalc_math_parameter_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	GType gcalc_parameter_type_id;
	gcalc_parameter_type_id = g_type_register_static (GCALC_TYPE_VARIABLE, "GCalcParameter", &g_define_type_info, 0);
	g_type_add_interface_static (gcalc_parameter_type_id, GCALC_TYPE_MATH_PARAMETER, &gcalc_math_parameter_info);
	return gcalc_parameter_type_id;
}

GType
gcalc_parameter_get_type (void)
{
	static volatile gsize gcalc_parameter_type_id__once = 0;
	if (g_once_init_enter (&gcalc_parameter_type_id__once)) {
		GType gcalc_parameter_type_id;
		gcalc_parameter_type_id = gcalc_parameter_get_type_once ();
		g_once_init_leave (&gcalc_parameter_type_id__once, gcalc_parameter_type_id);
	}
	return gcalc_parameter_type_id__once;
}

