Commit 612c375629f98b3f2e35862cbb1f068801aa6eed

sammy 2008-05-11T21:43:57

* Simplify the C bindings. 70 lines gained.

diff --git a/src/FTFont/FTFontGlue.cpp b/src/FTFont/FTFontGlue.cpp
index d3f8376..774ada7 100644
--- a/src/FTFont/FTFontGlue.cpp
+++ b/src/FTFont/FTFontGlue.cpp
@@ -114,25 +114,7 @@ C_TOR(ftglCreateCustomFont, (char const *fontFilePath, void *data,
             fprintf(stderr, "FTGL warning: NULL pointer in %s\n", #cname); \
             cxxerr; \
         } \
-        switch(f->type) \
-        { \
-            case FTGL::FONT_CUSTOM: \
-                return dynamic_cast<FTCustomFont*>(f->ptr)->cxxname cxxarg; \
-            case FTGL::FONT_BITMAP: \
-                return dynamic_cast<FTBitmapFont*>(f->ptr)->cxxname cxxarg; \
-            case FTGL::FONT_EXTRUDE: \
-                return dynamic_cast<FTExtrudeFont*>(f->ptr)->cxxname cxxarg; \
-            case FTGL::FONT_OUTLINE: \
-                return dynamic_cast<FTOutlineFont*>(f->ptr)->cxxname cxxarg; \
-            case FTGL::FONT_PIXMAP: \
-                return dynamic_cast<FTPixmapFont*>(f->ptr)->cxxname cxxarg; \
-            case FTGL::FONT_POLYGON: \
-                return dynamic_cast<FTPolygonFont*>(f->ptr)->cxxname cxxarg; \
-            case FTGL::FONT_TEXTURE: \
-                return dynamic_cast<FTTextureFont*>(f->ptr)->cxxname cxxarg; \
-        } \
-        fprintf(stderr, "FTGL warning: %s not implemented for %d\n", #cname, f->type); \
-        cxxerr; \
+        return f->ptr->cxxname cxxarg; \
     }
 
 // FTFont::~FTFont();
@@ -143,29 +125,7 @@ void ftglDestroyFont(FTGLfont *f)
         fprintf(stderr, "FTGL warning: NULL pointer in %s\n", __FUNCTION__);
         return;
     }
-    switch(f->type)
-    {
-        case FTGL::FONT_CUSTOM:
-            delete dynamic_cast<FTCustomFont*>(f->ptr); break;
-        case FTGL::FONT_BITMAP:
-            delete dynamic_cast<FTBitmapFont*>(f->ptr); break;
-        case FTGL::FONT_EXTRUDE:
-            delete dynamic_cast<FTExtrudeFont*>(f->ptr); break;
-        case FTGL::FONT_OUTLINE:
-            delete dynamic_cast<FTOutlineFont*>(f->ptr); break;
-        case FTGL::FONT_PIXMAP:
-            delete dynamic_cast<FTPixmapFont*>(f->ptr); break;
-        case FTGL::FONT_POLYGON:
-            delete dynamic_cast<FTPolygonFont*>(f->ptr); break;
-        case FTGL::FONT_TEXTURE:
-            delete dynamic_cast<FTTextureFont*>(f->ptr); break;
-        default:
-            fprintf(stderr, "FTGL warning: %s not implemented for %d\n",
-                            __FUNCTION__, f->type);
-            break;
-    }
-
-    f->ptr = NULL;
+    delete f->ptr;
     free(f);
 }
 
diff --git a/src/FTGlyph/FTGlyphGlue.cpp b/src/FTGlyph/FTGlyphGlue.cpp
index be9241e..f140ed0 100644
--- a/src/FTGlyph/FTGlyphGlue.cpp
+++ b/src/FTGlyph/FTGlyphGlue.cpp
@@ -139,25 +139,7 @@ C_TOR(ftglCreateCustomGlyph, (FTGLglyph *base, void *data,
             fprintf(stderr, "FTGL warning: NULL pointer in %s\n", #cname); \
             cxxerr; \
         } \
-        switch(g->type) \
-        { \
-            case FTGL::GLYPH_CUSTOM: \
-                return dynamic_cast<FTCustomGlyph*>(g->ptr)->cxxname cxxarg; \
-            case FTGL::GLYPH_BITMAP: \
-                return dynamic_cast<FTBitmapGlyph*>(g->ptr)->cxxname cxxarg; \
-            case FTGL::GLYPH_EXTRUDE: \
-                return dynamic_cast<FTExtrudeGlyph*>(g->ptr)->cxxname cxxarg; \
-            case FTGL::GLYPH_OUTLINE: \
-                return dynamic_cast<FTOutlineGlyph*>(g->ptr)->cxxname cxxarg; \
-            case FTGL::GLYPH_PIXMAP: \
-                return dynamic_cast<FTPixmapGlyph*>(g->ptr)->cxxname cxxarg; \
-            case FTGL::GLYPH_POLYGON: \
-                return dynamic_cast<FTPolygonGlyph*>(g->ptr)->cxxname cxxarg; \
-            case FTGL::GLYPH_TEXTURE: \
-                return dynamic_cast<FTTextureGlyph*>(g->ptr)->cxxname cxxarg; \
-        } \
-        fprintf(stderr, "FTGL warning: %s not implemented for %d\n", #cname, g->type); \
-        cxxerr; \
+        return g->ptr->cxxname cxxarg; \
     }
 
 // FTGlyph::~FTGlyph();
@@ -168,27 +150,7 @@ void ftglDestroyGlyph(FTGLglyph *g)
         fprintf(stderr, "FTGL warning: NULL pointer in %s\n", __FUNCTION__);
         return;
     }
-    switch(g->type)
-    {
-        case FTGL::GLYPH_CUSTOM:
-            delete dynamic_cast<FTCustomGlyph*>(g->ptr); break;
-        case FTGL::GLYPH_EXTRUDE:
-            delete dynamic_cast<FTExtrudeGlyph*>(g->ptr); break;
-        case FTGL::GLYPH_OUTLINE:
-            delete dynamic_cast<FTOutlineGlyph*>(g->ptr); break;
-        case FTGL::GLYPH_PIXMAP:
-            delete dynamic_cast<FTPixmapGlyph*>(g->ptr); break;
-        case FTGL::GLYPH_POLYGON:
-            delete dynamic_cast<FTPolygonGlyph*>(g->ptr); break;
-        case FTGL::GLYPH_TEXTURE:
-            delete dynamic_cast<FTTextureGlyph*>(g->ptr); break;
-        default:
-            fprintf(stderr, "FTGL warning: %s not implemented for %d\n",
-                            __FUNCTION__, g->type);
-            break;
-    }
-
-    g->ptr = NULL;
+    delete g->ptr;
     free(g);
 }
 
diff --git a/src/FTLayout/FTLayoutGlue.cpp b/src/FTLayout/FTLayoutGlue.cpp
index 7cbdde4..d1566f4 100644
--- a/src/FTLayout/FTLayoutGlue.cpp
+++ b/src/FTLayout/FTLayoutGlue.cpp
@@ -58,13 +58,7 @@ C_TOR(ftglCreateSimpleLayout, (), FTSimpleLayout, (), LAYOUT_SIMPLE);
             fprintf(stderr, "FTGL warning: NULL pointer in %s\n", #cname); \
             cxxerr; \
         } \
-        switch(l->type) \
-        { \
-            case FTGL::LAYOUT_SIMPLE: \
-                return dynamic_cast<FTSimpleLayout*>(l->ptr)->cxxname cxxarg; \
-        } \
-        fprintf(stderr, "FTGL warning: %s not implemented for %d\n", #cname, l->type); \
-        cxxerr; \
+        return l->ptr->cxxname cxxarg; \
     }
 
 // FTLayout::~FTLayout();
@@ -75,16 +69,7 @@ void ftglDestroyLayout(FTGLlayout *l)
         fprintf(stderr, "FTGL warning: NULL pointer in %s\n", __FUNCTION__);
         return;
     }
-    switch(l->type)
-    {
-        case FTGL::LAYOUT_SIMPLE:
-            delete dynamic_cast<FTSimpleLayout*>(l->ptr); break;
-        default:
-            fprintf(stderr, "FTGL warning: %s not implemented for %d\n",
-                            __FUNCTION__, l->type);
-    }
-
-    l->ptr = NULL;
+    delete l->ptr;
     free(l);
 }
 
@@ -106,22 +91,24 @@ void ftgGetlLayoutBBox(FTGLlayout *l, const char * s, float c[6])
 C_FUN(void, ftglRenderLayout, (FTGLlayout *l, const char *s, int r),
       return, Render, (s, r));
 
-// void SetFont(FTFont *fontInit)
+// FT_Error FTLayout::Error() const;
+C_FUN(FT_Error, ftglGetLayoutError, (FTGLlayout *l), return -1, Error, ());
+
+// void FTSimpleLayout::SetFont(FTFont *fontInit)
 void ftglSetLayoutFont(FTGLlayout *l, FTGLfont *font)
 {
     if(!l || !l->ptr)
     {
-        //XXX fprintf(stderr, "FTGL warning: NULL pointer in %s\n", __func__);
+        fprintf(stderr, "FTGL warning: NULL pointer in %s\n", __FUNCTION__);
         return;
     }
-    switch(l->type)
+    if(l->type != FTGL::LAYOUT_SIMPLE)
     {
-        case FTGL::LAYOUT_SIMPLE:
-            l->font = font;
-            return dynamic_cast<FTSimpleLayout*>(l->ptr)->SetFont(font->ptr);
+        fprintf(stderr, "FTGL warning: %s not implemented for %d\n",
+                        __FUNCTION__, l->type);
     }
-    fprintf(stderr, "FTGL warning: %s not implemented for %d\n",
-                    __FUNCTION__, l->type);
+    l->font = font;
+    return dynamic_cast<FTSimpleLayout*>(l->ptr)->SetFont(font->ptr);
 }
 
 // FTFont *FTSimpleLayout::GetFont()
@@ -132,9 +119,33 @@ FTGLfont *ftglGetLayoutFont(FTGLlayout *l)
         fprintf(stderr, "FTGL warning: NULL pointer in %s\n", __FUNCTION__);
         return NULL;
     }
+    if(l->type != FTGL::LAYOUT_SIMPLE)
+    {
+        fprintf(stderr, "FTGL warning: %s not implemented for %d\n",
+                        __FUNCTION__, l->type);
+    }
     return l->font;
 }
 
+#undef C_FUN
+
+#define C_FUN(cret, cname, cargs, cxxerr, cxxname, cxxarg) \
+    cret cname cargs \
+    { \
+        if(!l || !l->ptr) \
+        { \
+            fprintf(stderr, "FTGL warning: NULL pointer in %s\n", #cname); \
+            cxxerr; \
+        } \
+        if(l->type != FTGL::LAYOUT_SIMPLE) \
+        { \
+            fprintf(stderr, "FTGL warning: %s not implemented for %d\n", \
+                            __FUNCTION__, l->type); \
+            cxxerr; \
+        } \
+        return dynamic_cast<FTSimpleLayout*>(l->ptr)->cxxname cxxarg; \
+    }
+
 // void FTSimpleLayout::SetLineLength(const float LineLength);
 C_FUN(void, ftglSetLayoutLineLength, (FTGLlayout *l, const float length),
       return, SetLineLength, (length));
@@ -155,12 +166,5 @@ C_FUN(int, ftglGetLayoutAlignement, (FTGLlayout *l),
 C_FUN(void, ftglSetLayoutLineSpacing, (FTGLlayout *l, const float f),
       return, SetLineSpacing, (f));
 
-// float FTSimpleLayout::GetLineSpacing() const
-C_FUN(float, ftglGetLayoutLineSpacing, (FTGLlayout *l),
-      return 0.0f, GetLineSpacing, ());
-
-// FT_Error FTLayout::Error() const;
-C_FUN(FT_Error, ftglGetLayoutError, (FTGLlayout *l), return -1, Error, ());
-
 FTGL_END_C_DECLS