mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
GenericUMPConverter: Implement using variant
This commit is contained in:
parent
71b1d001de
commit
444eb0c85e
1 changed files with 33 additions and 14 deletions
|
|
@ -54,6 +54,8 @@ namespace juce::universal_midi_packets
|
||||||
{
|
{
|
||||||
Conversion::midi2ToMidi1DefaultTranslation (v, std::forward<Fn> (fn));
|
Conversion::midi2ToMidi1DefaultTranslation (v, std::forward<Fn> (fn));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void reset() {}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -97,22 +99,24 @@ namespace juce::universal_midi_packets
|
||||||
*/
|
*/
|
||||||
class GenericUMPConverter
|
class GenericUMPConverter
|
||||||
{
|
{
|
||||||
template <typename This, typename... Args>
|
using Converters = std::variant<ToUMP1Converter, ToUMP2Converter>;
|
||||||
static void visit (This& t, Args&&... args)
|
|
||||||
|
template <typename This, typename Fn>
|
||||||
|
static void visit (This& t, Fn&& fn)
|
||||||
{
|
{
|
||||||
if (t.mode == PacketProtocol::MIDI_1_0)
|
if (auto* converter1 = std::get_if<ToUMP1Converter> (&t.converters))
|
||||||
convertImpl (std::get<0> (t.converters), std::forward<Args> (args)...);
|
fn (*converter1);
|
||||||
else
|
else if (auto* converter2 = std::get_if<ToUMP2Converter> (&t.converters))
|
||||||
convertImpl (std::get<1> (t.converters), std::forward<Args> (args)...);
|
fn (*converter2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit GenericUMPConverter (PacketProtocol m)
|
explicit GenericUMPConverter (PacketProtocol m)
|
||||||
: mode (m) {}
|
: converters (m == PacketProtocol::MIDI_1_0 ? Converters (ToUMP1Converter()) : Converters (ToUMP2Converter())) {}
|
||||||
|
|
||||||
void reset()
|
void reset()
|
||||||
{
|
{
|
||||||
std::get<1> (converters).reset();
|
visit (*this, [] (auto& c) { c.reset(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Converter, typename Fn>
|
template <typename Converter, typename Fn>
|
||||||
|
|
@ -136,29 +140,44 @@ namespace juce::universal_midi_packets
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename Converter, typename Fn>
|
||||||
|
static void convertImpl (Converter& converter, const Packets& packets, Fn&& fn)
|
||||||
|
{
|
||||||
|
convertImpl (converter, packets.begin(), packets.end(), std::forward<Fn> (fn));
|
||||||
|
}
|
||||||
|
|
||||||
template <typename Fn>
|
template <typename Fn>
|
||||||
void convert (const BytesOnGroup& m, Fn&& fn)
|
void convert (const BytesOnGroup& m, Fn&& fn)
|
||||||
{
|
{
|
||||||
visit (*this, m, std::forward<Fn> (fn));
|
visit (*this, [&] (auto& c) { convertImpl (c, m, std::forward<Fn> (fn)); });
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Fn>
|
template <typename Fn>
|
||||||
void convert (const View& v, Fn&& fn)
|
void convert (const View& v, Fn&& fn)
|
||||||
{
|
{
|
||||||
visit (*this, v, std::forward<Fn> (fn));
|
visit (*this, [&] (auto& c) { convertImpl (c, v, std::forward<Fn> (fn)); });
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Fn>
|
template <typename Fn>
|
||||||
void convert (Iterator begin, Iterator end, Fn&& fn)
|
void convert (Iterator begin, Iterator end, Fn&& fn)
|
||||||
{
|
{
|
||||||
visit (*this, begin, end, std::forward<Fn> (fn));
|
visit (*this, [&] (auto& c) { convertImpl (c, begin, end, std::forward<Fn> (fn)); });
|
||||||
}
|
}
|
||||||
|
|
||||||
PacketProtocol getProtocol() const noexcept { return mode; }
|
template <typename Fn>
|
||||||
|
void convert (const Packets& packets, Fn&& fn)
|
||||||
|
{
|
||||||
|
visit (*this, [&] (auto& c) { convertImpl (c, packets, std::forward<Fn> (fn)); });
|
||||||
|
}
|
||||||
|
|
||||||
|
PacketProtocol getProtocol() const noexcept
|
||||||
|
{
|
||||||
|
return std::holds_alternative<ToUMP1Converter> (converters) ? PacketProtocol::MIDI_1_0
|
||||||
|
: PacketProtocol::MIDI_2_0;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::tuple<ToUMP1Converter, ToUMP2Converter> converters;
|
Converters converters;
|
||||||
const PacketProtocol mode{};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue