Created
March 5, 2012 06:13
-
-
Save inequation/1976985 to your computer and use it in GitHub Desktop.
Sample Vala intermediate C code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sample Vala intermediate C code, as compiled from | |
// https://github.com/inequation/PerceptVala/blob/master/src/Neuron.vala | |
/* Neuron.c generated by valac 0.12.1, the Vala compiler | |
* generated from Neuron.vala, do not modify */ | |
#include <glib.h> | |
#include <glib-object.h> | |
#include <float.h> | |
#include <math.h> | |
#include <gobject/gvaluecollector.h> | |
#define TYPE_NEURON (neuron_get_type ()) | |
#define NEURON(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_NEURON, Neuron)) | |
#define NEURON_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_NEURON, NeuronClass)) | |
#define IS_NEURON(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_NEURON)) | |
#define IS_NEURON_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_NEURON)) | |
#define NEURON_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_NEURON, NeuronClass)) | |
typedef struct _Neuron Neuron; | |
typedef struct _NeuronClass NeuronClass; | |
typedef struct _NeuronPrivate NeuronPrivate; | |
#define NEURON_TYPE_SYNAPSE (neuron_synapse_get_type ()) | |
typedef struct _NeuronSynapse NeuronSynapse; | |
#define _neuron_unref0(var) ((var == NULL) ? NULL : (var = (neuron_unref (var), NULL))) | |
typedef struct _ParamSpecNeuron ParamSpecNeuron; | |
struct _Neuron { | |
GTypeInstance parent_instance; | |
volatile int ref_count; | |
NeuronPrivate * priv; | |
}; | |
struct _NeuronClass { | |
GTypeClass parent_class; | |
void (*finalize) (Neuron *self); | |
gfloat (*get_signal) (Neuron* self); | |
}; | |
struct _NeuronSynapse { | |
Neuron* neuron; | |
gfloat weight; | |
}; | |
struct _NeuronPrivate { | |
NeuronSynapse* m_synapses; | |
gint m_synapses_length1; | |
gint _m_synapses_size_; | |
}; | |
struct _ParamSpecNeuron { | |
GParamSpec parent_instance; | |
}; | |
static gpointer neuron_parent_class = NULL; | |
gpointer neuron_ref (gpointer instance); | |
void neuron_unref (gpointer instance); | |
GParamSpec* param_spec_neuron (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags); | |
void value_set_neuron (GValue* value, gpointer v_object); | |
void value_take_neuron (GValue* value, gpointer v_object); | |
gpointer value_get_neuron (const GValue* value); | |
GType neuron_get_type (void) G_GNUC_CONST; | |
GType neuron_synapse_get_type (void) G_GNUC_CONST; | |
NeuronSynapse* neuron_synapse_dup (const NeuronSynapse* self); | |
void neuron_synapse_free (NeuronSynapse* self); | |
void neuron_synapse_copy (const NeuronSynapse* self, NeuronSynapse* dest); | |
void neuron_synapse_destroy (NeuronSynapse* self); | |
#define NEURON_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_NEURON, NeuronPrivate)) | |
enum { | |
NEURON_DUMMY_PROPERTY | |
}; | |
static void _vala_NeuronSynapse_array_free (NeuronSynapse* array, gint array_length); | |
void neuron_add_synapse (Neuron* self, NeuronSynapse* s); | |
static void _vala_array_add1 (NeuronSynapse** array, int* length, int* size, const NeuronSynapse* value); | |
gfloat neuron_get_signal (Neuron* self); | |
static gfloat neuron_real_get_signal (Neuron* self); | |
Neuron* neuron_new (void); | |
Neuron* neuron_construct (GType object_type); | |
static void neuron_finalize (Neuron* obj); | |
static void _vala_NeuronSynapse_array_free (NeuronSynapse* array, gint array_length) { | |
if (array != NULL) { | |
int i; | |
for (i = 0; i < array_length; i = i + 1) { | |
neuron_synapse_destroy (&array[i]); | |
} | |
} | |
g_free (array); | |
} | |
static void _vala_array_add1 (NeuronSynapse** array, int* length, int* size, const NeuronSynapse* value) { | |
if ((*length) == (*size)) { | |
*size = (*size) ? (2 * (*size)) : 4; | |
*array = g_renew (NeuronSynapse, *array, *size); | |
} | |
(*array)[(*length)++] = *value; | |
} | |
void neuron_add_synapse (Neuron* self, NeuronSynapse* s) { | |
NeuronSynapse _tmp0_; | |
NeuronSynapse _tmp1_ = {0}; | |
g_return_if_fail (self != NULL); | |
neuron_synapse_copy (s, &_tmp1_); | |
_tmp0_ = _tmp1_; | |
_vala_array_add1 (&self->priv->m_synapses, &self->priv->m_synapses_length1, &self->priv->_m_synapses_size_, &_tmp0_); | |
} | |
static gfloat neuron_real_get_signal (Neuron* self) { | |
gfloat result = 0.0F; | |
gfloat activation; | |
gfloat _tmp3_ = 0.0F; | |
g_return_val_if_fail (self != NULL, 0.0F); | |
activation = 0.0f; | |
{ | |
NeuronSynapse* s_collection; | |
int s_collection_length1; | |
int s_it; | |
s_collection = self->priv->m_synapses; | |
s_collection_length1 = self->priv->m_synapses_length1; | |
for (s_it = 0; s_it < self->priv->m_synapses_length1; s_it = s_it + 1) { | |
NeuronSynapse _tmp0_; | |
NeuronSynapse _tmp1_ = {0}; | |
NeuronSynapse s; | |
neuron_synapse_copy (&s_collection[s_it], &_tmp1_); | |
_tmp0_ = _tmp1_; | |
s = _tmp0_; | |
{ | |
gfloat _tmp2_; | |
_tmp2_ = neuron_get_signal (s.neuron); | |
activation = activation + (_tmp2_ * s.weight); | |
neuron_synapse_destroy (&s); | |
} | |
} | |
} | |
if (activation >= 1.0f) { | |
_tmp3_ = 1.0f; | |
} else { | |
_tmp3_ = 0.0f; | |
} | |
result = _tmp3_; | |
return result; | |
} | |
gfloat neuron_get_signal (Neuron* self) { | |
return NEURON_GET_CLASS (self)->get_signal (self); | |
} | |
Neuron* neuron_construct (GType object_type) { | |
Neuron* self = NULL; | |
self = (Neuron*) g_type_create_instance (object_type); | |
return self; | |
} | |
Neuron* neuron_new (void) { | |
return neuron_construct (TYPE_NEURON); | |
} | |
static gpointer _neuron_ref0 (gpointer self) { | |
return self ? neuron_ref (self) : NULL; | |
} | |
void neuron_synapse_copy (const NeuronSynapse* self, NeuronSynapse* dest) { | |
dest->neuron = _neuron_ref0 (self->neuron); | |
dest->weight = self->weight; | |
} | |
void neuron_synapse_destroy (NeuronSynapse* self) { | |
_neuron_unref0 ((*self).neuron); | |
} | |
NeuronSynapse* neuron_synapse_dup (const NeuronSynapse* self) { | |
NeuronSynapse* dup; | |
dup = g_new0 (NeuronSynapse, 1); | |
neuron_synapse_copy (self, dup); | |
return dup; | |
} | |
void neuron_synapse_free (NeuronSynapse* self) { | |
neuron_synapse_destroy (self); | |
g_free (self); | |
} | |
GType neuron_synapse_get_type (void) { | |
static volatile gsize neuron_synapse_type_id__volatile = 0; | |
if (g_once_init_enter (&neuron_synapse_type_id__volatile)) { | |
GType neuron_synapse_type_id; | |
neuron_synapse_type_id = g_boxed_type_register_static ("NeuronSynapse", (GBoxedCopyFunc) neuron_synapse_dup, (GBoxedFreeFunc) neuron_synapse_free); | |
g_once_init_leave (&neuron_synapse_type_id__volatile, neuron_synapse_type_id); | |
} | |
return neuron_synapse_type_id__volatile; | |
} | |
static void value_neuron_init (GValue* value) { | |
value->data[0].v_pointer = NULL; | |
} | |
static void value_neuron_free_value (GValue* value) { | |
if (value->data[0].v_pointer) { | |
neuron_unref (value->data[0].v_pointer); | |
} | |
} | |
static void value_neuron_copy_value (const GValue* src_value, GValue* dest_value) { | |
if (src_value->data[0].v_pointer) { | |
dest_value->data[0].v_pointer = neuron_ref (src_value->data[0].v_pointer); | |
} else { | |
dest_value->data[0].v_pointer = NULL; | |
} | |
} | |
static gpointer value_neuron_peek_pointer (const GValue* value) { | |
return value->data[0].v_pointer; | |
} | |
static gchar* value_neuron_collect_value (GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) { | |
if (collect_values[0].v_pointer) { | |
Neuron* 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 = neuron_ref (object); | |
} else { | |
value->data[0].v_pointer = NULL; | |
} | |
return NULL; | |
} | |
static gchar* value_neuron_lcopy_value (const GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) { | |
Neuron** 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 = neuron_ref (value->data[0].v_pointer); | |
} | |
return NULL; | |
} | |
GParamSpec* param_spec_neuron (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags) { | |
ParamSpecNeuron* spec; | |
g_return_val_if_fail (g_type_is_a (object_type, TYPE_NEURON), 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 value_get_neuron (const GValue* value) { | |
g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_NEURON), NULL); | |
return value->data[0].v_pointer; | |
} | |
void value_set_neuron (GValue* value, gpointer v_object) { | |
Neuron* old; | |
g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_NEURON)); | |
old = value->data[0].v_pointer; | |
if (v_object) { | |
g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_NEURON)); | |
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; | |
neuron_ref (value->data[0].v_pointer); | |
} else { | |
value->data[0].v_pointer = NULL; | |
} | |
if (old) { | |
neuron_unref (old); | |
} | |
} | |
void value_take_neuron (GValue* value, gpointer v_object) { | |
Neuron* old; | |
g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_NEURON)); | |
old = value->data[0].v_pointer; | |
if (v_object) { | |
g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_NEURON)); | |
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) { | |
neuron_unref (old); | |
} | |
} | |
static void neuron_class_init (NeuronClass * klass) { | |
neuron_parent_class = g_type_class_peek_parent (klass); | |
NEURON_CLASS (klass)->finalize = neuron_finalize; | |
g_type_class_add_private (klass, sizeof (NeuronPrivate)); | |
NEURON_CLASS (klass)->get_signal = neuron_real_get_signal; | |
} | |
static void neuron_instance_init (Neuron * self) { | |
self->priv = NEURON_GET_PRIVATE (self); | |
self->ref_count = 1; | |
} | |
static void neuron_finalize (Neuron* obj) { | |
Neuron * self; | |
self = NEURON (obj); | |
self->priv->m_synapses = (_vala_NeuronSynapse_array_free (self->priv->m_synapses, self->priv->m_synapses_length1), NULL); | |
} | |
/** | |
PerceptVala neuron class | |
Written by Leszek Godlewski <github@inequation.org> | |
@author Leszek Godlewski | |
*/ | |
GType neuron_get_type (void) { | |
static volatile gsize neuron_type_id__volatile = 0; | |
if (g_once_init_enter (&neuron_type_id__volatile)) { | |
static const GTypeValueTable g_define_type_value_table = { value_neuron_init, value_neuron_free_value, value_neuron_copy_value, value_neuron_peek_pointer, "p", value_neuron_collect_value, "p", value_neuron_lcopy_value }; | |
static const GTypeInfo g_define_type_info = { sizeof (NeuronClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) neuron_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (Neuron), 0, (GInstanceInitFunc) neuron_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 neuron_type_id; | |
neuron_type_id = g_type_register_fundamental (g_type_fundamental_next (), "Neuron", &g_define_type_info, &g_define_type_fundamental_info, 0); | |
g_once_init_leave (&neuron_type_id__volatile, neuron_type_id); | |
} | |
return neuron_type_id__volatile; | |
} | |
gpointer neuron_ref (gpointer instance) { | |
Neuron* self; | |
self = instance; | |
g_atomic_int_inc (&self->ref_count); | |
return instance; | |
} | |
void neuron_unref (gpointer instance) { | |
Neuron* self; | |
self = instance; | |
if (g_atomic_int_dec_and_test (&self->ref_count)) { | |
NEURON_GET_CLASS (self)->finalize (self); | |
g_type_free_instance ((GTypeInstance *) self); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment