Line data Source code
1 : #pragma once 2 : 3 : #include <Settings/SettingsWrapper.hpp> 4 : #if !DISABLE_PIPES 5 : 6 : #include <AH/STL/utility> // std::forward 7 : #include <Settings/NamespaceSettings.hpp> 8 : 9 : BEGIN_CS_NAMESPACE 10 : 11 : /// Struct that can cause a MIDI_Pipe to be stalled. 12 : struct MIDIStaller { 13 164 : virtual ~MIDIStaller() = default; 14 : /// Get the staller's name for debugging purposes. 15 2 : virtual const char *getName() const { return "<?>"; } 16 : /// Call back that should finish any MIDI messages that are in progress, and 17 : /// un-stall the pipe or MIDI source as quickly as possible. 18 : virtual void handleStall() = 0; 19 : 20 : /// Get the staller's name for debugging purposes. Correctly deals with 21 : /// null pointers or eternal stallers. 22 : static const char *getNameNull(MIDIStaller *s); 23 : }; 24 : 25 : /// Allocate a MIDIStaller that executes the given callback and deletes itself 26 : /// when @ref MIDIStaller::handleStall is called. 27 : /// @note Don't lose the pointer! If you never call `handleStall`, the memory 28 : /// won't be deallocated. 29 : template <class Callback> 30 3 : auto makeMIDIStaller(Callback &&callback) -> MIDIStaller * { 31 : 32 : struct AutoCleanupMIDIStaller : MIDIStaller { 33 3 : AutoCleanupMIDIStaller(Callback &&callback) 34 3 : : callback(std::forward<Callback>(callback)) {} 35 : 36 3 : void handleStall() override { 37 3 : callback(this); 38 3 : delete this; 39 3 : } 40 : 41 : Callback callback; 42 : }; 43 3 : return new AutoCleanupMIDIStaller(std::forward<Callback>(callback)); 44 : } 45 : 46 : END_CS_NAMESPACE 47 : 48 : #else 49 : 50 : BEGIN_CS_NAMESPACE 51 : 52 : struct MIDIStaller {}; 53 : 54 : END_CS_NAMESPACE 55 : 56 : #endif