From 2a0abed6d272f019bcd6befba4c1f1b486104855 Mon Sep 17 00:00:00 2001 From: "Vogel, Rick" Date: Mon, 20 Mar 2023 11:59:36 +0100 Subject: [PATCH] make literal constexpr check configurable --- features/qskconfig.pri | 1 + src/common/QskRgbValue.h | 33 +++++++++++++++------------------ src/src.pro | 1 + tests/test_QskRgbLiterals.cpp | 19 ------------------- tests/test_QskRgbLiterals.h | 2 ++ tests/tests_main.cpp | 2 +- tests/tests_main.h | 3 +++ 7 files changed, 23 insertions(+), 38 deletions(-) diff --git a/features/qskconfig.pri b/features/qskconfig.pri index 1b3f20fb..b1c40d56 100644 --- a/features/qskconfig.pri +++ b/features/qskconfig.pri @@ -63,6 +63,7 @@ OBJECTS_DIR = obj RCC_DIR = rcc QSK_CONFIG += QskDll +QSK_CONFIG += QskRequireConstexprLiterals linux { diff --git a/src/common/QskRgbValue.h b/src/common/QskRgbValue.h index bb9960c9..70bbf7f8 100644 --- a/src/common/QskRgbValue.h +++ b/src/common/QskRgbValue.h @@ -458,27 +458,26 @@ namespace QskRgb } } -#define QSK_REQUIRE_CONSTEXPR_LITERAL 1 -#ifdef QSK_REQUIRE_CONSTEXPR_LITERAL +#ifdef QSK_REQUIRE_CONSTEXPR_LITERALS // RGBA hex string to QRgb with defaulted alpha = 0xFF - static_assert( "#123456"_rgba == 0xFF123456, "not constexpr" ); - static_assert( "#123456"_argb == 0xFF123456, "not constexpr" ); + static_assert( "#123456"_rgba == 0xFF123456, "invalid or not constexpr" ); + static_assert( "#123456"_argb == 0xFF123456, "invalid or not constexpr" ); // ARGB hex string to QRgb with defaulted alpha = 0xFF - static_assert( "#AA112233"_argb == 0xAA112233, "not constexpr" ); - static_assert( "#112233AA"_rgba == 0xAA112233, "not constexpr" ); + static_assert( "#AA112233"_argb == 0xAA112233, "invalid or not constexpr" ); + static_assert( "#112233AA"_rgba == 0xAA112233, "invalid or not constexpr" ); // RGBA hex literal to QRgb with defaulted alpha = 0xFF - static_assert( 0x112233_rgba == 0xFF112233, "" ); - static_assert( 0xaabbcc_rgba == 0xFFAABBCC, "" ); - static_assert( 0xAABBCC_rgba == 0xFFAABBCC, "" ); - static_assert( 0x112233aa_rgba == 0xaa112233, "" ); + static_assert( 0x112233_rgba == 0xFF112233, "invalid or not constexpr" ); + static_assert( 0xaabbcc_rgba == 0xFFAABBCC, "invalid or not constexpr" ); + static_assert( 0xAABBCC_rgba == 0xFFAABBCC, "invalid or not constexpr" ); + static_assert( 0x112233aa_rgba == 0xaa112233, "invalid or not constexpr" ); // ARGB hex literal to QRgb with defaulted alpha = 0xFF - static_assert( 0x112233_argb == 0xFF112233, "" ); - static_assert( 0xaabbcc_argb == 0xFFAABBCC, "" ); - static_assert( 0xAABBCC_argb == 0xFFAABBCC, "" ); - static_assert( 0x112233aa_argb == 0x112233aa, "" ); + static_assert( 0x112233_argb == 0xFF112233, "invalid or not constexpr" ); + static_assert( 0xaabbcc_argb == 0xFFAABBCC, "invalid or not constexpr" ); + static_assert( 0xAABBCC_argb == 0xFFAABBCC, "invalid or not constexpr" ); + static_assert( 0x112233aa_argb == 0x112233aa, "invalid or not constexpr" ); #endif } @@ -493,8 +492,7 @@ namespace QskRgb const auto g = ( argb >> ( len == max ? 16 : 8 ) ) & 0xFF; const auto b = ( argb >> ( len == max ? 8 : 0 ) ) & 0xFF; const auto a = ( len == max ? argb & 0xFF : 0xFF ); - const auto color = QColor{ r, g, b, a }; - return color; + return { r, g, b, a }; } QSK_EXPORT constexpr QColor operator""_argb( @@ -506,8 +504,7 @@ namespace QskRgb const auto g = ( argb >> 8 ) & 0xFF; const auto b = ( argb >> 0 ) & 0xFF; const auto a = ( len == max ? argb >> 24 : 0xFF ) & 0xFF; - const auto color = QColor{ r, g, b, a }; - return color; + return { r, g, b, a }; } // converts a hex literal from '0xRRGGBB[AA]' to a QColor diff --git a/src/src.pro b/src/src.pro index 6cd9ba77..e0070a42 100644 --- a/src/src.pro +++ b/src/src.pro @@ -8,6 +8,7 @@ greaterThan( QT_MAJOR_VERSION, 5 ) { } contains(QSK_CONFIG, QskDll): DEFINES += QSK_MAKEDLL +contains(QSK_CONFIG, QskRequireConstexprLiterals): DEFINES += QSK_REQUIRE_CONSTEXPR_LITERALS QSK_SUBDIRS = common graphic nodes controls layouts dialogs inputpanel INCLUDEPATH *= $${QSK_SUBDIRS} diff --git a/tests/test_QskRgbLiterals.cpp b/tests/test_QskRgbLiterals.cpp index bea62d0b..c8fdbe79 100644 --- a/tests/test_QskRgbLiterals.cpp +++ b/tests/test_QskRgbLiterals.cpp @@ -56,25 +56,6 @@ void QskRgbLiterals::qrgbLiterals_data() void QskRgbLiterals::qrgbLiterals() { using namespace QskRgb::literals::qrgb; - -#ifdef QSK_REQUIRE_CONSTEXPR_LITERAL - static_assert( "#123456"_rgba == 0xFF123456, "not constexpr" ); - static_assert( "#123456"_argb == 0xFF123456, "not constexpr" ); - - static_assert( "#AA112233"_argb == 0xAA112233, "not constexpr" ); - static_assert( "#112233AA"_rgba == 0xAA112233, "not constexpr" ); - - static_assert( 0x112233_rgba == 0xFF112233, "not constexpr" ); - static_assert( 0xaabbcc_rgba == 0xFFAABBCC, "not constexpr" ); - static_assert( 0xAABBCC_rgba == 0xFFAABBCC, "not constexpr" ); - static_assert( 0x112233aa_rgba == 0xaa112233, "not constexpr" ); - - static_assert( 0x112233_argb == 0xFF112233, "not constexpr" ); - static_assert( 0xaabbcc_argb == 0xFFAABBCC, "not constexpr" ); - static_assert( 0xAABBCC_argb == 0xFFAABBCC, "not constexpr" ); - static_assert( 0x112233aa_argb == 0x112233aa, "not constexpr" ); -#endif - QFETCH( QRgb, actual ); QFETCH( QRgb, expected ); QCOMPARE( actual, expected ); diff --git a/tests/test_QskRgbLiterals.h b/tests/test_QskRgbLiterals.h index e2ac0147..b353e282 100644 --- a/tests/test_QskRgbLiterals.h +++ b/tests/test_QskRgbLiterals.h @@ -1,3 +1,5 @@ +#pragma once + #include class QskRgbLiterals final : public QObject diff --git a/tests/tests_main.cpp b/tests/tests_main.cpp index afaa16ed..ddefcfce 100644 --- a/tests/tests_main.cpp +++ b/tests/tests_main.cpp @@ -1,4 +1,4 @@ -#include "test_QskRgbLiterals.h" +#include "tests_main.h" #include QTEST_APPLESS_MAIN( QskRgbLiterals ) diff --git a/tests/tests_main.h b/tests/tests_main.h index e69de29b..2869350a 100644 --- a/tests/tests_main.h +++ b/tests/tests_main.h @@ -0,0 +1,3 @@ +#pragma once + +#include "test_QskRgbLiterals.h"