Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Vala and Objective C

Vala and Objective C

Avatar for Hunter Madison

Hunter Madison

June 01, 2016
Tweet

More Decks by Hunter Madison

Other Decks in Programming

Transcript

  1. Vala • Vala generates C code and uses the GObject

    system. • Simple interface with native apis (vapi) • Used by ElementaryOS, Gnome • Wraps the semantics of its object system in syntactic sugars
  2. /* test3.c generated by valac 0.26.1, the Vala compiler *

    generated from test3.vala, do not modify */ #include <glib.h> #include <glib-object.h> void _vala_main (void); void _vala_main (void) { g_print ("Hello World\n"); } int main (int argc, char ** argv) { #if !GLIB_CHECK_VERSION (2,35,0) g_type_init (); #endif _vala_main (); return 0; }
  3. class Vector : Object { public int x { get;

    set; default = 0; } public int y { get; set; default = 0; } public int z { get; set; default = 0; } public Vector(int x, int y, int z) { _x = x; _y = y; _z = z; } public string to_string() { return "(%i, %i, %i)".printf(_x,_y,_x); } } int main() { var v1 = new Vector(1,2,3); print(v1.to_string()); return 0; }
  4. gint _tmp2_ = 0; self = (Vector*) g_object_new (object_type, NULL);

    _tmp0_ = x; self->priv->_x = _tmp0_; _tmp1_ = y; self->priv->_y = _tmp1_; _tmp2_ = z; self->priv->_z = _tmp2_; return self; } Vector* vector_new (gint x, gint y, gint z) { return vector_construct (TYPE_VECTOR, x, y, z); } gchar* vector_to_string (Vector* self) { gchar* result = NULL; gint _tmp0_ = 0; gint _tmp1_ = 0; gint _tmp2_ = 0; gchar* _tmp3_ = NULL; g_return_val_if_fail (self != NULL, NULL); _tmp0_ = self->priv->_x; _tmp1_ = self->priv->_y; _tmp2_ = self->priv->_x; _tmp3_ = g_strdup_printf ("(%i, %i, %i)", _tmp0_, _tmp1_, _tmp2_); result = _tmp3_; return result; } gint vector_get_x (Vector* self) { gint result; gint _tmp0_ = 0; g_return_val_if_fail (self != NULL, 0); _tmp0_ = self->priv->_x; result = _tmp0_; return result; } void vector_set_x (Vector* self, gint value) { gint _tmp0_ = 0; g_return_if_fail (self != NULL); _tmp0_ = value; self->priv->_x = _tmp0_; g_object_notify ((GObject *) self, "x"); } gint vector_get_y (Vector* self) { gint result; gint _tmp0_ = 0; g_return_val_if_fail (self != NULL, 0); _tmp0_ = self->priv->_y; result = _tmp0_; return result; } void vector_set_y (Vector* self, gint value) { gint _tmp0_ = 0; g_return_if_fail (self != NULL); _tmp0_ = value; self->priv->_y = _tmp0_; g_object_notify ((GObject *) self, "y"); } gint vector_get_z (Vector* self) { gint result; gint _tmp0_ = 0; g_return_val_if_fail (self != NULL, 0); _tmp0_ = self->priv->_z; result = _tmp0_; return result; } void vector_set_z (Vector* self, gint value) { gint _tmp0_ = 0; g_return_if_fail (self != NULL); _tmp0_ = value; self->priv->_z = _tmp0_; g_object_notify ((GObject *) self, "z"); } static void vector_class_init (VectorClass * klass) { vector_parent_class = g_type_class_peek_parent (klass); g_type_class_add_private (klass, sizeof (VectorPrivate)); G_OBJECT_CLASS (klass)->get_property = _vala_vector_get_property; G_OBJECT_CLASS (klass)->set_property = _vala_vector_set_property; G_OBJECT_CLASS (klass)->finalize = vector_finalize; g_object_class_install_property (G_OBJECT_CLASS (klass), VECTOR_X, g_param_spec_int ("x", "x", "x", G_MININT, G_MAXINT, 0, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE)); g_object_class_install_property (G_OBJECT_CLASS (klass), VECTOR_Y, g_param_spec_int ("y", "y", "y", G_MININT, G_MAXINT, 0, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE)); g_object_class_install_property (G_OBJECT_CLASS (klass), VECTOR_Z, g_param_spec_int ("z", "z", "z", G_MININT, G_MAXINT, 0, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE)); } static void vector_instance_init (Vector * self) { self->priv = VECTOR_GET_PRIVATE (self); self->priv->_x = 0; self->priv->_y = 0; self->priv->_z = 0; } static void vector_finalize (GObject* obj) { Vector * self; self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_VECTOR, Vector); G_OBJECT_CLASS (vector_parent_class)->finalize (obj); } GType vector_get_type (void) { static volatile gsize vector_type_id__volatile = 0; if (g_once_init_enter (&vector_type_id__volatile)) { static const GTypeInfo g_define_type_info = { sizeof (VectorClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) vector_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (Vector), 0, (GInstanceInitFunc) vector_instance_init, NULL }; GType vector_type_id; vector_type_id = g_type_register_static (G_TYPE_OBJECT, "Vector", &g_define_type_info, 0); g_once_init_leave (&vector_type_id__volatile, vector_type_id); } return vector_type_id__volatile; }
  5. Objective C • A strange amalgamation of C and Smalltalk

    • Born out of Next, used by Apple everywhere • Superset of C, any valid C is valid Objective C
  6. The “hack” • Vala and Objective C both obey the

    C calling conventions • This means we can use ld to munge them together • Why?
  7. UI!

  8. UI • OSX requires* you to use Objective C to

    access most of its ui functionality • This even includes getting a frame buffer. • Would be nice to use other UI toolkits.
  9. Apple’s ABI Stack • CoreFoundation (C) • Foundation / AppKit

    / Etc (Objective C) • Carbon (C - Deprecated)
  10. Under the Hood • NSAutoReleasePool • NSApplication • NsApp setActivationPolicy

    • NSWindow • Our stuff here • activateIgnoringOtherApps • run
  11. ???