/* state-multiplexer.c generated by valac 0.24.0, the Vala compiler
 * generated from state-multiplexer.vala, do not modify */

/* 
 * SmartSim - Digital Logic Circuit Designer and Simulator
 *   
 *   This program 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 3 of the License, or
 *   (at your option) any later version.
 *   
 *   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *   
 *   Filename: componentstate/state-multiplexer.vala
 *   
 *   Copyright Ashley Newson 2013
 */

#include <glib.h>
#include <glib-object.h>
#include <cairo.h>


#define TYPE_COMPONENT_STATE (component_state_get_type ())
#define COMPONENT_STATE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_COMPONENT_STATE, ComponentState))
#define COMPONENT_STATE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_COMPONENT_STATE, ComponentStateClass))
#define IS_COMPONENT_STATE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_COMPONENT_STATE))
#define IS_COMPONENT_STATE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_COMPONENT_STATE))
#define COMPONENT_STATE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_COMPONENT_STATE, ComponentStateClass))

typedef struct _ComponentState ComponentState;
typedef struct _ComponentStateClass ComponentStateClass;
typedef struct _ComponentStatePrivate ComponentStatePrivate;

#define TYPE_COMPILED_CIRCUIT (compiled_circuit_get_type ())
#define COMPILED_CIRCUIT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_COMPILED_CIRCUIT, CompiledCircuit))
#define COMPILED_CIRCUIT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_COMPILED_CIRCUIT, CompiledCircuitClass))
#define IS_COMPILED_CIRCUIT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_COMPILED_CIRCUIT))
#define IS_COMPILED_CIRCUIT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_COMPILED_CIRCUIT))
#define COMPILED_CIRCUIT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_COMPILED_CIRCUIT, CompiledCircuitClass))

typedef struct _CompiledCircuit CompiledCircuit;
typedef struct _CompiledCircuitClass CompiledCircuitClass;

#define TYPE_COMPONENT_INST (component_inst_get_type ())
#define COMPONENT_INST(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_COMPONENT_INST, ComponentInst))
#define COMPONENT_INST_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_COMPONENT_INST, ComponentInstClass))
#define IS_COMPONENT_INST(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_COMPONENT_INST))
#define IS_COMPONENT_INST_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_COMPONENT_INST))
#define COMPONENT_INST_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_COMPONENT_INST, ComponentInstClass))

typedef struct _ComponentInst ComponentInst;
typedef struct _ComponentInstClass ComponentInstClass;

#define TYPE_MULTIPLEXER_COMPONENT_STATE (multiplexer_component_state_get_type ())
#define MULTIPLEXER_COMPONENT_STATE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MULTIPLEXER_COMPONENT_STATE, MultiplexerComponentState))
#define MULTIPLEXER_COMPONENT_STATE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MULTIPLEXER_COMPONENT_STATE, MultiplexerComponentStateClass))
#define IS_MULTIPLEXER_COMPONENT_STATE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MULTIPLEXER_COMPONENT_STATE))
#define IS_MULTIPLEXER_COMPONENT_STATE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MULTIPLEXER_COMPONENT_STATE))
#define MULTIPLEXER_COMPONENT_STATE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MULTIPLEXER_COMPONENT_STATE, MultiplexerComponentStateClass))

typedef struct _MultiplexerComponentState MultiplexerComponentState;
typedef struct _MultiplexerComponentStateClass MultiplexerComponentStateClass;
typedef struct _MultiplexerComponentStatePrivate MultiplexerComponentStatePrivate;

#define TYPE_CONNECTION (connection_get_type ())
#define CONNECTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_CONNECTION, Connection))
#define CONNECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_CONNECTION, ConnectionClass))
#define IS_CONNECTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_CONNECTION))
#define IS_CONNECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_CONNECTION))
#define CONNECTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_CONNECTION, ConnectionClass))

typedef struct _Connection Connection;
typedef struct _ConnectionClass ConnectionClass;
#define _connection_unref0(var) ((var == NULL) ? NULL : (var = (connection_unref (var), NULL)))
#define _component_inst_unref0(var) ((var == NULL) ? NULL : (var = (component_inst_unref (var), NULL)))

struct _ComponentState {
	GTypeInstance parent_instance;
	volatile int ref_count;
	ComponentStatePrivate * priv;
	CompiledCircuit* compiledCircuit;
	ComponentInst** ancestry;
	gint ancestry_length1;
	ComponentInst* componentInst;
	gint renderQueueID;
	gint processQueueID;
	gboolean display;
};

struct _ComponentStateClass {
	GTypeClass parent_class;
	void (*finalize) (ComponentState *self);
	void (*update) (ComponentState* self);
	void (*click) (ComponentState* self);
	void (*render) (ComponentState* self, cairo_t* context);
	gboolean (*get_alwaysUpdate) (ComponentState* self);
};

struct _MultiplexerComponentState {
	ComponentState parent_instance;
	MultiplexerComponentStatePrivate * priv;
};

struct _MultiplexerComponentStateClass {
	ComponentStateClass parent_class;
};

struct _MultiplexerComponentStatePrivate {
	Connection** selectWires;
	gint selectWires_length1;
	gint _selectWires_size_;
	Connection** dataWires;
	gint dataWires_length1;
	gint _dataWires_size_;
	Connection* outputWire;
};


static gpointer multiplexer_component_state_parent_class = NULL;

gpointer component_state_ref (gpointer instance);
void component_state_unref (gpointer instance);
GParamSpec* param_spec_component_state (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void value_set_component_state (GValue* value, gpointer v_object);
void value_take_component_state (GValue* value, gpointer v_object);
gpointer value_get_component_state (const GValue* value);
GType component_state_get_type (void) G_GNUC_CONST;
gpointer compiled_circuit_ref (gpointer instance);
void compiled_circuit_unref (gpointer instance);
GParamSpec* param_spec_compiled_circuit (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void value_set_compiled_circuit (GValue* value, gpointer v_object);
void value_take_compiled_circuit (GValue* value, gpointer v_object);
gpointer value_get_compiled_circuit (const GValue* value);
GType compiled_circuit_get_type (void) G_GNUC_CONST;
gpointer component_inst_ref (gpointer instance);
void component_inst_unref (gpointer instance);
GParamSpec* param_spec_component_inst (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void value_set_component_inst (GValue* value, gpointer v_object);
void value_take_component_inst (GValue* value, gpointer v_object);
gpointer value_get_component_inst (const GValue* value);
GType component_inst_get_type (void) G_GNUC_CONST;
GType multiplexer_component_state_get_type (void) G_GNUC_CONST;
gpointer connection_ref (gpointer instance);
void connection_unref (gpointer instance);
GParamSpec* param_spec_connection (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void value_set_connection (GValue* value, gpointer v_object);
void value_take_connection (GValue* value, gpointer v_object);
gpointer value_get_connection (const GValue* value);
GType connection_get_type (void) G_GNUC_CONST;
#define MULTIPLEXER_COMPONENT_STATE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_MULTIPLEXER_COMPONENT_STATE, MultiplexerComponentStatePrivate))
enum  {
	MULTIPLEXER_COMPONENT_STATE_DUMMY_PROPERTY
};
MultiplexerComponentState* multiplexer_component_state_new (Connection** selectWires, int selectWires_length1, Connection** dataWires, int dataWires_length1, Connection* outputWire, ComponentInst** ancestry, int ancestry_length1, ComponentInst* componentInst);
MultiplexerComponentState* multiplexer_component_state_construct (GType object_type, Connection** selectWires, int selectWires_length1, Connection** dataWires, int dataWires_length1, Connection* outputWire, ComponentInst** ancestry, int ancestry_length1, ComponentInst* componentInst);
ComponentState* component_state_construct (GType object_type);
static Connection** _vala_array_dup29 (Connection** self, int length);
void connection_set_affects (Connection* self, ComponentState* componentState);
static Connection** _vala_array_dup30 (Connection** self, int length);
static ComponentInst** _vala_array_dup31 (ComponentInst** self, int length);
static void multiplexer_component_state_real_update (ComponentState* base);
gboolean connection_get_signalState (Connection* self);
void connection_set_signalState (Connection* self, gboolean value);
static void multiplexer_component_state_finalize (ComponentState* obj);
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);


static gpointer _connection_ref0 (gpointer self) {
	return self ? connection_ref (self) : NULL;
}


static Connection** _vala_array_dup29 (Connection** self, int length) {
	Connection** result;
	int i;
	result = g_new0 (Connection*, length + 1);
	for (i = 0; i < length; i++) {
		Connection* _tmp0_ = NULL;
		_tmp0_ = _connection_ref0 (self[i]);
		result[i] = _tmp0_;
	}
	return result;
}


static Connection** _vala_array_dup30 (Connection** self, int length) {
	Connection** result;
	int i;
	result = g_new0 (Connection*, length + 1);
	for (i = 0; i < length; i++) {
		Connection* _tmp0_ = NULL;
		_tmp0_ = _connection_ref0 (self[i]);
		result[i] = _tmp0_;
	}
	return result;
}


static gpointer _component_inst_ref0 (gpointer self) {
	return self ? component_inst_ref (self) : NULL;
}


static ComponentInst** _vala_array_dup31 (ComponentInst** self, int length) {
	ComponentInst** result;
	int i;
	result = g_new0 (ComponentInst*, length + 1);
	for (i = 0; i < length; i++) {
		ComponentInst* _tmp0_ = NULL;
		_tmp0_ = _component_inst_ref0 (self[i]);
		result[i] = _tmp0_;
	}
	return result;
}


MultiplexerComponentState* multiplexer_component_state_construct (GType object_type, Connection** selectWires, int selectWires_length1, Connection** dataWires, int dataWires_length1, Connection* outputWire, ComponentInst** ancestry, int ancestry_length1, ComponentInst* componentInst) {
	MultiplexerComponentState* self = NULL;
	Connection** _tmp0_ = NULL;
	gint _tmp0__length1 = 0;
	Connection** _tmp1_ = NULL;
	gint _tmp1__length1 = 0;
	Connection** _tmp2_ = NULL;
	gint _tmp2__length1 = 0;
	Connection** _tmp5_ = NULL;
	gint _tmp5__length1 = 0;
	Connection** _tmp6_ = NULL;
	gint _tmp6__length1 = 0;
	Connection** _tmp7_ = NULL;
	gint _tmp7__length1 = 0;
	Connection* _tmp10_ = NULL;
	Connection* _tmp11_ = NULL;
	ComponentInst** _tmp12_ = NULL;
	gint _tmp12__length1 = 0;
	ComponentInst** _tmp13_ = NULL;
	gint _tmp13__length1 = 0;
	ComponentInst* _tmp14_ = NULL;
	ComponentInst* _tmp15_ = NULL;
	g_return_val_if_fail (outputWire != NULL, NULL);
	g_return_val_if_fail (componentInst != NULL, NULL);
	self = (MultiplexerComponentState*) component_state_construct (object_type);
	_tmp0_ = selectWires;
	_tmp0__length1 = selectWires_length1;
	_tmp1_ = (_tmp0_ != NULL) ? _vala_array_dup29 (_tmp0_, _tmp0__length1) : ((gpointer) _tmp0_);
	_tmp1__length1 = _tmp0__length1;
	self->priv->selectWires = (_vala_array_free (self->priv->selectWires, self->priv->selectWires_length1, (GDestroyNotify) connection_unref), NULL);
	self->priv->selectWires = _tmp1_;
	self->priv->selectWires_length1 = _tmp1__length1;
	self->priv->_selectWires_size_ = self->priv->selectWires_length1;
	_tmp2_ = selectWires;
	_tmp2__length1 = selectWires_length1;
	{
		Connection** selectWire_collection = NULL;
		gint selectWire_collection_length1 = 0;
		gint _selectWire_collection_size_ = 0;
		gint selectWire_it = 0;
		selectWire_collection = _tmp2_;
		selectWire_collection_length1 = _tmp2__length1;
		for (selectWire_it = 0; selectWire_it < _tmp2__length1; selectWire_it = selectWire_it + 1) {
			Connection* _tmp3_ = NULL;
			Connection* selectWire = NULL;
			_tmp3_ = _connection_ref0 (selectWire_collection[selectWire_it]);
			selectWire = _tmp3_;
			{
				Connection* _tmp4_ = NULL;
				_tmp4_ = selectWire;
				connection_set_affects (_tmp4_, (ComponentState*) self);
				_connection_unref0 (selectWire);
			}
		}
	}
	_tmp5_ = dataWires;
	_tmp5__length1 = dataWires_length1;
	_tmp6_ = (_tmp5_ != NULL) ? _vala_array_dup30 (_tmp5_, _tmp5__length1) : ((gpointer) _tmp5_);
	_tmp6__length1 = _tmp5__length1;
	self->priv->dataWires = (_vala_array_free (self->priv->dataWires, self->priv->dataWires_length1, (GDestroyNotify) connection_unref), NULL);
	self->priv->dataWires = _tmp6_;
	self->priv->dataWires_length1 = _tmp6__length1;
	self->priv->_dataWires_size_ = self->priv->dataWires_length1;
	_tmp7_ = dataWires;
	_tmp7__length1 = dataWires_length1;
	{
		Connection** dataWire_collection = NULL;
		gint dataWire_collection_length1 = 0;
		gint _dataWire_collection_size_ = 0;
		gint dataWire_it = 0;
		dataWire_collection = _tmp7_;
		dataWire_collection_length1 = _tmp7__length1;
		for (dataWire_it = 0; dataWire_it < _tmp7__length1; dataWire_it = dataWire_it + 1) {
			Connection* _tmp8_ = NULL;
			Connection* dataWire = NULL;
			_tmp8_ = _connection_ref0 (dataWire_collection[dataWire_it]);
			dataWire = _tmp8_;
			{
				Connection* _tmp9_ = NULL;
				_tmp9_ = dataWire;
				connection_set_affects (_tmp9_, (ComponentState*) self);
				_connection_unref0 (dataWire);
			}
		}
	}
	_tmp10_ = outputWire;
	_tmp11_ = _connection_ref0 (_tmp10_);
	_connection_unref0 (self->priv->outputWire);
	self->priv->outputWire = _tmp11_;
	_tmp12_ = ancestry;
	_tmp12__length1 = ancestry_length1;
	_tmp13_ = (_tmp12_ != NULL) ? _vala_array_dup31 (_tmp12_, _tmp12__length1) : ((gpointer) _tmp12_);
	_tmp13__length1 = _tmp12__length1;
	((ComponentState*) self)->ancestry = (_vala_array_free (((ComponentState*) self)->ancestry, ((ComponentState*) self)->ancestry_length1, (GDestroyNotify) component_inst_unref), NULL);
	((ComponentState*) self)->ancestry = _tmp13_;
	((ComponentState*) self)->ancestry_length1 = _tmp13__length1;
	_tmp14_ = componentInst;
	_tmp15_ = _component_inst_ref0 (_tmp14_);
	_component_inst_unref0 (((ComponentState*) self)->componentInst);
	((ComponentState*) self)->componentInst = _tmp15_;
	return self;
}


MultiplexerComponentState* multiplexer_component_state_new (Connection** selectWires, int selectWires_length1, Connection** dataWires, int dataWires_length1, Connection* outputWire, ComponentInst** ancestry, int ancestry_length1, ComponentInst* componentInst) {
	return multiplexer_component_state_construct (TYPE_MULTIPLEXER_COMPONENT_STATE, selectWires, selectWires_length1, dataWires, dataWires_length1, outputWire, ancestry, ancestry_length1, componentInst);
}


static void multiplexer_component_state_real_update (ComponentState* base) {
	MultiplexerComponentState * self;
	gboolean output = FALSE;
	gint selected = 0;
	gint _tmp11_ = 0;
	Connection** _tmp12_ = NULL;
	gint _tmp12__length1 = 0;
	Connection* _tmp18_ = NULL;
	gboolean _tmp19_ = FALSE;
	self = (MultiplexerComponentState*) base;
	output = FALSE;
	selected = 0;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp0_ = FALSE;
			_tmp0_ = TRUE;
			while (TRUE) {
				gint _tmp2_ = 0;
				Connection** _tmp3_ = NULL;
				gint _tmp3__length1 = 0;
				gint _tmp4_ = 0;
				Connection** _tmp5_ = NULL;
				gint _tmp5__length1 = 0;
				gint _tmp6_ = 0;
				Connection* _tmp7_ = NULL;
				gboolean _tmp8_ = FALSE;
				gboolean _tmp9_ = FALSE;
				if (!_tmp0_) {
					gint _tmp1_ = 0;
					_tmp1_ = i;
					i = _tmp1_ + 1;
				}
				_tmp0_ = FALSE;
				_tmp2_ = i;
				_tmp3_ = self->priv->selectWires;
				_tmp3__length1 = self->priv->selectWires_length1;
				if (!(_tmp2_ < _tmp3__length1)) {
					break;
				}
				_tmp4_ = selected;
				selected = _tmp4_ << 1;
				_tmp5_ = self->priv->selectWires;
				_tmp5__length1 = self->priv->selectWires_length1;
				_tmp6_ = i;
				_tmp7_ = _tmp5_[_tmp6_];
				_tmp8_ = connection_get_signalState (_tmp7_);
				_tmp9_ = _tmp8_;
				if (_tmp9_) {
					gint _tmp10_ = 0;
					_tmp10_ = selected;
					selected = _tmp10_ | 1;
				}
			}
		}
	}
	_tmp11_ = selected;
	_tmp12_ = self->priv->dataWires;
	_tmp12__length1 = self->priv->dataWires_length1;
	if (_tmp11_ < _tmp12__length1) {
		Connection** _tmp13_ = NULL;
		gint _tmp13__length1 = 0;
		gint _tmp14_ = 0;
		Connection* _tmp15_ = NULL;
		gboolean _tmp16_ = FALSE;
		gboolean _tmp17_ = FALSE;
		_tmp13_ = self->priv->dataWires;
		_tmp13__length1 = self->priv->dataWires_length1;
		_tmp14_ = selected;
		_tmp15_ = _tmp13_[_tmp14_];
		_tmp16_ = connection_get_signalState (_tmp15_);
		_tmp17_ = _tmp16_;
		output = _tmp17_;
	}
	_tmp18_ = self->priv->outputWire;
	_tmp19_ = output;
	connection_set_signalState (_tmp18_, _tmp19_);
}


static void multiplexer_component_state_class_init (MultiplexerComponentStateClass * klass) {
	multiplexer_component_state_parent_class = g_type_class_peek_parent (klass);
	COMPONENT_STATE_CLASS (klass)->finalize = multiplexer_component_state_finalize;
	g_type_class_add_private (klass, sizeof (MultiplexerComponentStatePrivate));
	COMPONENT_STATE_CLASS (klass)->update = multiplexer_component_state_real_update;
}


static void multiplexer_component_state_instance_init (MultiplexerComponentState * self) {
	self->priv = MULTIPLEXER_COMPONENT_STATE_GET_PRIVATE (self);
}


static void multiplexer_component_state_finalize (ComponentState* obj) {
	MultiplexerComponentState * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_MULTIPLEXER_COMPONENT_STATE, MultiplexerComponentState);
	self->priv->selectWires = (_vala_array_free (self->priv->selectWires, self->priv->selectWires_length1, (GDestroyNotify) connection_unref), NULL);
	self->priv->dataWires = (_vala_array_free (self->priv->dataWires, self->priv->dataWires_length1, (GDestroyNotify) connection_unref), NULL);
	_connection_unref0 (self->priv->outputWire);
	COMPONENT_STATE_CLASS (multiplexer_component_state_parent_class)->finalize (obj);
}


GType multiplexer_component_state_get_type (void) {
	static volatile gsize multiplexer_component_state_type_id__volatile = 0;
	if (g_once_init_enter (&multiplexer_component_state_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (MultiplexerComponentStateClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) multiplexer_component_state_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (MultiplexerComponentState), 0, (GInstanceInitFunc) multiplexer_component_state_instance_init, NULL };
		GType multiplexer_component_state_type_id;
		multiplexer_component_state_type_id = g_type_register_static (TYPE_COMPONENT_STATE, "MultiplexerComponentState", &g_define_type_info, 0);
		g_once_init_leave (&multiplexer_component_state_type_id__volatile, multiplexer_component_state_type_id);
	}
	return multiplexer_component_state_type_id__volatile;
}


static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	if ((array != NULL) && (destroy_func != NULL)) {
		int i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}


static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}



