mirror of
https://github.com/KartKrewDev/RingRacers.git
synced 2025-10-30 08:01:28 +00:00
Merge branch 'overload-visitor' into 'master'
cxx: Add srb2::Overload for using std::visit See merge request KartKrew/Kart!879
This commit is contained in:
commit
c5ecec1523
2 changed files with 17 additions and 12 deletions
|
|
@ -13,6 +13,8 @@
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#include "../cxxutil.hpp"
|
||||||
|
|
||||||
using namespace srb2;
|
using namespace srb2;
|
||||||
using srb2::audio::Wav;
|
using srb2::audio::Wav;
|
||||||
|
|
||||||
|
|
@ -130,14 +132,6 @@ std::vector<int16_t> read_int16_samples_from_stream(io::SpanStream& stream, std:
|
||||||
return samples;
|
return samples;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
|
||||||
struct OverloadVisitor : Ts... {
|
|
||||||
using Ts::operator()...;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename... Ts>
|
|
||||||
OverloadVisitor(Ts...) -> OverloadVisitor<Ts...>;
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
Wav::Wav() = default;
|
Wav::Wav() = default;
|
||||||
|
|
@ -171,7 +165,7 @@ Wav::Wav(tcb::span<std::byte> data) {
|
||||||
throw std::runtime_error("WAVE tag length exceeds stream length");
|
throw std::runtime_error("WAVE tag length exceeds stream length");
|
||||||
}
|
}
|
||||||
|
|
||||||
auto tag_visitor = OverloadVisitor {
|
auto tag_visitor = srb2::Overload {
|
||||||
[&](const FmtTag& fmt) {
|
[&](const FmtTag& fmt) {
|
||||||
if (read_fmt) {
|
if (read_fmt) {
|
||||||
throw std::runtime_error("WAVE has multiple 'fmt' tags");
|
throw std::runtime_error("WAVE has multiple 'fmt' tags");
|
||||||
|
|
@ -248,7 +242,7 @@ std::size_t read_samples(std::size_t channels,
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
std::size_t Wav::get_samples(std::size_t offset, tcb::span<audio::Sample<1>> buffer) const noexcept {
|
std::size_t Wav::get_samples(std::size_t offset, tcb::span<audio::Sample<1>> buffer) const noexcept {
|
||||||
auto samples_visitor = OverloadVisitor {
|
auto samples_visitor = srb2::Overload {
|
||||||
[&](const std::vector<uint8_t>& samples) { return read_samples<uint8_t>(channels(), offset, samples, buffer); },
|
[&](const std::vector<uint8_t>& samples) { return read_samples<uint8_t>(channels(), offset, samples, buffer); },
|
||||||
[&](const std::vector<int16_t>& samples) {
|
[&](const std::vector<int16_t>& samples) {
|
||||||
return read_samples<int16_t>(channels(), offset, samples, buffer);
|
return read_samples<int16_t>(channels(), offset, samples, buffer);
|
||||||
|
|
@ -258,7 +252,7 @@ std::size_t Wav::get_samples(std::size_t offset, tcb::span<audio::Sample<1>> buf
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t Wav::interleaved_length() const noexcept {
|
std::size_t Wav::interleaved_length() const noexcept {
|
||||||
auto samples_visitor = OverloadVisitor {[](const std::vector<uint8_t>& samples) { return samples.size(); },
|
auto samples_visitor = srb2::Overload {[](const std::vector<uint8_t>& samples) { return samples.size(); },
|
||||||
[](const std::vector<int16_t>& samples) { return samples.size(); }};
|
[](const std::vector<int16_t>& samples) { return samples.size(); }};
|
||||||
return std::visit(samples_visitor, interleaved_samples_);
|
return std::visit(samples_visitor, interleaved_samples_);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -147,6 +147,17 @@ public:
|
||||||
template <class T>
|
template <class T>
|
||||||
NotNull(T) -> NotNull<T>;
|
NotNull(T) -> NotNull<T>;
|
||||||
|
|
||||||
|
/// @brief Utility struct for combining several Callables (e.g. lambdas) into a single Callable with the call operator
|
||||||
|
/// overloaded. Use it to build a visitor for calling std::visit on variants.
|
||||||
|
/// @tparam ...Ts callable types
|
||||||
|
template <typename... Ts>
|
||||||
|
struct Overload : Ts... {
|
||||||
|
using Ts::operator()...;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename... Ts>
|
||||||
|
Overload(Ts...) -> Overload<Ts...>;
|
||||||
|
|
||||||
} // namespace srb2
|
} // namespace srb2
|
||||||
|
|
||||||
#endif // __SRB2_CXXUTIL_HPP__
|
#endif // __SRB2_CXXUTIL_HPP__
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue