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