Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user-defined literals for chowdsp::StringLiteral #465

Merged
merged 8 commits into from
Nov 7, 2023
6 changes: 3 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ jobs:
"Test",
"Coverage",
]
os: [ubuntu-latest, windows-2019, macos-latest]
os: [ubuntu-latest, windows-2022, macos-latest]
include:
- name: "Test"
os: ubuntu-latest
cmake_args: "-DCMAKE_BUILD_TYPE=Release"
build_type: "Release"
- name: "Test"
os: windows-2019
os: windows-2022
build_type: "Release"
- name: "Coverage"
os: ubuntu-latest
Expand All @@ -61,7 +61,7 @@ jobs:
- name: "Test"
os: macos-latest
- name: "Coverage"
os: windows-2019
os: windows-2022

steps:
- name: Install Linux Deps
Expand Down
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 Down Expand Up @@ -151,6 +147,25 @@ 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
{
// MSVC doesn't support this yet :(
// 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 };
}
} // namespace string_literals
#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,6 +2,8 @@
#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")
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 == chowdsp::StringLiteral { "15" });
}
}
2 changes: 1 addition & 1 deletion tests/dsp_tests/chowdsp_dsp_juce_test/DiffuserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ TEST_CASE ("Diffuser Test", "[dsp][reverb]")
{
static constexpr int nChannels = 4;
static constexpr int nStages = 4;
using TestDiffuser = chowdsp::Reverb::Diffuser<float, nChannels, chowdsp::DelayLineInterpolationTypes::None, 1 << 13>;
using TestDiffuser = chowdsp::Reverb::Diffuser<float, nChannels, chowdsp::DelayLineInterpolationTypes::None, 1 << 10>;

SECTION ("Energy Preserving Test")
{
Expand Down
Loading