diff --git a/modules/juce_audio_basics/midi/ump/juce_UMPConverters.h b/modules/juce_audio_basics/midi/ump/juce_UMPConverters.h index 807d372629..e137513750 100644 --- a/modules/juce_audio_basics/midi/ump/juce_UMPConverters.h +++ b/modules/juce_audio_basics/midi/ump/juce_UMPConverters.h @@ -54,6 +54,8 @@ namespace juce::universal_midi_packets { Conversion::midi2ToMidi1DefaultTranslation (v, std::forward (fn)); } + + void reset() {} }; /** @@ -97,22 +99,24 @@ namespace juce::universal_midi_packets */ class GenericUMPConverter { - template - static void visit (This& t, Args&&... args) + using Converters = std::variant; + + template + static void visit (This& t, Fn&& fn) { - if (t.mode == PacketProtocol::MIDI_1_0) - convertImpl (std::get<0> (t.converters), std::forward (args)...); - else - convertImpl (std::get<1> (t.converters), std::forward (args)...); + if (auto* converter1 = std::get_if (&t.converters)) + fn (*converter1); + else if (auto* converter2 = std::get_if (&t.converters)) + fn (*converter2); } public: explicit GenericUMPConverter (PacketProtocol m) - : mode (m) {} + : converters (m == PacketProtocol::MIDI_1_0 ? Converters (ToUMP1Converter()) : Converters (ToUMP2Converter())) {} void reset() { - std::get<1> (converters).reset(); + visit (*this, [] (auto& c) { c.reset(); }); } template @@ -136,29 +140,44 @@ namespace juce::universal_midi_packets }); } + template + static void convertImpl (Converter& converter, const Packets& packets, Fn&& fn) + { + convertImpl (converter, packets.begin(), packets.end(), std::forward (fn)); + } + template void convert (const BytesOnGroup& m, Fn&& fn) { - visit (*this, m, std::forward (fn)); + visit (*this, [&] (auto& c) { convertImpl (c, m, std::forward (fn)); }); } template void convert (const View& v, Fn&& fn) { - visit (*this, v, std::forward (fn)); + visit (*this, [&] (auto& c) { convertImpl (c, v, std::forward (fn)); }); } template void convert (Iterator begin, Iterator end, Fn&& fn) { - visit (*this, begin, end, std::forward (fn)); + visit (*this, [&] (auto& c) { convertImpl (c, begin, end, std::forward (fn)); }); } - PacketProtocol getProtocol() const noexcept { return mode; } + template + void convert (const Packets& packets, Fn&& fn) + { + visit (*this, [&] (auto& c) { convertImpl (c, packets, std::forward (fn)); }); + } + + PacketProtocol getProtocol() const noexcept + { + return std::holds_alternative (converters) ? PacketProtocol::MIDI_1_0 + : PacketProtocol::MIDI_2_0; + } private: - std::tuple converters; - const PacketProtocol mode{}; + Converters converters; }; /**