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

/* gcalc-function-tanh.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 <glib.h>
#include <gio/gio.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <mpc.h>
#include <mpfr.h>
#include <gee.h>
#include "gcalc.h"

enum  {
	GCALC_FUNCTION_TANH_0_PROPERTY,
	GCALC_FUNCTION_TANH_NUM_PROPERTIES
};
static GParamSpec* gcalc_function_tanh_properties[GCALC_FUNCTION_TANH_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

static gpointer gcalc_function_tanh_parent_class = NULL;

VALA_EXTERN GCalcMathExpression* gcalc_function_evaluate (GCalcFunction* self,
                                              GError** error);
static GCalcMathExpression* gcalc_function_tanh_real_evaluate (GCalcFunction* base,
                                                        GError** error);
VALA_EXTERN void gcalc_constant_get_complex (GCalcConstant* self,
                                 __mpc_struct* result);
VALA_EXTERN GCalcConstant* gcalc_constant_new_internal_complex (__mpc_struct* complex);
VALA_EXTERN GCalcConstant* gcalc_constant_construct_internal_complex (GType object_type,
                                                          __mpc_struct* complex);
static GObject * gcalc_function_tanh_constructor (GType type,
                                           guint n_construct_properties,
                                           GObjectConstructParam * construct_properties);
static GType gcalc_function_tanh_get_type_once (void);

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

static GCalcMathExpression*
gcalc_function_tanh_real_evaluate (GCalcFunction* base,
                                   GError** error)
{
	GCalcFunctionTanh * self;
	GCalcConstant* c = NULL;
	GCalcMathExpression* exp = NULL;
	GCalcExpressionContainer* _tmp0_;
	GCalcExpressionContainer* _tmp1_;
	GObject* _tmp2_;
	GCalcMathExpression* _tmp3_;
	GCalcMathExpression* _tmp4_;
	GCalcMathResult* ev = NULL;
	GCalcMathExpression* _tmp7_;
	GCalcMathResult* _tmp8_;
	GCalcMathResult* _tmp9_;
	GCalcMathResult* _tmp14_;
	GCalcConstant* _tmp19_;
	__mpc_struct p1 = {0};
	GCalcConstant* _tmp21_;
	__mpc_struct _tmp22_ = {0};
	__mpc_struct res = {0};
	GCalcConstant* nc = NULL;
	GCalcConstant* _tmp23_;
	GCalcConstant* _tmp24_;
	GCalcMathExpression* _tmp25_;
	GError* _inner_error0_ = NULL;
	GCalcMathExpression* result;
	self = (GCalcFunctionTanh*) base;
	gcalc_math_function_verify_params ((GCalcMathFunction*) self, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		return NULL;
	}
	c = NULL;
	_tmp0_ = gcalc_math_expression_get_expressions ((GCalcMathExpression*) self);
	_tmp1_ = _tmp0_;
	_tmp2_ = g_list_model_get_item ((GListModel*) _tmp1_, (guint) 0);
	_tmp3_ = GCALC_IS_MATH_EXPRESSION (_tmp2_) ? ((GCalcMathExpression*) _tmp2_) : NULL;
	if (_tmp3_ == NULL) {
		_g_object_unref0 (_tmp2_);
	}
	exp = _tmp3_;
	_tmp4_ = exp;
	if (_tmp4_ == NULL) {
		const gchar* _tmp5_;
		GError* _tmp6_;
		_tmp5_ = g_type_name (GCALC_TYPE_MATH_EXPRESSION);
		_tmp6_ = g_error_new (GCALC_FUNCTION_ERROR, GCALC_FUNCTION_ERROR_INVOCATION_ERROR, "Invalid parameter type. Expected %s", _tmp5_);
		_inner_error0_ = _tmp6_;
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (exp);
		_g_object_unref0 (c);
		return NULL;
	}
	_tmp7_ = exp;
	_tmp8_ = gcalc_math_expression_solve (_tmp7_);
	ev = _tmp8_;
	_tmp9_ = ev;
	if (GCALC_IS_ERROR_RESULT (_tmp9_)) {
		GCalcMathResult* _tmp10_;
		const gchar* _tmp11_;
		const gchar* _tmp12_;
		GError* _tmp13_;
		_tmp10_ = ev;
		_tmp11_ = gcalc_math_error_result_get_message ((GCalcMathErrorResult*) G_TYPE_CHECK_INSTANCE_CAST (_tmp10_, GCALC_TYPE_ERROR_RESULT, GCalcErrorResult));
		_tmp12_ = _tmp11_;
		_tmp13_ = g_error_new (GCALC_FUNCTION_ERROR, GCALC_FUNCTION_ERROR_INVOCATION_ERROR, "Invalid expression: %s", _tmp12_);
		_inner_error0_ = _tmp13_;
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (ev);
		_g_object_unref0 (exp);
		_g_object_unref0 (c);
		return NULL;
	}
	_tmp14_ = ev;
	if (GCALC_IS_MATH_RESULT (_tmp14_)) {
		GCalcMathResult* _tmp15_;
		GCalcMathExpression* _tmp16_;
		GCalcMathExpression* _tmp17_;
		GCalcConstant* _tmp18_;
		_tmp15_ = ev;
		_tmp16_ = gcalc_math_result_get_expression (G_TYPE_CHECK_INSTANCE_CAST (_tmp15_, GCALC_TYPE_MATH_RESULT, GCalcMathResult));
		_tmp17_ = _tmp16_;
		_tmp18_ = _g_object_ref0 (GCALC_IS_CONSTANT (_tmp17_) ? ((GCalcConstant*) _tmp17_) : NULL);
		_g_object_unref0 (c);
		c = _tmp18_;
	}
	_tmp19_ = c;
	if (_tmp19_ == NULL) {
		GError* _tmp20_;
		_tmp20_ = g_error_new_literal (GCALC_FUNCTION_ERROR, GCALC_FUNCTION_ERROR_INVOCATION_ERROR, "Invalid expression in result");
		_inner_error0_ = _tmp20_;
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (ev);
		_g_object_unref0 (exp);
		_g_object_unref0 (c);
		return NULL;
	}
	mpc_init2 (&p1, (mpfr_prec_t) 1000);
	_tmp21_ = c;
	gcalc_constant_get_complex (_tmp21_, &_tmp22_);
	mpc_set (&p1, &_tmp22_, MPC_RNDNN);
	mpc_init2 (&res, (mpfr_prec_t) 1000);
	mpc_tanh (&res, &p1, MPC_RNDNN);
	_tmp23_ = gcalc_constant_new_internal_complex (&res);
	nc = _tmp23_;
	_tmp24_ = nc;
	_tmp25_ = _g_object_ref0 (GCALC_IS_MATH_EXPRESSION (_tmp24_) ? ((GCalcMathExpression*) _tmp24_) : NULL);
	result = _tmp25_;
	_g_object_unref0 (nc);
	mpc_clear (&res);
	mpc_clear (&p1);
	_g_object_unref0 (ev);
	_g_object_unref0 (exp);
	_g_object_unref0 (c);
	return result;
}

GCalcFunctionTanh*
gcalc_function_tanh_construct (GType object_type)
{
	GCalcFunctionTanh * self = NULL;
	self = (GCalcFunctionTanh*) gcalc_function_construct (object_type);
	return self;
}

GCalcFunctionTanh*
gcalc_function_tanh_new (void)
{
	return gcalc_function_tanh_construct (GCALC_TYPE_FUNCTION_TANH);
}

static GObject *
gcalc_function_tanh_constructor (GType type,
                                 guint n_construct_properties,
                                 GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	GCalcFunctionTanh * self;
	GCalcExpressionContainer* _tmp0_;
	GCalcExpressionContainer* _tmp1_;
	GCalcConstant* _tmp2_;
	GCalcConstant* _tmp3_;
	parent_class = G_OBJECT_CLASS (gcalc_function_tanh_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GCALC_TYPE_FUNCTION_TANH, GCalcFunctionTanh);
	gcalc_math_function_set_name ((GCalcMathFunction*) self, "tanh");
	gcalc_math_function_set_n_params ((GCalcMathFunction*) self, (guint) 1);
	_tmp0_ = gcalc_math_function_get_param_types ((GCalcMathFunction*) self);
	_tmp1_ = _tmp0_;
	_tmp2_ = gcalc_constant_new ();
	_tmp3_ = _tmp2_;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp1_, (GCalcMathExpression*) _tmp3_);
	_g_object_unref0 (_tmp3_);
	return obj;
}

static void
gcalc_function_tanh_class_init (GCalcFunctionTanhClass * klass,
                                gpointer klass_data)
{
	gcalc_function_tanh_parent_class = g_type_class_peek_parent (klass);
	((GCalcFunctionClass *) klass)->evaluate = (GCalcMathExpression* (*) (GCalcFunction*, GError**)) gcalc_function_tanh_real_evaluate;
	G_OBJECT_CLASS (klass)->constructor = gcalc_function_tanh_constructor;
}

static void
gcalc_function_tanh_instance_init (GCalcFunctionTanh * self,
                                   gpointer klass)
{
}

/**
 * Function to calculate hyperbolic tangent of a {@link MathConstant} in radians
 */
static GType
gcalc_function_tanh_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GCalcFunctionTanhClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gcalc_function_tanh_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GCalcFunctionTanh), 0, (GInstanceInitFunc) gcalc_function_tanh_instance_init, NULL };
	GType gcalc_function_tanh_type_id;
	gcalc_function_tanh_type_id = g_type_register_static (GCALC_TYPE_FUNCTION, "GCalcFunctionTanh", &g_define_type_info, 0);
	return gcalc_function_tanh_type_id;
}

GType
gcalc_function_tanh_get_type (void)
{
	static volatile gsize gcalc_function_tanh_type_id__once = 0;
	if (g_once_init_enter (&gcalc_function_tanh_type_id__once)) {
		GType gcalc_function_tanh_type_id;
		gcalc_function_tanh_type_id = gcalc_function_tanh_get_type_once ();
		g_once_init_leave (&gcalc_function_tanh_type_id__once, gcalc_function_tanh_type_id);
	}
	return gcalc_function_tanh_type_id__once;
}

