← Back to team overview

kicad-developers team mailing list archive

Re: [Patch] Fix initialization order for COLOR4D statics

 

The updated patch using constexpr (which now only affects the color4d
files) is attached.

Simon, thanks for the suggestion to use constexpr. It ended up working by
just declaring the variables constexpr and also making one of the
constructors constexpr. Definitely much simpler.

-Ian

On Mon, Aug 5, 2019 at 2:10 PM Wayne Stambaugh <stambaughw@xxxxxxxxx> wrote:

> On 8/5/19 5:03 AM, Simon Richter wrote:
> > Hi,
> >
> > On Mon, Aug 05, 2019 at 10:58:44AM +0200, Ian McInerney wrote:
> >
> >> I tracked it down to the fact that COLOR4D has some static colors that
> were
> >> being used to initialize some other static variables in pcbnew's layer
> >> widgets. I have moved all the static colors to an initialize on first
> use
> >> paradigm (so now we just call them like a function, e.g.
> >> COLOR4D::UNSPECIFIED() ) to fix it.
> >
> > Can that be solved by constexpr?
> >
> >    Simon
> >
>
> I would prefer the constexpr solution if it resolves the issue.  It
> certainly would be a much simpler patch.
>
> Wayne
>
> _______________________________________________
> Mailing list: https://launchpad.net/~kicad-developers
> Post to     : kicad-developers@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~kicad-developers
> More help   : https://help.launchpad.net/ListHelp
>
From 06c283922ae6c01be0de45c74c25185c6884955a Mon Sep 17 00:00:00 2001
From: Ian McInerney <Ian.S.McInerney@xxxxxxxx>
Date: Mon, 5 Aug 2019 15:54:13 +0200
Subject: [PATCH] Fix initialization of COLOR4D statics

Just declaring as static const would give an initialization order
fiasco since they were being used to initialize other statics.
---
 common/gal/color4d.cpp | 6 +++---
 include/gal/color4d.h  | 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/common/gal/color4d.cpp b/common/gal/color4d.cpp
index bc71f45bc..eadc3c220 100644
--- a/common/gal/color4d.cpp
+++ b/common/gal/color4d.cpp
@@ -362,6 +362,6 @@ COLOR4D& COLOR4D::Saturate( double aFactor )
     return *this;
 }
 
-const COLOR4D COLOR4D::UNSPECIFIED( 0, 0, 0, 0 );
-const COLOR4D COLOR4D::WHITE( 1, 1, 1, 1 );
-const COLOR4D COLOR4D::BLACK( 0, 0, 0, 1 );
+constexpr COLOR4D COLOR4D::UNSPECIFIED( 0, 0, 0, 0 );
+constexpr COLOR4D COLOR4D::WHITE( 1, 1, 1, 1 );
+constexpr COLOR4D COLOR4D::BLACK( 0, 0, 0, 1 );
diff --git a/include/gal/color4d.h b/include/gal/color4d.h
index 1b3932623..acd48205e 100644
--- a/include/gal/color4d.h
+++ b/include/gal/color4d.h
@@ -2,7 +2,7 @@
  * This program source code file is part of KICAD, a free EDA CAD application.
  *
  * Copyright (C) 2012 Torsten Hueter, torstenhtr <at> gmx.de
- * Copyright (C) 2017 Kicad Developers, see AUTHORS.txt for contributors.
+ * Copyright (C) 2017-2019 Kicad Developers, see AUTHORS.txt for contributors.
  *
  * Color class
  *
@@ -53,7 +53,7 @@ public:
      * @param aBlue  is the blue component  [0.0 .. 1.0].
      * @param aAlpha is the alpha value     [0.0 .. 1.0].
      */
-    COLOR4D( double aRed, double aGreen, double aBlue, double aAlpha ) :
+    constexpr COLOR4D( double aRed, double aGreen, double aBlue, double aAlpha ) :
         r( aRed ), g( aGreen ), b( aBlue ), a( aAlpha )
     {
         assert( r >= 0.0 && r <= 1.0 );
-- 
2.21.0


Follow ups

References