Line data Source code
1 : /* ✔ */ 2 : 3 : #pragma once 4 : 5 : #include <stdint.h> // uint8_t 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 319 : explicit constexpr Cable(uint8_t zeroBasedCable) 22 319 : : 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 411 : constexpr uint8_t getRaw() const { return zeroBasedCable; } 30 : 31 : /** 32 : * @brief Create a cable. 33 : * 34 : * @param oneBasedCable 35 : * The cable number (1 is the first cable). 36 : */ 37 : static constexpr Cable createCable(uint8_t oneBasedCable) { 38 : return Cable{uint8_t(oneBasedCable - 1)}; 39 : } 40 : 41 : /** 42 : * @brief Check if two cables are the same. 43 : * 44 : * @param rhs 45 : * The other cable to compare this cable to. 46 : */ 47 34 : constexpr bool operator==(const Cable &rhs) const { 48 34 : return this->zeroBasedCable == rhs.zeroBasedCable; 49 : } 50 : 51 : /** 52 : * @brief Add an offset. 53 : * 54 : * @param rhs 55 : * The offset to add to this cable. 56 : */ 57 : Cable &operator+=(uint8_t rhs) { 58 : this->zeroBasedCable += rhs; 59 : return *this; 60 : } 61 : 62 : /** 63 : * @brief Add an offset to a cable. 64 : * 65 : * @param rhs 66 : * The offset to add to the cable. 67 : */ 68 : Cable operator+(uint8_t rhs) const { 69 : Cable copy = *this; 70 : copy += rhs; 71 : return copy; 72 : } 73 : 74 : /** 75 : * @brief Subtract an offset. 76 : * 77 : * @param rhs 78 : * The offset to subtract from this cable. 79 : */ 80 : Cable &operator-=(uint8_t rhs) { 81 : this->zeroBasedCable -= rhs; 82 : return *this; 83 : } 84 : 85 : /** 86 : * @brief Subtract an offset from a cable. 87 : * 88 : * @param rhs 89 : * The offset to subtract from the cable. 90 : */ 91 : Cable operator-(uint8_t rhs) const { 92 : Cable copy = *this; 93 : copy -= rhs; 94 : return copy; 95 : } 96 : 97 : private: 98 : uint8_t zeroBasedCable : 4; 99 : }; 100 : 101 : /** 102 : * @brief A literal operator to create MIDI Cables. 103 : * 104 : * @param cb 105 : * The (one-based) MIDI cable (1 is the first cable). 106 : */ 107 : constexpr Cable operator"" _cb(unsigned long long cb) { 108 : return Cable::createCable(cb); 109 : } 110 : 111 : constexpr Cable CABLE_1 = 1_cb; 112 : constexpr Cable CABLE_2 = 2_cb; 113 : constexpr Cable CABLE_3 = 3_cb; 114 : constexpr Cable CABLE_4 = 4_cb; 115 : constexpr Cable CABLE_5 = 5_cb; 116 : constexpr Cable CABLE_6 = 6_cb; 117 : constexpr Cable CABLE_7 = 7_cb; 118 : constexpr Cable CABLE_8 = 8_cb; 119 : constexpr Cable CABLE_9 = 9_cb; 120 : constexpr Cable CABLE_10 = 10_cb; 121 : constexpr Cable CABLE_11 = 11_cb; 122 : constexpr Cable CABLE_12 = 12_cb; 123 : constexpr Cable CABLE_13 = 13_cb; 124 : constexpr Cable CABLE_14 = 14_cb; 125 : constexpr Cable CABLE_15 = 15_cb; 126 : constexpr Cable CABLE_16 = 16_cb; 127 : 128 : END_CS_NAMESPACE