LCOV - code coverage report
Current view: top level - src/Def - Cable.hpp (source / functions) Hit Total Coverage
Test: ffed98f648fe78e7aa7bdd228474317d40dadbec Lines: 8 8 100.0 %
Date: 2022-05-28 15:22:59 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* ✔ */
       2             : 
       3             : #pragma once
       4             : 
       5             : #include <AH/Arduino-Wrapper.h> // Print
       6             : #include <Settings/NamespaceSettings.hpp>
       7             : 
       8             : BEGIN_CS_NAMESPACE
       9             : 
      10             : /** 
      11             :  * A type-safe class for MIDI USB Cable numbers.
      12             :  */
      13             : class Cable {
      14             :   public:
      15             :     /**
      16             :      * @brief   Create a MIDI Cable object.
      17             :      * 
      18             :      * @param   zeroBasedCable
      19             :      *          The zero-based cable (0 is the first cable).
      20             :      */
      21        2543 :     explicit constexpr Cable(uint8_t zeroBasedCable)
      22        2543 :         : zeroBasedCable(zeroBasedCable) {}
      23             : 
      24             :     /**
      25             :      * @brief   Get the cable as an integer.
      26             :      * 
      27             :      * @return  The zero-based cable (0 is the first cable).
      28             :      */
      29        3087 :     constexpr uint8_t getRaw() const { return zeroBasedCable; }
      30             : 
      31             :     /**
      32             :      * @brief   Get the cable as an integer.
      33             :      * 
      34             :      * @return  The one-based cable (1 is the first cable).
      35             :      */
      36          17 :     constexpr uint8_t getOneBased() const { return zeroBasedCable + 1; }
      37             : 
      38             :     /**
      39             :      * @brief   Create a cable.
      40             :      * 
      41             :      * @param   oneBasedCable
      42             :      *          The cable number (1 is the first cable).
      43             :      */
      44             :     static constexpr Cable createCable(uint8_t oneBasedCable) {
      45             :         return Cable{uint8_t(oneBasedCable - 1)};
      46             :     }
      47             : 
      48             :     /**
      49             :      * @brief   Check if two cables are the same.
      50             :      * 
      51             :      * @param   rhs
      52             :      *          The other cable to compare this cable to.
      53             :      */
      54         300 :     constexpr bool operator==(const Cable &rhs) const {
      55         300 :         return this->zeroBasedCable == rhs.zeroBasedCable;
      56             :     }
      57             : 
      58             :     /**
      59             :      * @brief   Check if two cables are the different.
      60             :      * 
      61             :      * @param   rhs
      62             :      *          The other cable to compare this cable to.
      63             :      */
      64          32 :     constexpr bool operator!=(const Cable &rhs) const {
      65          32 :         return this->zeroBasedCable != rhs.zeroBasedCable;
      66             :     }
      67             : 
      68             :     /**
      69             :      * @brief   Add an offset.
      70             :      * 
      71             :      * @param   rhs
      72             :      *          The offset to add to this cable.
      73             :      */
      74             :     Cable &operator+=(uint8_t rhs) {
      75             :         this->zeroBasedCable += rhs;
      76             :         return *this;
      77             :     }
      78             : 
      79             :     /**
      80             :      * @brief   Add an offset to a cable.
      81             :      * 
      82             :      * @param   rhs
      83             :      *          The offset to add to the cable.
      84             :      */
      85             :     Cable operator+(uint8_t rhs) const {
      86             :         Cable copy = *this;
      87             :         copy += rhs;
      88             :         return copy;
      89             :     }
      90             : 
      91             :     /**
      92             :      * @brief   Subtract an offset.
      93             :      * 
      94             :      * @param   rhs
      95             :      *          The offset to subtract from this cable.
      96             :      */
      97             :     Cable &operator-=(uint8_t rhs) {
      98             :         this->zeroBasedCable -= rhs;
      99             :         return *this;
     100             :     }
     101             : 
     102             :     /**
     103             :      * @brief   Subtract an offset from a cable.
     104             :      * 
     105             :      * @param   rhs
     106             :      *          The offset to subtract from the cable.
     107             :      */
     108             :     Cable operator-(uint8_t rhs) const {
     109             :         Cable copy = *this;
     110             :         copy -= rhs;
     111             :         return copy;
     112             :     }
     113             : 
     114             :   private:
     115             :     uint8_t zeroBasedCable : 4;
     116             : };
     117             : 
     118             : constexpr Cable CABLE_1 = Cable::createCable(1);
     119             : constexpr Cable CABLE_2 = Cable::createCable(2);
     120             : constexpr Cable CABLE_3 = Cable::createCable(3);
     121             : constexpr Cable CABLE_4 = Cable::createCable(4);
     122             : constexpr Cable CABLE_5 = Cable::createCable(5);
     123             : constexpr Cable CABLE_6 = Cable::createCable(6);
     124             : constexpr Cable CABLE_7 = Cable::createCable(7);
     125             : constexpr Cable CABLE_8 = Cable::createCable(8);
     126             : constexpr Cable CABLE_9 = Cable::createCable(9);
     127             : constexpr Cable CABLE_10 = Cable::createCable(10);
     128             : constexpr Cable CABLE_11 = Cable::createCable(11);
     129             : constexpr Cable CABLE_12 = Cable::createCable(12);
     130             : constexpr Cable CABLE_13 = Cable::createCable(13);
     131             : constexpr Cable CABLE_14 = Cable::createCable(14);
     132             : constexpr Cable CABLE_15 = Cable::createCable(15);
     133             : constexpr Cable CABLE_16 = Cable::createCable(16);
     134             : 
     135             : Print &operator<<(Print &, Cable);
     136             : 
     137             : END_CS_NAMESPACE

Generated by: LCOV version 1.15