From 580dae2183e27ccec00f127b85188df143f49c54 Mon Sep 17 00:00:00 2001
From: Matthias Vogelgesang <matthias.vogelgesang@gmail.com>
Date: Tue, 12 Jun 2012 10:21:55 +0200
Subject: Generate enum types from source

It became a little unwieldy to create the enum types manually via
g_enums_register_static(). This changeset creates the types from enum
definitions in public headers using glib2-mkenum.

Be sure to include uca-enums.h in every source file that needs to know GObject
enum type.
---
 src/CMakeLists.txt           | 28 +++++++++++++++++++--
 src/cameras/uca-pco-camera.c | 59 +++++++++++---------------------------------
 src/cameras/uca-pco-camera.h |  4 ---
 src/uca-camera.c             | 34 +++++++------------------
 src/uca-camera.h             |  2 --
 src/uca-enums.c.template     | 44 +++++++++++++++++++++++++++++++++
 src/uca-enums.h.template     | 25 +++++++++++++++++++
 7 files changed, 118 insertions(+), 78 deletions(-)
 create mode 100644 src/uca-enums.c.template
 create mode 100644 src/uca-enums.h.template

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 73d1034..e9d5ebc 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -24,7 +24,7 @@ find_package(ClSerMe4)
 
 # --- Miscellanous packages
 find_package(PkgConfig)
-
+find_program(GLIB2_MKENUMS glib-mkenums REQUIRED)
 pkg_check_modules(GLIB2 glib-2.0>=2.24 REQUIRED)
 pkg_check_modules(GOBJECT2 gobject-2.0>=2.24 REQUIRED)
 
@@ -96,6 +96,28 @@ if (HAVE_MOCK_CAMERA)
     list(APPEND cameras "Mock")
 endif()
 
+# --- Generate enum file
+add_custom_command(
+    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/uca-enums.h
+    COMMAND ${GLIB2_MKENUMS}
+    ARGS
+        --template uca-enums.h.template
+        ${uca_HDRS} > ${CMAKE_CURRENT_BINARY_DIR}/uca-enums.h
+    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+    DEPENDS ${uca_HDRS} 
+            ${CMAKE_CURRENT_SOURCE_DIR}/uca-enums.h.template)
+
+add_custom_command(
+    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/uca-enums.c
+    COMMAND ${GLIB2_MKENUMS}
+    ARGS
+        --template uca-enums.c.template
+        ${uca_HDRS} > ${CMAKE_CURRENT_BINARY_DIR}/uca-enums.c
+    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+    DEPENDS ${uca_HDRS} 
+            ${CMAKE_CURRENT_BINARY_DIR}/uca-enums.h
+            ${CMAKE_CURRENT_SOURCE_DIR}/uca-enums.c.template
+    )
 
 # --- Configure step
 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in
@@ -111,7 +133,9 @@ include_directories(
 # --- Build target ------------------------------------------------------------
 add_definitions("-std=c99 -Wall")
 
-add_library(uca SHARED ${uca_SRCS})
+add_library(uca SHARED 
+    ${uca_SRCS} 
+    ${CMAKE_CURRENT_BINARY_DIR}/uca-enums.c)
 
 set_target_properties(uca PROPERTIES
       VERSION ${UCA_ABI_VERSION}
diff --git a/src/cameras/uca-pco-camera.c b/src/cameras/uca-pco-camera.c
index bba69c2..d12083f 100644
--- a/src/cameras/uca-pco-camera.c
+++ b/src/cameras/uca-pco-camera.c
@@ -24,6 +24,7 @@
 #include <fgrab_prototyp.h>
 #include "uca-camera.h"
 #include "uca-pco-camera.h"
+#include "uca-enums.h"
 
 #define FG_TRY_PARAM(fg, camobj, param, val_addr, port)     \
     { int r = Fg_setParameter(fg, param, val_addr, port);   \
@@ -57,6 +58,19 @@ G_DEFINE_TYPE(UcaPcoCamera, uca_pco_camera, UCA_TYPE_CAMERA)
 
 #define TIMEBASE_INVALID 0xDEAD
 
+/**
+ * UcaPcoCameraRecordMode:
+ * @UCA_PCO_CAMERA_RECORD_MODE_SEQUENCE: Store all frames and stop if necessary
+ * @UCA_PCO_CAMERA_RECORD_MODE_RING_BUFFER: Store frames in ring-buffer fashion
+ *      and overwrite if necessary
+ */
+
+/**
+ * UcaPcoCameraAcquireMode:
+ * @UCA_PCO_CAMERA_ACQUIRE_MODE_AUTO: Take all images
+ * @UCA_PCO_CAMERA_ACQUIRE_MODE_EXTERNAL: Use <acq enbl> signal
+ */
+
 /**
  * UcaPcoCameraError:
  * @UCA_PCO_CAMERA_ERROR_LIBPCO_INIT: Initializing libpco failed
@@ -156,51 +170,6 @@ struct _UcaPcoCameraPrivate {
     guint current_image;
 };
 
-/**
- * UcaPcoCameraRecordMode:
- * @UCA_PCO_CAMERA_RECORD_MODE_SEQUENCE: Store all frames and stop if necessary
- * @UCA_PCO_CAMERA_RECORD_MODE_RING_BUFFER: Store frames in ring-buffer fashion
- *      and overwrite if necessary
- */
-static GType uca_pco_camera_record_mode_get_type(void)
-{
-    static GType record_mode_type = 0;
-
-    if (!record_mode_type) {
-        static GEnumValue record_modes[] = {
-            { UCA_PCO_CAMERA_RECORD_MODE_SEQUENCE, "Store frames in a ring buffer", "ring-buffer" },
-            { UCA_PCO_CAMERA_RECORD_MODE_RING_BUFFER, "Store frames in a sequence", "sequence" },
-            { 0, NULL, NULL }
-        }; 
-
-        record_mode_type = g_enum_register_static("UcaPcoCameraRecordMode", record_modes);
-    }
-
-    return record_mode_type;
-}
-
-/**
- * UcaPcoCameraAcquireMode:
- * @UCA_PCO_CAMERA_ACQUIRE_MODE_AUTO: Take all images
- * @UCA_PCO_CAMERA_ACQUIRE_MODE_EXTERNAL: Use <acq enbl> signal
- */
-static GType uca_pco_camera_acquire_mode_get_type(void)
-{
-    static GType acquire_mode_type = 0;
-
-    if (!acquire_mode_type) {
-        static GEnumValue acquire_modes[] = {
-            { UCA_PCO_CAMERA_ACQUIRE_MODE_AUTO, "Take all images", "auto" },
-            { UCA_PCO_CAMERA_ACQUIRE_MODE_EXTERNAL, "Use <acq enbl> signal", "external" },
-            { 0, NULL, NULL }
-        }; 
-
-        acquire_mode_type = g_enum_register_static("UcaPcoCameraAcquireMode", acquire_modes);
-    }
-
-    return acquire_mode_type;
-}
-
 static pco_cl_map_entry pco_cl_map[] = { 
     { CAMERATYPE_PCO_EDGE,       "libFullAreaGray8.so",  FG_CL_8BIT_FULL_10,        FG_GRAY,     30.0f, FALSE },
     { CAMERATYPE_PCO4000,        "libDualAreaGray16.so", FG_CL_SINGLETAP_16_BIT,    FG_GRAY16,    5.0f, TRUE },
diff --git a/src/cameras/uca-pco-camera.h b/src/cameras/uca-pco-camera.h
index 243fac4..d2892b2 100644
--- a/src/cameras/uca-pco-camera.h
+++ b/src/cameras/uca-pco-camera.h
@@ -42,15 +42,11 @@ typedef struct _UcaPcoCamera           UcaPcoCamera;
 typedef struct _UcaPcoCameraClass      UcaPcoCameraClass;
 typedef struct _UcaPcoCameraPrivate    UcaPcoCameraPrivate;
 
-#define UCA_TYPE_PCO_CAMERA_RECORD_MODE (uca_pco_camera_record_mode_get_type())
-
 typedef enum {
     UCA_PCO_CAMERA_RECORD_MODE_SEQUENCE,
     UCA_PCO_CAMERA_RECORD_MODE_RING_BUFFER,
 } UcaPcoCameraRecordMode;
 
-#define UCA_TYPE_PCO_CAMERA_ACQUIRE_MODE (uca_pco_camera_acquire_mode_get_type())
-
 typedef enum {
     UCA_PCO_CAMERA_ACQUIRE_MODE_AUTO,
     UCA_PCO_CAMERA_ACQUIRE_MODE_EXTERNAL
diff --git a/src/uca-camera.c b/src/uca-camera.c
index fcb2ab7..5844eef 100644
--- a/src/uca-camera.c
+++ b/src/uca-camera.c
@@ -18,6 +18,7 @@
 #include <glib.h>
 #include "config.h"
 #include "uca-camera.h"
+#include "uca-enums.h"
 
 #ifdef HAVE_PCO_CL
 #include "cameras/uca-pco-camera.h"
@@ -35,6 +36,14 @@
 
 G_DEFINE_TYPE(UcaCamera, uca_camera, G_TYPE_OBJECT)
 
+/**
+ * UcaCameraTrigger:
+ * @UCA_CAMERA_TRIGGER_AUTO: Trigger automatically
+ * @UCA_CAMERA_TRIGGER_EXTERNAL: Trigger from an external source
+ * @UCA_CAMERA_TRIGGER_INTERNAL: Trigger internally from software using
+ *      #uca_camera_trigger
+ */
+
 /**
  * UcaCameraError:
  * @UCA_CAMERA_ERROR_NOT_FOUND: Camera type is unknown
@@ -103,31 +112,6 @@ struct _UcaCameraPrivate {
     gboolean transfer_async;
 };
 
-/**
- * UcaCameraTrigger:
- * @UCA_CAMERA_TRIGGER_AUTO: Trigger automatically
- * @UCA_CAMERA_TRIGGER_EXTERNAL: Trigger from an external source
- * @UCA_CAMERA_TRIGGER_INTERNAL: Trigger internally from software using
- *      #uca_camera_trigger
- */
-static GType uca_camera_trigger_get_type(void)
-{
-    static GType camera_trigger_type = 0;
-
-    if (!camera_trigger_type) {
-        static GEnumValue trigger_types[] = {
-            { UCA_CAMERA_TRIGGER_AUTO,      "Automatic internal camera trigger",  "auto" },
-            { UCA_CAMERA_TRIGGER_EXTERNAL,  "External trigger",                   "external" },
-            { UCA_CAMERA_TRIGGER_INTERNAL,  "Internal software trigger",          "internal" },
-            { 0, NULL, NULL }
-        }; 
-
-        camera_trigger_type = g_enum_register_static("UcaCameraTrigger", trigger_types);
-    }
-
-    return camera_trigger_type;
-}
-
 static void uca_camera_set_property(GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
 {
     UcaCameraPrivate *priv = UCA_CAMERA_GET_PRIVATE(object);
diff --git a/src/uca-camera.h b/src/uca-camera.h
index 39fd7ab..e2887a0 100644
--- a/src/uca-camera.h
+++ b/src/uca-camera.h
@@ -40,8 +40,6 @@ typedef enum {
     UCA_CAMERA_ERROR_NOT_IMPLEMENTED
 } UcaCameraError;
 
-#define UCA_TYPE_CAMERA_TRIGGER     (uca_camera_trigger_get_type())
-
 typedef enum {
     UCA_CAMERA_TRIGGER_AUTO,
     UCA_CAMERA_TRIGGER_INTERNAL,
diff --git a/src/uca-enums.c.template b/src/uca-enums.c.template
new file mode 100644
index 0000000..4e4a8bb
--- /dev/null
+++ b/src/uca-enums.c.template
@@ -0,0 +1,44 @@
+/*** BEGIN file-header ***/
+#include <config.h>
+
+#include "uca-enums.h"
+
+/*** END file-header ***/
+
+/*** BEGIN file-production ***/
+/* enumerations from "@filename@" */
+#include "@filename@"
+/*** END file-production ***/
+
+
+/*** BEGIN value-header ***/
+GType
+@enum_name@_get_type (void)
+{
+  static volatile gsize g_define_type_id__volatile = 0;
+ 
+  if (g_once_init_enter (&g_define_type_id__volatile)) {
+    static const G@Type@Value values[] = {
+/*** END value-header ***/
+
+/*** BEGIN value-production ***/
+      { @VALUENAME@, "@VALUENAME@", "@valuenick@" },
+/*** END value-production ***/
+
+/*** BEGIN value-tail ***/
+      { 0, NULL, NULL }
+    };
+    GType g_define_type_id = 
+       g_@type@_register_static (g_intern_static_string ("@EnumName@"), values);
+      
+    g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
+  }
+    
+  return g_define_type_id__volatile;
+}
+
+/*** END value-tail ***/
+
+/*** BEGIN file-tail ***/
+
+/*** END file-tail ***/
diff --git a/src/uca-enums.h.template b/src/uca-enums.h.template
new file mode 100644
index 0000000..e9d00b9
--- /dev/null
+++ b/src/uca-enums.h.template
@@ -0,0 +1,25 @@
+/*** BEGIN file-header ***/
+
+#ifndef UCA_ENUMS_H
+#define UCA_ENUMS_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+/*** END file-header ***/
+
+/*** BEGIN file-production ***/
+
+/* enumerations from "@filename@" */
+/*** END file-production ***/
+
+/*** BEGIN value-header ***/
+GType @enum_name@_get_type (void) G_GNUC_CONST;
+#define UCA_TYPE_@ENUMSHORT@ (@enum_name@_get_type ())
+/*** END value-header ***/
+
+/*** BEGIN file-tail ***/
+G_END_DECLS
+
+#endif /* !UCA_ENUMS_H */
+/*** END file-tail ***/
-- 
cgit v1.2.3