]> granicus.if.org Git - graphviz/commitdiff
gdiplus plugin: remove the use of C-style casts to convert void pointers
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 18 Apr 2021 00:35:33 +0000 (17:35 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 24 Apr 2021 03:14:26 +0000 (20:14 -0700)
In C++, C-style casts are a heavy hammer that is generally too powerful for your
needs. It effectively says to the compiler, “shut up, I know what I’m doing” and
impedes its ability to give you useful diagnostics. By using less powerful
reinterpret_casts, we preserve the compiler’s ability to warn about, e.g.,
implicitly converting a const pointer to non-const.

plugin/gdiplus/gvloadimage_gdiplus.cpp
plugin/gdiplus/gvrender_gdiplus.cpp
plugin/gdiplus/gvtextlayout_gdiplus.cpp

index bac2d9efc4ce7e6953eab04ca92c5ac73958ec75..bc0ca17e5ccb4758958504222bf7274a476e51f2 100644 (file)
@@ -57,15 +57,17 @@ static Image* gdiplus_loadimage(GVJ_t * job, usershape_t *us)
                        
                gvusershape_file_release(us);
     }
-    return (Image *)(us->data);
+    return reinterpret_cast<Image*>(us->data);
 }
 
 static void gdiplus_loadimage_gdiplus(GVJ_t * job, usershape_t *us, boxf b, boolean filled)
 {
        /* get the image from usershape details, then blit it to the context */
        Image *image = gdiplus_loadimage(job, us);
-       if (image)
-               ((Graphics *)job->context)->DrawImage(image, RectF(b.LL.x, b.LL.y, b.UR.x - b.LL.x, b.UR.y - b.LL.y));
+       if (image) {
+               auto g = reinterpret_cast<Graphics*>(job->context);
+               g->DrawImage(image, RectF(b.LL.x, b.LL.y, b.UR.x - b.LL.x, b.UR.y - b.LL.y));
+       }
 }
 
 static gvloadimage_engine_t engine = {
index f2dba8a45ea0dd899c82f4848b8be76d633ced6c..f8661623e58c0e9d388a5f704e283cc788f3b507 100644 (file)
@@ -46,14 +46,15 @@ static void gdiplusgen_begin_job(GVJ_t *job)
        {
                /* save the passed-in context in the window field, so we can create a Metafile in the context field later on */
                job->window = job->context;
-               *((Metafile**)job->window) = NULL;
+               auto m = reinterpret_cast<Metafile**>(job->window);
+               *m = NULL;
                job->context = NULL;
 }
 }
 
 static void gdiplusgen_end_job(GVJ_t *job)
 {
-               Graphics *context = (Graphics *)job->context;
+               auto context = reinterpret_cast<Graphics*>(job->context);
                
        if (!job->external_context) {
                /* flush and delete the graphics */
@@ -129,7 +130,8 @@ static void gdiplusgen_begin_page(GVJ_t *job)
                                RectF(0.0f, 0.0f, job->width, job->height),
                                MetafileFrameUnitPixel,
                                EmfTypeEmfPlusOnly);
-                       *((Metafile**)job->window) = metafile;
+                       auto m = reinterpret_cast<Metafile**>(job->window);
+                       *m = metafile;
                        job->context = new Graphics(metafile);
                }
        }
@@ -151,7 +153,7 @@ static void gdiplusgen_begin_page(GVJ_t *job)
 
 static void gdiplusgen_textspan(GVJ_t *job, pointf p, textspan_t *span)
 {
-       Graphics* context = (Graphics*)job->context;
+       auto context = reinterpret_cast<Graphics*>(job->context);
                
                /* adjust text position */
                switch (span->just) {
@@ -170,7 +172,7 @@ static void gdiplusgen_textspan(GVJ_t *job, pointf p, textspan_t *span)
 
        Layout* layout;
        if (span->free_layout == &gdiplus_free_layout)
-               layout = (Layout*)span->layout;
+               layout = reinterpret_cast<Layout*>(span->layout);
        else
                layout = new Layout(span->font->name, span->font->size, span->str);
 
@@ -195,7 +197,7 @@ static vector<PointF> points(pointf *A, int n)
 
 static void gdiplusgen_path(GVJ_t *job, const GraphicsPath *path, int filled)
 {
-       Graphics *context = (Graphics *)job->context;
+       auto context = reinterpret_cast<Graphics *>(job->context);
        
        /* fill the given path with job fill color */
        if (filled) {
index 66f5ef695f79c278278da5eb073883742c1a9616..3fc4ab3d297629f71b95c5bd446427ad0f78bd19 100644 (file)
@@ -63,7 +63,7 @@ Layout::Layout(char *fontname, double fontsize, char* string)
 void gdiplus_free_layout(void *layout)
 {
        if (layout)
-               delete (Layout*)layout;
+               delete reinterpret_cast<Layout*>(layout);
 };
 
 boolean gdiplus_textlayout(textspan_t *span, char **fontpath)