From 6cc160b9ff65925552624ed119d826504efd890f Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Thu, 13 Aug 2020 18:38:13 +0200 Subject: [PATCH 1/2] Move template functions of Opcode.h to implementation side --- src/sfizz/Opcode.cpp | 148 +++++++++++++++++++++++++++++++++++++++++-- src/sfizz/Opcode.h | 104 +++--------------------------- 2 files changed, 151 insertions(+), 101 deletions(-) diff --git a/src/sfizz/Opcode.cpp b/src/sfizz/Opcode.cpp index 52b3ec66..6ec196a2 100644 --- a/src/sfizz/Opcode.cpp +++ b/src/sfizz/Opcode.cpp @@ -13,7 +13,9 @@ #include #include -sfz::Opcode::Opcode(absl::string_view inputOpcode, absl::string_view inputValue) +namespace sfz { + +Opcode::Opcode(absl::string_view inputOpcode, absl::string_view inputValue) : opcode(trim(inputOpcode)) , value(trim(inputValue)) , category(identifyCategory(inputOpcode)) @@ -49,7 +51,7 @@ static absl::string_view extractBackInteger(absl::string_view opcodeName) return opcodeName.substr(i); } -std::string sfz::Opcode::getDerivedName(sfz::OpcodeCategory newCategory, unsigned number) const +std::string Opcode::getDerivedName(OpcodeCategory newCategory, unsigned number) const { std::string derivedName(opcode); @@ -95,9 +97,9 @@ std::string sfz::Opcode::getDerivedName(sfz::OpcodeCategory newCategory, unsigne return derivedName; } -sfz::OpcodeCategory sfz::Opcode::identifyCategory(absl::string_view name) +OpcodeCategory Opcode::identifyCategory(absl::string_view name) { - sfz::OpcodeCategory category = kOpcodeNormal; + OpcodeCategory category = kOpcodeNormal; if (!name.empty() && absl::ascii_isdigit(name.back())) { absl::string_view part = name; @@ -115,7 +117,7 @@ sfz::OpcodeCategory sfz::Opcode::identifyCategory(absl::string_view name) return category; } -absl::optional sfz::readNoteValue(absl::string_view value) +absl::optional readNoteValue(absl::string_view value) { char noteLetter = absl::ascii_tolower(value.empty() ? '\0' : value.front()); value.remove_prefix(1); @@ -156,6 +158,142 @@ absl::optional sfz::readNoteValue(absl::string_view value) return static_cast(noteNumber); } +/// +template ::value, int>> +absl::optional readOpcode(absl::string_view value, const Range& validRange) +{ + size_t numberEnd = 0; + + if (numberEnd < value.size() && (value[numberEnd] == '+' || value[numberEnd] == '-')) + ++numberEnd; + while (numberEnd < value.size() && absl::ascii_isdigit(value[numberEnd])) + ++numberEnd; + + value = value.substr(0, numberEnd); + + int64_t returnedValue; + if (!absl::SimpleAtoi(value, &returnedValue)) + return absl::nullopt; + + if (returnedValue > std::numeric_limits::max()) + returnedValue = std::numeric_limits::max(); + if (returnedValue < std::numeric_limits::min()) + returnedValue = std::numeric_limits::min(); + + return validRange.clamp(static_cast(returnedValue)); +} + +template ::value, int>> +absl::optional readOpcode(absl::string_view value, const Range& validRange) +{ + size_t numberEnd = 0; + + if (numberEnd < value.size() && (value[numberEnd] == '+' || value[numberEnd] == '-')) + ++numberEnd; + while (numberEnd < value.size() && absl::ascii_isdigit(value[numberEnd])) + ++numberEnd; + + if (numberEnd < value.size() && value[numberEnd] == '.') { + ++numberEnd; + while (numberEnd < value.size() && absl::ascii_isdigit(value[numberEnd])) + ++numberEnd; + } + + value = value.substr(0, numberEnd); + + float returnedValue; + if (!absl::SimpleAtof(value, &returnedValue)) + return absl::nullopt; + + return validRange.clamp(returnedValue); +} + +absl::optional readBooleanFromOpcode(const Opcode& opcode) +{ + switch (hash(opcode.value)) { + case hash("off"): + return false; + case hash("on"): + return true; + default: + return {}; + } +} + +template +void setValueFromOpcode(const Opcode& opcode, ValueType& target, const Range& validRange) +{ + auto value = readOpcode(opcode.value, validRange); + if (!value) // Try and read a note rather than a number + value = readNoteValue(opcode.value); + if (value) + target = *value; +} + +template +inline void setValueFromOpcode(const Opcode& opcode, absl::optional& target, const Range& validRange) +{ + auto value = readOpcode(opcode.value, validRange); + if (!value) // Try and read a note rather than a number + value = readNoteValue(opcode.value); + if (value) + target = *value; +} + +template +void setRangeEndFromOpcode(const Opcode& opcode, Range& target, const Range& validRange) +{ + auto value = readOpcode(opcode.value, validRange); + if (!value) // Try and read a note rather than a number + value = readNoteValue(opcode.value); + if (value) + target.setEnd(*value); +} + +template +void setRangeStartFromOpcode(const Opcode& opcode, Range& target, const Range& validRange) +{ + auto value = readOpcode(opcode.value, validRange); + if (!value) // Try and read a note rather than a number + value = readNoteValue(opcode.value); + if (value) + target.setStart(*value); +} + +template +void setCCPairFromOpcode(const Opcode& opcode, absl::optional>& target, const Range& validRange) +{ + auto value = readOpcode(opcode.value, validRange); + if (value && Default::ccNumberRange.containsWithEnd(opcode.parameters.back())) + target = { opcode.parameters.back(), *value }; + else + target = {}; +} + +/// +#define INSTANCIATE_FOR(T) \ + template absl::optional readOpcode(absl::string_view value, const Range& validRange); \ + template void setValueFromOpcode(const Opcode& opcode, T& target, const Range& validRange); \ + template void setValueFromOpcode(const Opcode& opcode, absl::optional& target, const Range& validRange); \ + template void setRangeEndFromOpcode(const Opcode& opcode, Range& target, const Range& validRange); \ + template void setRangeStartFromOpcode(const Opcode& opcode, Range& target, const Range& validRange); \ + template void setCCPairFromOpcode(const Opcode& opcode, absl::optional>& target, const Range& validRange); + +INSTANCIATE_FOR(float) +INSTANCIATE_FOR(double) +INSTANCIATE_FOR(int8_t) +INSTANCIATE_FOR(int16_t) +INSTANCIATE_FOR(int32_t) +INSTANCIATE_FOR(int64_t) +INSTANCIATE_FOR(uint8_t) +INSTANCIATE_FOR(uint16_t) +INSTANCIATE_FOR(uint32_t) +//INSTANCIATE_FOR(uint64_t) + +#undef INSTANCIATE_FOR + +} // namespace sfz + std::ostream &operator<<(std::ostream &os, const sfz::Opcode &opcode) { return os << opcode.opcode << '=' << '"' << opcode.value << '"'; diff --git a/src/sfizz/Opcode.h b/src/sfizz/Opcode.h index 4d15f844..a3132499 100644 --- a/src/sfizz/Opcode.h +++ b/src/sfizz/Opcode.h @@ -125,28 +125,7 @@ absl::optional readNoteValue(absl::string_view value); * @return absl::optional the cast value, or null */ template ::value, int> = 0> -inline absl::optional readOpcode(absl::string_view value, const Range& validRange) -{ - size_t numberEnd = 0; - - if (numberEnd < value.size() && (value[numberEnd] == '+' || value[numberEnd] == '-')) - ++numberEnd; - while (numberEnd < value.size() && absl::ascii_isdigit(value[numberEnd])) - ++numberEnd; - - value = value.substr(0, numberEnd); - - int64_t returnedValue; - if (!absl::SimpleAtoi(value, &returnedValue)) - return absl::nullopt; - - if (returnedValue > std::numeric_limits::max()) - returnedValue = std::numeric_limits::max(); - if (returnedValue < std::numeric_limits::min()) - returnedValue = std::numeric_limits::min(); - - return validRange.clamp(static_cast(returnedValue)); -} +absl::optional readOpcode(absl::string_view value, const Range& validRange); /** * @brief Read a value from the sfz file and cast it to the destination parameter along @@ -159,44 +138,12 @@ inline absl::optional readOpcode(absl::string_view value, const Range * @return absl::optional the cast value, or null */ template ::value, int> = 0> -inline absl::optional readOpcode(absl::string_view value, const Range& validRange) -{ - size_t numberEnd = 0; - - if (numberEnd < value.size() && (value[numberEnd] == '+' || value[numberEnd] == '-')) - ++numberEnd; - while (numberEnd < value.size() && absl::ascii_isdigit(value[numberEnd])) - ++numberEnd; - - if (numberEnd < value.size() && value[numberEnd] == '.') { - ++numberEnd; - while (numberEnd < value.size() && absl::ascii_isdigit(value[numberEnd])) - ++numberEnd; - } - - value = value.substr(0, numberEnd); - - float returnedValue; - if (!absl::SimpleAtof(value, &returnedValue)) - return absl::nullopt; - - return validRange.clamp(returnedValue); -} +absl::optional readOpcode(absl::string_view value, const Range& validRange); /** * @brief Read a boolean value from the sfz file and cast it to the destination parameter. */ -inline absl::optional readBooleanFromOpcode(const Opcode& opcode) -{ - switch (hash(opcode.value)) { - case hash("off"): - return false; - case hash("on"): - return true; - default: - return {}; - } -} +absl::optional readBooleanFromOpcode(const Opcode& opcode); /** * @brief Set a target parameter from an opcode value, with possibly a textual note rather @@ -208,14 +155,7 @@ inline absl::optional readBooleanFromOpcode(const Opcode& opcode) * @param validRange the range of admitted values used to clamp the opcode */ template -inline void setValueFromOpcode(const Opcode& opcode, ValueType& target, const Range& validRange) -{ - auto value = readOpcode(opcode.value, validRange); - if (!value) // Try and read a note rather than a number - value = readNoteValue(opcode.value); - if (value) - target = *value; -} +void setValueFromOpcode(const Opcode& opcode, ValueType& target, const Range& validRange); /** * @brief Set a target parameter from an opcode value, with possibly a textual note rather @@ -227,14 +167,7 @@ inline void setValueFromOpcode(const Opcode& opcode, ValueType& target, const Ra * @param validRange the range of admitted values used to clamp the opcode */ template -inline void setValueFromOpcode(const Opcode& opcode, absl::optional& target, const Range& validRange) -{ - auto value = readOpcode(opcode.value, validRange); - if (!value) // Try and read a note rather than a number - value = readNoteValue(opcode.value); - if (value) - target = *value; -} +void setValueFromOpcode(const Opcode& opcode, absl::optional& target, const Range& validRange); /** * @brief Set a target end of a range from an opcode value, with possibly a textual note rather @@ -246,14 +179,7 @@ inline void setValueFromOpcode(const Opcode& opcode, absl::optional& * @param validRange the range of admitted values used to clamp the opcode */ template -inline void setRangeEndFromOpcode(const Opcode& opcode, Range& target, const Range& validRange) -{ - auto value = readOpcode(opcode.value, validRange); - if (!value) // Try and read a note rather than a number - value = readNoteValue(opcode.value); - if (value) - target.setEnd(*value); -} +void setRangeEndFromOpcode(const Opcode& opcode, Range& target, const Range& validRange); /** * @brief Set a target beginning of a range from an opcode value, with possibly a textual note rather @@ -265,14 +191,7 @@ inline void setRangeEndFromOpcode(const Opcode& opcode, Range& target * @param validRange the range of admitted values used to clamp the opcode */ template -inline void setRangeStartFromOpcode(const Opcode& opcode, Range& target, const Range& validRange) -{ - auto value = readOpcode(opcode.value, validRange); - if (!value) // Try and read a note rather than a number - value = readNoteValue(opcode.value); - if (value) - target.setStart(*value); -} +void setRangeStartFromOpcode(const Opcode& opcode, Range& target, const Range& validRange); /** * @brief Set a CC modulation parameter from an opcode value. @@ -283,14 +202,7 @@ inline void setRangeStartFromOpcode(const Opcode& opcode, Range& targ * @param validRange the range of admitted values used to clamp the opcode */ template -inline void setCCPairFromOpcode(const Opcode& opcode, absl::optional>& target, const Range& validRange) -{ - auto value = readOpcode(opcode.value, validRange); - if (value && Default::ccNumberRange.containsWithEnd(opcode.parameters.back())) - target = { opcode.parameters.back(), *value }; - else - target = {}; -} +void setCCPairFromOpcode(const Opcode& opcode, absl::optional>& target, const Range& validRange); } From 0d6195a42e1e5ce0d59ad41333bd5511a885ff57 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Thu, 13 Aug 2020 18:51:31 +0200 Subject: [PATCH 2/2] No lint for bugprone-macro-parentheses --- src/sfizz/Opcode.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/sfizz/Opcode.cpp b/src/sfizz/Opcode.cpp index 6ec196a2..1ffafdbf 100644 --- a/src/sfizz/Opcode.cpp +++ b/src/sfizz/Opcode.cpp @@ -272,12 +272,12 @@ void setCCPairFromOpcode(const Opcode& opcode, absl::optional> /// #define INSTANCIATE_FOR(T) \ - template absl::optional readOpcode(absl::string_view value, const Range& validRange); \ - template void setValueFromOpcode(const Opcode& opcode, T& target, const Range& validRange); \ - template void setValueFromOpcode(const Opcode& opcode, absl::optional& target, const Range& validRange); \ - template void setRangeEndFromOpcode(const Opcode& opcode, Range& target, const Range& validRange); \ - template void setRangeStartFromOpcode(const Opcode& opcode, Range& target, const Range& validRange); \ - template void setCCPairFromOpcode(const Opcode& opcode, absl::optional>& target, const Range& validRange); + template absl::optional readOpcode(absl::string_view value, const Range& validRange); /*NOLINT(bugprone-macro-parentheses)*/ \ + template void setValueFromOpcode(const Opcode& opcode, T& target, const Range& validRange); /*NOLINT(bugprone-macro-parentheses)*/ \ + template void setValueFromOpcode(const Opcode& opcode, absl::optional& target, const Range& validRange); /*NOLINT(bugprone-macro-parentheses)*/ \ + template void setRangeEndFromOpcode(const Opcode& opcode, Range& target, const Range& validRange); /*NOLINT(bugprone-macro-parentheses)*/ \ + template void setRangeStartFromOpcode(const Opcode& opcode, Range& target, const Range& validRange); /*NOLINT(bugprone-macro-parentheses)*/ \ + template void setCCPairFromOpcode(const Opcode& opcode, absl::optional>& target, const Range& validRange); /*NOLINT(bugprone-macro-parentheses)*/ INSTANCIATE_FOR(float) INSTANCIATE_FOR(double)