Convert GisObject to GObject
authorAndy Spencer <andy753421@gmail.com>
Wed, 3 Feb 2010 08:24:51 +0000 (08:24 +0000)
committerAndy Spencer <andy753421@gmail.com>
Wed, 3 Feb 2010 08:24:51 +0000 (08:24 +0000)
- Sigh... oh well, at least we have evil macros to make ourselves feel
  better about this.

src/gis-object.c
src/gis-object.h
src/gis-opengl.c

index c70adc4..4eca077 100644 (file)
@@ -40,15 +40,29 @@ void gis_point_free(GisPoint *self)
 }
 
 
+/* GisObject */
+G_DEFINE_TYPE(GisObject, gis_object, G_TYPE_OBJECT);
+static void gis_object_init(GisObject *self) { }
+static void gis_object_class_init(GisObjectClass *klass) { }
+
+
 /* GisMarker */
+G_DEFINE_TYPE(GisMarker, gis_marker, GIS_TYPE_OBJECT);
+static void gis_marker_init(GisMarker *self) { }
+
+static void gis_marker_finalize(GObject *_self);
+static void gis_marker_class_init(GisMarkerClass *klass)
+{
+       G_OBJECT_CLASS(klass)->finalize = gis_marker_finalize;
+}
+
 GisMarker *gis_marker_new(const gchar *label)
 {
        static const int RADIUS =   4;
        static const int WIDTH  = 100;
        static const int HEIGHT =  20;
 
-       GisMarker *self = g_new0(GisMarker, 1);
-       GIS_OBJECT(self)->type = GIS_TYPE_MARKER;
+       GisMarker *self = g_object_new(GIS_TYPE_MARKER, NULL);
        self->xoff  = RADIUS;
        self->yoff  = HEIGHT-RADIUS;
        self->label = g_strdup(label);
@@ -63,8 +77,9 @@ GisMarker *gis_marker_new(const gchar *label)
        return self;
 }
 
-void gis_marker_free(GisMarker *self)
+static void gis_marker_finalize(GObject *_self)
 {
+       GisMarker *self = GIS_MARKER(_self);
        cairo_surface_destroy(cairo_get_target(self->cairo));
        cairo_destroy(self->cairo);
        g_free(self->label);
@@ -73,16 +88,14 @@ void gis_marker_free(GisMarker *self)
 
 
 /* GisCallback */
+G_DEFINE_TYPE(GisCallback, gis_callback, GIS_TYPE_OBJECT);
+static void gis_callback_init(GisCallback *self) { }
+static void gis_callback_class_init(GisCallbackClass *klass) { }
+
 GisCallback *gis_callback_new(GisCallbackFunc callback, gpointer user_data)
 {
-       GisCallback *self = g_new0(GisCallback, 1);
-       GIS_OBJECT(self)->type = GIS_TYPE_CALLBACK;
+       GisCallback *self = g_object_new(GIS_TYPE_CALLBACK, NULL);
        self->callback  = callback;
        self->user_data = user_data;
        return self;
 }
-
-void gis_callback_free(GisCallback *self)
-{
-       g_free(self);
-}
index 147c147..acc4255 100644 (file)
 #define __GIS_OBJECT_H__
 
 #include <glib.h>
+#include <glib-object.h>
 #include <cairo.h>
 
+
+/* Take that GLib boilerplate! */
+#define GOBJECT_HEAD( \
+               MAM, BAR, \
+               Mam, Bar, \
+               mam, bar) \
+GType mam##_##bar##_get_type(void); \
+typedef struct _##Mam##Bar Mam##Bar; \
+typedef struct _##Mam##Bar##Class Mam##Bar##Class; \
+static inline Mam##Bar *MAM##_##BAR(gpointer obj) { \
+       return G_TYPE_CHECK_INSTANCE_CAST(obj, MAM##_TYPE_##BAR, Mam##Bar); \
+} \
+static inline gboolean MAM##_IS_##BAR(gpointer obj) { \
+       return G_TYPE_CHECK_INSTANCE_TYPE(obj, MAM##_TYPE_##BAR); \
+} \
+static inline Mam##Bar##Class *MAM##_##BAR##_CLASS(gpointer klass) { \
+       return G_TYPE_CHECK_CLASS_CAST(klass, MAM##_TYPE_##BAR, Mam##Bar##Class); \
+} \
+static inline gboolean MAM##_IS_##BAR##_CLASS(gpointer klass) { \
+       return G_TYPE_CHECK_CLASS_TYPE(klass, MAM##_TYPE_##BAR); \
+} \
+static inline Mam##Bar##Class *MAM##_##BAR##_GET_CLASS(gpointer obj) { \
+       return G_TYPE_INSTANCE_GET_CLASS(obj, MAM##_TYPE_##BAR, Mam##Bar##Class); \
+}
+
+#define GOBJECT_BODY( \
+               parent_type, \
+               MAM, BAR, \
+               Mam, Bar, \
+               mam, bar) \
+G_DEFINE_TYPE(Mam##Bar, mam##_##bar, parent_type); \
+static void mam##_##bar##_init(Mam##Bar *self) { \
+} \
+static void mam##_##bar##_class_init(Mam##Bar##Class *klass) { \
+} \
+static Mam##Bar *mam##_##bar##_new() { \
+       return g_object_new(MAM##_TYPE_##BAR, NULL); \
+}
+
+
 /* GisPoint */
-typedef struct _GisPoint      GisPoint;
+typedef struct _GisPoint GisPoint;
 
 struct _GisPoint {
        gdouble lat, lon, elev;
@@ -34,20 +75,21 @@ void gis_point_free(GisPoint *point);
 
 
 /* GisObject */
-#define GIS_OBJECT(object)     ((GisObject  *)object)
+#define GIS_TYPE_OBJECT (gis_object_get_type())
 
-typedef enum {
-       GIS_TYPE_CALLBACK,
-       GIS_TYPE_MARKER,
-       GIS_NUM_TYPES,
-} GisObjectType;
-
-typedef struct _GisObject GisObject;
+GOBJECT_HEAD(
+       GIS, OBJECT,
+       Gis, Object,
+       gis, object);
 
 struct _GisObject {
-       GisObjectType type;
-       GisPoint      center;
-       gdouble       lod;
+       GObject parent_instance;
+       GisPoint center;
+       gdouble  lod;
+};
+
+struct _GisObjectClass {
+       GObjectClass parent_class;
 };
 
 static inline GisPoint *gis_object_center(GisObject *object)
@@ -57,26 +99,36 @@ static inline GisPoint *gis_object_center(GisObject *object)
 
 
 /* GisMarker */
-#define GIS_MARKER(marker) ((GisMarker  *)marker)
+#define GIS_TYPE_MARKER (gis_marker_get_type())
 
-typedef struct _GisMarker GisMarker;
+GOBJECT_HEAD(
+       GIS, MARKER,
+       Gis, Marker,
+       gis, marker);
 
-struct _GisMarker   {
-       GisObject  parent;
+struct _GisMarker {
+       GisObject  parent_instance;
        gint       xoff, yoff;
        gchar     *label;
        cairo_t   *cairo;
        guint      tex;
 };
 
+struct _GisMarkerClass {
+       GisObjectClass parent_class;
+};
+
 GisMarker *gis_marker_new(const gchar *label);
-void gis_marker_free(GisMarker *marker);
 
 
 /* GisCallback */
-#define GIS_CALLBACK(callback) ((GisCallback*)callback)
+#define GIS_TYPE_CALLBACK (gis_callback_get_type())
+
+GOBJECT_HEAD(
+       GIS, CALLBACK,
+       Gis, Callback,
+       gis, callback);
 
-typedef struct _GisCallback GisCallback;
 typedef gpointer (*GisCallbackFunc)(GisCallback *callback, gpointer user_data);
 
 struct _GisCallback {
@@ -85,8 +137,10 @@ struct _GisCallback {
        gpointer        user_data;
 };
 
-GisCallback *gis_callback_new(GisCallbackFunc callback, gpointer user_data);
-void gis_callback_free(GisCallback *cb);
+struct _GisCallbackClass {
+       GisObjectClass parent_class;
+};
 
+GisCallback *gis_callback_new(GisCallbackFunc callback, gpointer user_data);
 
 #endif
index 30671aa..7263ef4 100644 (file)
@@ -188,15 +188,10 @@ static void _draw_object(GisOpenGL *self, GisObject *object)
        glMatrixMode(GL_PROJECTION); glPushMatrix();
        glMatrixMode(GL_MODELVIEW);  glPushMatrix();
        glPushAttrib(GL_ALL_ATTRIB_BITS);
-       switch (object->type) {
-       case GIS_TYPE_MARKER:
+       if (GIS_IS_MARKER(object)) {
                _draw_marker(self, GIS_MARKER(object));
-               break;
-       case GIS_TYPE_CALLBACK:
+       } else if (GIS_IS_CALLBACK(object)) {
                _draw_callback(self, GIS_CALLBACK(object));
-               break;
-       default:
-               break;
        }
        glPopAttrib();
        glMatrixMode(GL_PROJECTION); glPopMatrix();
@@ -206,8 +201,7 @@ static void _draw_object(GisOpenGL *self, GisObject *object)
 static void _load_object(GisOpenGL *self, GisObject *object)
 {
        g_debug("GisOpenGL: load_object");
-       switch (object->type) {
-       case GIS_TYPE_MARKER: {
+       if (GIS_IS_MARKER(object)) {
                GisMarker *marker = GIS_MARKER(object);
                cairo_surface_t *surface = cairo_get_target(marker->cairo);
                gdouble width  = cairo_image_surface_get_width(surface);
@@ -226,25 +220,16 @@ static void _load_object(GisOpenGL *self, GisObject *object)
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
                g_debug("load_texture: %d", marker->tex);
                _gis_opengl_end(self);
-               break;
-       }
-       default:
-               break;
        }
 }
 
 static void _unload_object(GisOpenGL *self, GisObject *object)
 {
        g_debug("GisOpenGL: unload_object");
-       switch (object->type) {
-       case GIS_TYPE_MARKER: {
+       if (GIS_IS_MARKER(object)) {
                GisMarker *marker = GIS_MARKER(object);
                g_debug("delete_texture: %d", marker->tex);
                glDeleteTextures(1, &marker->tex);
-               break;
-       }
-       default:
-               break;
        }
 }