shithub: choc

Download patch

ref: 11643c088751a2d3797467463bce4476d3b03292
parent: 8d188ff3d11c199621c56275d4fd6730d6f63c1b
author: Simon Howard <[email protected]>
date: Sun May 21 20:56:12 EDT 2006

Add casting macros to allow for easy casts between types.

Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 503

--- a/textscreen/guitest.c
+++ b/textscreen/guitest.c
@@ -1,3 +1,4 @@
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -22,7 +23,7 @@
 txt_window_t *firstwin;
 int checkbox_value;
 
-void CloseWindow(txt_widget_t *widget, void *user_data)
+void CloseWindow(UNCAST(button), void *user_data)
 {
     TXT_CloseWindow(firstwin);
 }
--- a/textscreen/txt_button.c
+++ b/textscreen/txt_button.c
@@ -6,12 +6,11 @@
 #include "txt_button.h"
 #include "txt_io.h"
 #include "txt_main.h"
-#include "txt_widget.h"
 #include "txt_window.h"
 
-static void TXT_ButtonSizeCalc(txt_widget_t *widget, int *w, int *h)
+static void TXT_ButtonSizeCalc(UNCAST(button), int *w, int *h)
 {
-    txt_button_t *button = (txt_button_t *) widget;
+    CAST(txt_button_t, button);
 
     // Minimum width is the string length + two spaces for padding
 
@@ -19,9 +18,9 @@
     *h = 1;
 }
 
-static void TXT_ButtonDrawer(txt_widget_t *widget, int w, int selected)
+static void TXT_ButtonDrawer(UNCAST(button), int w, int selected)
 {
-    txt_button_t *button = (txt_button_t *) widget;
+    CAST(txt_button_t, button);
     int i;
 
     TXT_BGColor(TXT_COLOR_BLUE, 0);
@@ -41,18 +40,20 @@
     }
 }
 
-static void TXT_ButtonDestructor(txt_widget_t *widget)
+static void TXT_ButtonDestructor(UNCAST(button))
 {
-    txt_button_t *button = (txt_button_t *) widget;
+    CAST(txt_button_t, button);
 
     free(button->label);
 }
 
-static int TXT_ButtonKeyPress(txt_widget_t *widget, int key)
+static int TXT_ButtonKeyPress(UNCAST(button), int key)
 {
+    CAST(txt_button_t, button);
+
     if (key == KEY_ENTER)
     {
-        TXT_EmitSignal(widget, "pressed");
+        TXT_EmitSignal(button, "pressed");
     }
     
     return 0;
--- a/textscreen/txt_checkbox.c
+++ b/textscreen/txt_checkbox.c
@@ -8,9 +8,9 @@
 #include "txt_main.h"
 #include "txt_window.h"
 
-static void TXT_CheckBoxSizeCalc(txt_widget_t *widget, int *w, int *h)
+static void TXT_CheckBoxSizeCalc(UNCAST(checkbox), int *w, int *h)
 {
-    txt_checkbox_t *checkbox = (txt_checkbox_t *) widget;
+    CAST(txt_checkbox_t, checkbox);
 
     // Minimum width is the string length + two spaces for padding
 
@@ -18,9 +18,9 @@
     *h = 1;
 }
 
-static void TXT_CheckBoxDrawer(txt_widget_t *widget, int w, int selected)
+static void TXT_CheckBoxDrawer(UNCAST(checkbox), int w, int selected)
 {
-    txt_checkbox_t *checkbox = (txt_checkbox_t *) widget;
+    CAST(txt_checkbox_t, checkbox);
     int i;
 
     TXT_BGColor(TXT_COLOR_BLUE, 0);
@@ -57,21 +57,21 @@
     }
 }
 
-static void TXT_CheckBoxDestructor(txt_widget_t *widget)
+static void TXT_CheckBoxDestructor(UNCAST(checkbox))
 {
-    txt_checkbox_t *checkbox = (txt_checkbox_t *) widget;
+    CAST(txt_checkbox_t, checkbox);
 
     free(checkbox->label);
 }
 
-static int TXT_CheckBoxKeyPress(txt_widget_t *widget, int key)
+static int TXT_CheckBoxKeyPress(UNCAST(checkbox), int key)
 {
-    txt_checkbox_t *checkbox = (txt_checkbox_t *) widget;
+    CAST(txt_checkbox_t, checkbox);
 
     if (key == KEY_ENTER || key == ' ')
     {
         *checkbox->variable = !*checkbox->variable;
-        TXT_EmitSignal(widget, "changed");
+        TXT_EmitSignal(checkbox, "changed");
         return 1;
     }
     
--- a/textscreen/txt_label.c
+++ b/textscreen/txt_label.c
@@ -4,20 +4,19 @@
 #include "txt_label.h"
 #include "txt_io.h"
 #include "txt_main.h"
-#include "txt_widget.h"
 #include "txt_window.h"
 
-static void TXT_LabelSizeCalc(txt_widget_t *widget, int *w, int *h)
+static void TXT_LabelSizeCalc(UNCAST(label), int *w, int *h)
 {
-    txt_label_t *label = (txt_label_t *) widget;
+    CAST(txt_label_t, label);
 
     *w = label->w;
     *h = label->h;
 }
 
-static void TXT_LabelDrawer(txt_widget_t *widget, int w, int selected)
+static void TXT_LabelDrawer(UNCAST(label), int w, int selected)
 {
-    txt_label_t *label = (txt_label_t *) widget;
+    CAST(txt_label_t, label);
     int i;
     int origin_x, origin_y;
 
@@ -33,9 +32,9 @@
     }
 }
 
-static void TXT_LabelDestructor(txt_widget_t *widget)
+static void TXT_LabelDestructor(UNCAST(label))
 {
-    txt_label_t *label = (txt_label_t *) widget;
+    CAST(txt_label_t, label);
 
     free(label->label);
     free(label->lines);
--- a/textscreen/txt_radiobutton.c
+++ b/textscreen/txt_radiobutton.c
@@ -8,9 +8,9 @@
 #include "txt_main.h"
 #include "txt_window.h"
 
-static void TXT_RadioButtonSizeCalc(txt_widget_t *widget, int *w, int *h)
+static void TXT_RadioButtonSizeCalc(UNCAST(radiobutton), int *w, int *h)
 {
-    txt_radiobutton_t *radiobutton = (txt_radiobutton_t *) widget;
+    CAST(txt_radiobutton_t, radiobutton);
 
     // Minimum width is the string length + two spaces for padding
 
@@ -18,9 +18,9 @@
     *h = 1;
 }
 
-static void TXT_RadioButtonDrawer(txt_widget_t *widget, int w, int selected)
+static void TXT_RadioButtonDrawer(UNCAST(radiobutton), int w, int selected)
 {
-    txt_radiobutton_t *radiobutton = (txt_radiobutton_t *) widget;
+    CAST(txt_radiobutton_t, radiobutton);
     int i;
 
     TXT_BGColor(TXT_COLOR_BLUE, 0);
@@ -57,16 +57,16 @@
     }
 }
 
-static void TXT_RadioButtonDestructor(txt_widget_t *widget)
+static void TXT_RadioButtonDestructor(UNCAST(radiobutton))
 {
-    txt_radiobutton_t *radiobutton = (txt_radiobutton_t *) widget;
+    CAST(txt_radiobutton_t, radiobutton);
 
     free(radiobutton->label);
 }
 
-static int TXT_RadioButtonKeyPress(txt_widget_t *widget, int key)
+static int TXT_RadioButtonKeyPress(UNCAST(radiobutton), int key)
 {
-    txt_radiobutton_t *radiobutton = (txt_radiobutton_t *) widget;
+    CAST(txt_radiobutton_t, radiobutton);
 
     if (key == KEY_ENTER || key == ' ')
     {
@@ -73,7 +73,7 @@
         if (*radiobutton->variable != radiobutton->value)
         {
             *radiobutton->variable = radiobutton->value;
-            TXT_EmitSignal(widget, "selected");
+            TXT_EmitSignal(radiobutton, "selected");
         }
         return 1;
     }
--- a/textscreen/txt_separator.c
+++ b/textscreen/txt_separator.c
@@ -4,12 +4,11 @@
 #include "txt_separator.h"
 #include "txt_io.h"
 #include "txt_main.h"
-#include "txt_widget.h"
 #include "txt_window.h"
 
-static void TXT_SeparatorSizeCalc(txt_widget_t *widget, int *w, int *h)
+static void TXT_SeparatorSizeCalc(UNCAST(separator), int *w, int *h)
 {
-    txt_separator_t *separator = (txt_separator_t *) widget;
+    CAST(txt_separator_t, separator);
 
     if (separator->label != NULL)
     {
@@ -25,9 +24,9 @@
     *h = 1;
 }
 
-static void TXT_SeparatorDrawer(txt_widget_t *widget, int w, int selected)
+static void TXT_SeparatorDrawer(UNCAST(separator), int w, int selected)
 {
-    txt_separator_t *separator = (txt_separator_t *) widget;
+    CAST(txt_separator_t, separator);
     int i;
     int x, y;
 
@@ -50,9 +49,9 @@
     }
 }
 
-static void TXT_SeparatorDestructor(txt_widget_t *widget)
+static void TXT_SeparatorDestructor(UNCAST(separator))
 {
-    txt_separator_t *separator = (txt_separator_t *) widget;
+    CAST(txt_separator_t, separator);
 
     free(separator->label);
 }
--- a/textscreen/txt_table.c
+++ b/textscreen/txt_table.c
@@ -33,9 +33,9 @@
 #include "txt_separator.h"
 #include "txt_table.h"
 
-static void TXT_TableDestructor(txt_widget_t *widget)
+static void TXT_TableDestructor(UNCAST(table))
 {
-    txt_table_t *table = (txt_table_t *) widget;
+    CAST(txt_table_t, table);
     int i;
 
     // Free all widgets
@@ -90,9 +90,9 @@
     }
 }
 
-static void TXT_CalcTableSize(txt_widget_t *widget, int *w, int *h)
+static void TXT_CalcTableSize(UNCAST(table), int *w, int *h)
 {
-    txt_table_t *table = (txt_table_t *) widget;
+    CAST(txt_table_t, table);
     int *column_widths;
     int *row_heights;
     int x, y;
@@ -123,14 +123,11 @@
     free(column_widths);
 }
 
-void TXT_AddWidget(void *uncast_table, void *uncast_widget)
+void TXT_AddWidget(UNCAST(table), UNCAST(widget))
 {
-    txt_widget_t *widget;
-    txt_table_t *table;
+    CAST(txt_table_t, table);
+    CAST(txt_widget_t, widget);
 
-    table = (txt_table_t *) uncast_table;
-    widget = (txt_widget_t *) uncast_widget;
-
     if (table->num_widgets > 0)
     {
         txt_widget_t *last_widget;
@@ -210,9 +207,9 @@
     return -1;
 }
 
-static int TXT_TableKeyPress(txt_widget_t *widget, int key)
+static int TXT_TableKeyPress(UNCAST(table), int key)
 {
-    txt_table_t *table = (txt_table_t *) widget;
+    CAST(txt_table_t, table);
     int selected;
     int rows;
 
@@ -342,9 +339,9 @@
     }
 }
 
-static void TXT_TableDrawer(txt_widget_t *widget, int w, int selected)
+static void TXT_TableDrawer(UNCAST(table), int w, int selected)
 {
-    txt_table_t *table = (txt_table_t *) widget;
+    CAST(txt_table_t, table);
     int *column_widths;
     int *row_heights;
     int origin_x, origin_y;
--- a/textscreen/txt_table.h
+++ b/textscreen/txt_table.h
@@ -51,7 +51,7 @@
 
 txt_table_t *TXT_NewTable(int columns);
 void TXT_InitTable(txt_table_t *table, int columns);
-void TXT_AddWidget(void *table, void *widget);
+void TXT_AddWidget(UNCAST(table), UNCAST(widget));
 
 #endif /* #ifndef TXT_TABLE_T */
 
--- a/textscreen/txt_widget.c
+++ b/textscreen/txt_widget.c
@@ -40,9 +40,9 @@
     free(table);
 }
 
-void TXT_InitWidget(void *uncast_widget, txt_widget_class_t *widget_class)
+void TXT_InitWidget(UNCAST(widget), txt_widget_class_t *widget_class)
 {
-    txt_widget_t *widget = (txt_widget_t *) uncast_widget;
+    CAST(txt_widget_t, widget);
 
     widget->widget_class = widget_class;
     widget->callback_table = TXT_NewCallbackTable();
@@ -53,11 +53,12 @@
     widget->visible = 1;
 }
 
-void TXT_SignalConnect(txt_widget_t *widget, 
+void TXT_SignalConnect(UNCAST(widget),
                        char *signal_name,
                        TxtWidgetSignalFunc func, 
                        void *user_data)
 {
+    CAST(txt_widget_t, widget);
     txt_callback_table_t *table;
     txt_callback_t *callback;
     int i;
@@ -89,8 +90,9 @@
     callback->user_data = user_data;
 }
 
-void TXT_EmitSignal(txt_widget_t *widget, char *signal_name)
+void TXT_EmitSignal(UNCAST(widget), char *signal_name)
 {
+    CAST(txt_widget_t, widget);
     txt_callback_table_t *table;
     int i;
 
@@ -106,25 +108,33 @@
     }
 }
 
-void TXT_CalcWidgetSize(txt_widget_t *widget, int *w, int *h)
+void TXT_CalcWidgetSize(UNCAST(widget), int *w, int *h)
 {
+    CAST(txt_widget_t, widget);
+
     return widget->widget_class->size_calc(widget, w, h);
 }
 
-void TXT_DrawWidget(txt_widget_t *widget, int w, int selected)
+void TXT_DrawWidget(UNCAST(widget), int w, int selected)
 {
+    CAST(txt_widget_t, widget);
+
     widget->widget_class->drawer(widget, w, selected);
 }
 
-void TXT_DestroyWidget(txt_widget_t *widget)
+void TXT_DestroyWidget(UNCAST(widget))
 {
+    CAST(txt_widget_t, widget);
+
     widget->widget_class->destructor(widget);
     TXT_DestroyCallbackTable(widget->callback_table);
     free(widget);
 }
 
-int TXT_WidgetKeyPress(txt_widget_t *widget, int key)
+int TXT_WidgetKeyPress(UNCAST(widget), int key)
 {
+    CAST(txt_widget_t, widget);
+
     if (widget->widget_class->key_press != NULL)
     {
         return widget->widget_class->key_press(widget, key);
--- a/textscreen/txt_widget.h
+++ b/textscreen/txt_widget.h
@@ -27,15 +27,18 @@
 #ifndef TXT_WIDGET_H
 #define TXT_WIDGET_H
 
+#define UNCAST(name)   void *uncast_ ## name
+#define CAST(type, name)  type *name = (type *) uncast_ ## name
+
 typedef struct txt_widget_class_s txt_widget_class_t;
 typedef struct txt_widget_s txt_widget_t;
 typedef struct txt_callback_table_s txt_callback_table_t;
 
-typedef void (*TxtWidgetSizeCalc)(txt_widget_t *widget, int *w, int *h);
-typedef void (*TxtWidgetDrawer)(txt_widget_t *widget, int w, int selected);
-typedef void (*TxtWidgetDestroy)(txt_widget_t *widget);
-typedef int (*TxtWidgetKeyPress)(txt_widget_t *widget, int key);
-typedef void (*TxtWidgetSignalFunc)(txt_widget_t *widget, void *user_data);
+typedef void (*TxtWidgetSizeCalc)(UNCAST(widget), int *w, int *h);
+typedef void (*TxtWidgetDrawer)(UNCAST(widget), int w, int selected);
+typedef void (*TxtWidgetDestroy)(UNCAST(widget));
+typedef int (*TxtWidgetKeyPress)(UNCAST(widget), int key);
+typedef void (*TxtWidgetSignalFunc)(UNCAST(widget), void *user_data);
 
 struct txt_widget_class_s
 {
@@ -53,14 +56,14 @@
     int visible;
 };
 
-void TXT_InitWidget(void *widget, txt_widget_class_t *widget_class);
-void TXT_CalcWidgetSize(txt_widget_t *widget, int *w, int *h);
-void TXT_DrawWidget(txt_widget_t *widget, int w, int selected);
-void TXT_SignalConnect(txt_widget_t *widget, char *signal_name,
+void TXT_InitWidget(UNCAST(widget), txt_widget_class_t *widget_class);
+void TXT_CalcWidgetSize(UNCAST(widget), int *w, int *h);
+void TXT_DrawWidget(UNCAST(widget), int w, int selected);
+void TXT_SignalConnect(UNCAST(widget), char *signal_name,
                        TxtWidgetSignalFunc func, void *user_data);
-void TXT_EmitSignal(txt_widget_t *widget, char *signal_name);
-int TXT_WidgetKeyPress(txt_widget_t *widget, int key);
-void TXT_DestroyWidget(txt_widget_t *widget);
+void TXT_EmitSignal(UNCAST(widget), char *signal_name);
+int TXT_WidgetKeyPress(UNCAST(widget), int key);
+void TXT_DestroyWidget(UNCAST(widget));
 
 #endif /* #ifndef TXT_WIDGET_H */