Skip to content

Commit

Permalink
Add user-defined literals for chowdsp::StringLiteral
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinchowdhury18 committed Nov 7, 2023
1 parent 16c145a commit dd2ff95
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@ namespace sl_detail

/** A string-literal wrapper type. */
template <size_t N>
class StringLiteral
struct StringLiteral
{
std::array<char, N> chars {};
size_t actual_size = 0;

template <size_t NN, size_t MM>
friend constexpr StringLiteral<NN + MM> operator+ (const StringLiteral<NN>&, const StringLiteral<MM>&);

public:
constexpr StringLiteral() = default;
constexpr StringLiteral (const StringLiteral&) = default;
constexpr StringLiteral& operator= (const StringLiteral&) = default;
Expand All @@ -48,7 +44,7 @@ class StringLiteral
}

constexpr StringLiteral (const char (&str)[N]) // NOSONAR NOLINT(google-explicit-constructor)
: StringLiteral (str, std::integral_constant<size_t, N - 1> {})
: StringLiteral (str, std::integral_constant<size_t, N-1> {})
{
}

Expand Down Expand Up @@ -151,6 +147,24 @@ constexpr bool operator!= (const std::string_view& lhs, const StringLiteral<N>&
{
return ! (lhs == rhs);
}

#if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
namespace string_literals
{
template <StringLiteral sl>
constexpr auto operator"" _sl()
{
return sl;
}

template <char... str>
constexpr auto operator"" _sl()
{
constexpr char str_array[] { str..., '\0' };
return StringLiteral { str_array };
}
}
#endif
} // namespace chowdsp

JUCE_END_IGNORE_WARNINGS_MSVC
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ TEST_CASE ("Arena Allocator Test", "[common][data-structures]")
// aligned allocation
{
auto* some_data = allocator.allocate<float> (1, 16);
REQUIRE (juce::snapPointerToAlignment (some_data, 16) == some_data);
REQUIRE (juce::snapPointerToAlignment (some_data, (size_t) 16) == some_data);
}

// overfull allocation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,17 @@ TEST_CASE ("Zip-Multi")
SECTION ("Filling Arrays")
{
const std::array<int, 6> x_int { 0, 1, 2, 3, 4, 5 };
std::array<float, 6> x_float;
std::array<double, 6> x_double;
std::array<float, 6> x_float {};
std::array<double, 6> x_double {};
for (auto [int_val, float_val, double_val] : chowdsp::zip_multi (x_int, x_float, x_double))
{
float_val = static_cast<float> (int_val);
double_val = static_cast<double> (int_val);
}
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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
#include <CatchUtils.h>
#include <chowdsp_data_structures/chowdsp_data_structures.h>

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<std::remove_cv_t<decltype (sl1)>, chowdsp::StringLiteral<5>>);

Expand Down Expand Up @@ -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);
}
}

0 comments on commit dd2ff95

Please sign in to comment.