mirror of https://github.com/AxioDL/boo.git
MIDIDecoder: Convert return value of readContinuedValue into a std::optional
Rather than use an out reference, we can convert the return value into a std::optional, combining the out reference and boolean return value into one.
This commit is contained in:
parent
64fbb923ca
commit
e48a094198
|
@ -1,6 +1,7 @@
|
||||||
#include "boo/audiodev/MIDIDecoder.hpp"
|
#include "boo/audiodev/MIDIDecoder.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
#include "boo/audiodev/IMIDIReader.hpp"
|
#include "boo/audiodev/IMIDIReader.hpp"
|
||||||
#include "lib/audiodev/MIDICommon.hpp"
|
#include "lib/audiodev/MIDICommon.hpp"
|
||||||
|
@ -10,28 +11,30 @@ namespace boo {
|
||||||
namespace {
|
namespace {
|
||||||
constexpr uint8_t clamp7(uint8_t val) { return std::max(0, std::min(127, int(val))); }
|
constexpr uint8_t clamp7(uint8_t val) { return std::max(0, std::min(127, int(val))); }
|
||||||
|
|
||||||
bool readContinuedValue(std::vector<uint8_t>::const_iterator& it, std::vector<uint8_t>::const_iterator end,
|
std::optional<uint32_t> readContinuedValue(std::vector<uint8_t>::const_iterator& it,
|
||||||
uint32_t& valOut) {
|
std::vector<uint8_t>::const_iterator end) {
|
||||||
uint8_t a = *it++;
|
uint8_t a = *it++;
|
||||||
valOut = a & 0x7f;
|
uint32_t valOut = a & 0x7f;
|
||||||
|
|
||||||
if (a & 0x80) {
|
if ((a & 0x80) != 0) {
|
||||||
if (it == end)
|
if (it == end) {
|
||||||
return false;
|
return std::nullopt;
|
||||||
|
}
|
||||||
valOut <<= 7;
|
valOut <<= 7;
|
||||||
a = *it++;
|
a = *it++;
|
||||||
valOut |= a & 0x7f;
|
valOut |= a & 0x7f;
|
||||||
|
|
||||||
if (a & 0x80) {
|
if ((a & 0x80) != 0) {
|
||||||
if (it == end)
|
if (it == end) {
|
||||||
return false;
|
return std::nullopt;
|
||||||
|
}
|
||||||
valOut <<= 7;
|
valOut <<= 7;
|
||||||
a = *it++;
|
a = *it++;
|
||||||
valOut |= a & 0x7f;
|
valOut |= a & 0x7f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return valOut;
|
||||||
}
|
}
|
||||||
} // Anonymous namespace
|
} // Anonymous namespace
|
||||||
|
|
||||||
|
@ -52,9 +55,8 @@ std::vector<uint8_t>::const_iterator MIDIDecoder::receiveBytes(std::vector<uint8
|
||||||
return begin;
|
return begin;
|
||||||
a = *it++;
|
a = *it++;
|
||||||
|
|
||||||
uint32_t length;
|
const auto length = readContinuedValue(it, end);
|
||||||
readContinuedValue(it, end, length);
|
it += *length;
|
||||||
it += length;
|
|
||||||
} else {
|
} else {
|
||||||
uint8_t chan = m_status & 0xf;
|
uint8_t chan = m_status & 0xf;
|
||||||
switch (Status(m_status & 0xf0)) {
|
switch (Status(m_status & 0xf0)) {
|
||||||
|
@ -125,10 +127,11 @@ std::vector<uint8_t>::const_iterator MIDIDecoder::receiveBytes(std::vector<uint8
|
||||||
case Status::SysEx: {
|
case Status::SysEx: {
|
||||||
switch (Status(m_status & 0xff)) {
|
switch (Status(m_status & 0xff)) {
|
||||||
case Status::SysEx: {
|
case Status::SysEx: {
|
||||||
uint32_t len;
|
const auto len = readContinuedValue(it, end);
|
||||||
if (!readContinuedValue(it, end, len) || end - it < len)
|
if (!len || end - it < *len) {
|
||||||
return begin;
|
return begin;
|
||||||
m_out.sysex(&*it, len);
|
}
|
||||||
|
m_out.sysex(&*it, *len);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Status::TimecodeQuarterFrame: {
|
case Status::TimecodeQuarterFrame: {
|
||||||
|
|
Loading…
Reference in New Issue