Reintroduce constexpr to COLOR4D

This fixes the initialization-order fiasco in the color initialization
sequence, which was originally fixed by making COLOR4D constexpr, but
was then reintroduced when the assert was changed to wxASSERT (wxASSERT
is not compatible with constexpr).
This commit is contained in:
Ian McInerney 2023-04-05 14:07:01 +01:00
parent 1283c4713f
commit 33da9b2327
2 changed files with 37 additions and 5 deletions

View File

@ -521,11 +521,12 @@ COLOR4D& COLOR4D::Desaturate()
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 );
const COLOR4D COLOR4D::CLEAR( 1, 0, 1, 0 );
// These call the 5-argument constructor to get a constexpr COLOR4D object.
// The 5th argument isn't actually used at all, so its bool value doesn't matter.
constexpr COLOR4D COLOR4D::UNSPECIFIED( 0, 0, 0, 0, true );
constexpr COLOR4D COLOR4D::WHITE( 1, 1, 1, 1, true );
constexpr COLOR4D COLOR4D::BLACK( 0, 0, 0, 1, true );
constexpr COLOR4D COLOR4D::CLEAR( 1, 0, 1, 0, true );
double COLOR4D::Distance( const COLOR4D& other ) const

View File

@ -129,6 +129,37 @@ public:
wxASSERT( a >= 0.0 && a <= 1.0 );
}
/**
* A COLOR4D constructor that is constexpr-capable.
*
* This constructor simply adds a 5th unused argument to differentiate it from
* the normal 4-argument constructor, so any COLOR4D instances that should be compile-time
* created should call this 5-argument version, while everyother instance should use the
* 4-argument version.
*
* @param aRed is the red component [0.0 .. 1.0].
* @param aGreen is the green component [0.0 .. 1.0].
* @param aBlue is the blue component [0.0 .. 1.0].
* @param aAlpha is the alpha value [0.0 .. 1.0].
* @param aIsConst is just an argument to differentiate this constructor from the 4-argument one,
* actual value doesn't matter.
*/
constexpr COLOR4D( double aRed, double aGreen, double aBlue, double aAlpha, bool aIsConst ) :
r( aRed ),
g( aGreen ),
b( aBlue ),
a( aAlpha )
{
/*!!!!!!!!!!!!!!!!!!!!!!
* DO NOT change these to wxASSERT or collapse this into the other constructor,
* it must remain separate to ensure COLOR4D objects can be compile-time constructed
*/
assert( r >= 0.0 && r <= 1.0 );
assert( g >= 0.0 && g <= 1.0 );
assert( b >= 0.0 && b <= 1.0 );
assert( a >= 0.0 && a <= 1.0 );
}
/**
* @param aColor is one of KiCad's palette colors.
* @see EDA_COLOR_T