diff --git a/modules/common/chowdsp_data_structures/Structures/chowdsp_StringLiteral.h b/modules/common/chowdsp_data_structures/Structures/chowdsp_StringLiteral.h index aed3713fd..6c84e5a73 100644 --- a/modules/common/chowdsp_data_structures/Structures/chowdsp_StringLiteral.h +++ b/modules/common/chowdsp_data_structures/Structures/chowdsp_StringLiteral.h @@ -27,15 +27,11 @@ namespace sl_detail /** A string-literal wrapper type. */ template -class StringLiteral +struct StringLiteral { std::array chars {}; size_t actual_size = 0; - template - friend constexpr StringLiteral operator+ (const StringLiteral&, const StringLiteral&); - -public: constexpr StringLiteral() = default; constexpr StringLiteral (const StringLiteral&) = default; constexpr StringLiteral& operator= (const StringLiteral&) = default; @@ -48,7 +44,7 @@ class StringLiteral } constexpr StringLiteral (const char (&str)[N]) // NOSONAR NOLINT(google-explicit-constructor) - : StringLiteral (str, std::integral_constant {}) + : StringLiteral (str, std::integral_constant {}) { } @@ -151,6 +147,24 @@ constexpr bool operator!= (const std::string_view& lhs, const StringLiteral& { return ! (lhs == rhs); } + +#if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) +namespace string_literals +{ + template + constexpr auto operator"" _sl() + { + return sl; + } + + template + constexpr auto operator"" _sl() + { + constexpr char str_array[] { str..., '\0' }; + return StringLiteral { str_array }; + } +} +#endif } // namespace chowdsp JUCE_END_IGNORE_WARNINGS_MSVC diff --git a/tests/common_tests/chowdsp_data_structures_test/ArenaAllocatorTest.cpp b/tests/common_tests/chowdsp_data_structures_test/ArenaAllocatorTest.cpp index e848c1cb1..a437d5cd6 100644 --- a/tests/common_tests/chowdsp_data_structures_test/ArenaAllocatorTest.cpp +++ b/tests/common_tests/chowdsp_data_structures_test/ArenaAllocatorTest.cpp @@ -32,7 +32,7 @@ TEST_CASE ("Arena Allocator Test", "[common][data-structures]") // aligned allocation { auto* some_data = allocator.allocate (1, 16); - REQUIRE (juce::snapPointerToAlignment (some_data, 16) == some_data); + REQUIRE (juce::snapPointerToAlignment (some_data, (size_t) 16) == some_data); } // overfull allocation diff --git a/tests/common_tests/chowdsp_data_structures_test/IteratorsTest.cpp b/tests/common_tests/chowdsp_data_structures_test/IteratorsTest.cpp index a72a7afd8..12b349707 100644 --- a/tests/common_tests/chowdsp_data_structures_test/IteratorsTest.cpp +++ b/tests/common_tests/chowdsp_data_structures_test/IteratorsTest.cpp @@ -199,8 +199,8 @@ TEST_CASE ("Zip-Multi") SECTION ("Filling Arrays") { const std::array x_int { 0, 1, 2, 3, 4, 5 }; - std::array x_float; - std::array x_double; + std::array x_float {}; + std::array x_double {}; for (auto [int_val, float_val, double_val] : chowdsp::zip_multi (x_int, x_float, x_double)) { float_val = static_cast (int_val); @@ -208,8 +208,8 @@ TEST_CASE ("Zip-Multi") } for (auto [int_val, float_val, double_val] : chowdsp::zip_multi (x_int, x_float, x_double)) { - REQUIRE (int_val == (float) float_val); - REQUIRE (int_val == (double) double_val); + REQUIRE (juce::exactlyEqual ((float) int_val, float_val)); + REQUIRE (juce::exactlyEqual ((double) int_val, double_val)); } } diff --git a/tests/common_tests/chowdsp_data_structures_test/StringLiteralTest.cpp b/tests/common_tests/chowdsp_data_structures_test/StringLiteralTest.cpp index 1236f0960..cd2cc0088 100644 --- a/tests/common_tests/chowdsp_data_structures_test/StringLiteralTest.cpp +++ b/tests/common_tests/chowdsp_data_structures_test/StringLiteralTest.cpp @@ -2,11 +2,13 @@ #include #include +using namespace chowdsp::string_literals; + TEST_CASE ("String Literal Test", "[common][data-structures]") { SECTION ("Construction") { - static constexpr auto sl1 = chowdsp::StringLiteral { "TEST" }; + static constexpr auto sl1 = "TEST"_sl; STATIC_REQUIRE (sl1.data()[0] == 'T'); STATIC_REQUIRE (std::is_same_v, chowdsp::StringLiteral<5>>); @@ -76,4 +78,10 @@ TEST_CASE ("String Literal Test", "[common][data-structures]") const auto sl2 = sl + " BLAH!"; REQUIRE (sl2 == "BLAH BLAH!"); } + + SECTION ("Numbers") + { + static constexpr auto fifteen = 15_sl; + REQUIRE (fifteen == "15"_sl); + } }