LCOV - code coverage report
Current view: top level - src/MIDI_Interfaces - MIDI_Staller.hpp (source / functions) Hit Total Coverage
Test: ffed98f648fe78e7aa7bdd228474317d40dadbec Lines: 10 10 100.0 %
Date: 2022-05-28 15:22:59 Functions: 11 12 91.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <AH/Settings/Warnings.hpp>
       4             : #include <Settings/NamespaceSettings.hpp>
       5             : #include <AH/STL/utility> // std::forward
       6             : 
       7             : AH_DIAGNOSTIC_WERROR()
       8             : 
       9             : BEGIN_CS_NAMESPACE
      10             : 
      11             : /// Struct that can cause a MIDI_Pipe to be stalled.
      12             : struct MIDIStaller {
      13         162 :     virtual ~MIDIStaller() = default;
      14             :     /// Get the staller's name for debugging purposes.
      15           1 :     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             : AH_DIAGNOSTIC_POP()

Generated by: LCOV version 1.15