From: <sv...@op...> - 2024-12-19 20:03:28
|
Author: sagamusix Date: Thu Dec 19 21:03:22 2024 New Revision: 22580 URL: https://source.openmpt.org/browse/openmpt/?op=revision&rev=22580 Log: [Ref] Make ModInstrument constexpr-constructable. This will allow an instrument extension writer implementation without a global constant ModInstrument object once C++17 support is dropped. Modified: trunk/OpenMPT/soundlib/ModInstrument.cpp trunk/OpenMPT/soundlib/ModInstrument.h trunk/OpenMPT/src/mpt/string/buffer.hpp Modified: trunk/OpenMPT/soundlib/ModInstrument.cpp ============================================================================== --- trunk/OpenMPT/soundlib/ModInstrument.cpp Thu Dec 19 19:40:32 2024 (r22579) +++ trunk/OpenMPT/soundlib/ModInstrument.cpp Thu Dec 19 21:03:22 2024 (r22580) @@ -152,18 +152,6 @@ } -ModInstrument::ModInstrument(SAMPLEINDEX sample) -{ - SetCutoff(0, false); - SetResonance(0, false); - - pitchToTempoLock.Set(0); - - AssignSample(sample); - ResetNoteMap(); -} - - // Translate instrument properties between two given formats. void ModInstrument::Convert(MODTYPE fromType, MODTYPE toType) { Modified: trunk/OpenMPT/soundlib/ModInstrument.h ============================================================================== --- trunk/OpenMPT/soundlib/ModInstrument.h Thu Dec 19 19:40:32 2024 (r22579) +++ trunk/OpenMPT/soundlib/ModInstrument.h Thu Dec 19 21:03:22 2024 (r22580) @@ -35,8 +35,8 @@ tick_t tick = 0; // Envelope node position (x axis) value_t value = 0; // Envelope node value (y axis) - EnvelopeNode() { } - EnvelopeNode(tick_t tick, value_t value) : tick(tick), value(value) { } + constexpr EnvelopeNode() = default; + constexpr EnvelopeNode(tick_t tick, value_t value) : tick{tick}, value{value} { } bool operator== (const EnvelopeNode &other) const { return tick == other.tick && value == other.value; } }; @@ -128,16 +128,20 @@ // WHEN adding new members here, ALSO update InstrumentExtensions.cpp // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - explicit ModInstrument(SAMPLEINDEX sample = 0); + MPT_CONSTEXPR20_FUN explicit ModInstrument(SAMPLEINDEX sample = 0) + { + AssignSample(sample); + ResetNoteMap(); + } // Assign all notes to a given sample. - void AssignSample(SAMPLEINDEX sample) + MPT_CONSTEXPR20_FUN void AssignSample(SAMPLEINDEX sample) { Keyboard.fill(sample); } // Reset note mapping (i.e. every note is mapped to itself) - void ResetNoteMap() + MPT_CONSTEXPR20_FUN void ResetNoteMap() { std::iota(NoteMap.begin(), NoteMap.end(), static_cast<uint8>(NOTE_MIN)); } @@ -149,23 +153,23 @@ // Transpose entire note mapping by given number of semitones void Transpose(int8 amount); - bool IsCutoffEnabled() const { return (nIFC & 0x80) != 0; } - bool IsResonanceEnabled() const { return (nIFR & 0x80) != 0; } - uint8 GetCutoff() const { return (nIFC & 0x7F); } - uint8 GetResonance() const { return (nIFR & 0x7F); } - void SetCutoff(uint8 cutoff, bool enable) { nIFC = std::min(cutoff, uint8(0x7F)) | (enable ? 0x80 : 0x00); } - void SetResonance(uint8 resonance, bool enable) { nIFR = std::min(resonance, uint8(0x7F)) | (enable ? 0x80 : 0x00); } + MPT_CONSTEXPRINLINE bool IsCutoffEnabled() const { return (nIFC & 0x80) != 0; } + MPT_CONSTEXPRINLINE bool IsResonanceEnabled() const { return (nIFR & 0x80) != 0; } + MPT_CONSTEXPRINLINE uint8 GetCutoff() const { return (nIFC & 0x7F); } + MPT_CONSTEXPRINLINE uint8 GetResonance() const { return (nIFR & 0x7F); } + MPT_CONSTEXPRINLINE void SetCutoff(uint8 cutoff, bool enable) { nIFC = std::min(cutoff, uint8(0x7F)) | (enable ? 0x80 : 0x00); } + MPT_CONSTEXPRINLINE void SetResonance(uint8 resonance, bool enable) { nIFR = std::min(resonance, uint8(0x7F)) | (enable ? 0x80 : 0x00); } - bool HasValidMIDIChannel() const { return (nMidiChannel >= 1 && nMidiChannel <= 17); } + MPT_CONSTEXPRINLINE bool HasValidMIDIChannel() const { return (nMidiChannel >= 1 && nMidiChannel <= 17); } uint8 GetMIDIChannel(const ModChannel &channel, CHANNELINDEX chn) const; - void SetTuning(CTuning *pT) + MPT_CONSTEXPRINLINE void SetTuning(CTuning *pT) { pTuning = pT; } // Get a reference to a specific envelope of this instrument - const InstrumentEnvelope &GetEnvelope(EnvelopeType envType) const + MPT_CONSTEXPRINLINE const InstrumentEnvelope &GetEnvelope(EnvelopeType envType) const { switch(envType) { Modified: trunk/OpenMPT/src/mpt/string/buffer.hpp ============================================================================== --- trunk/OpenMPT/src/mpt/string/buffer.hpp Thu Dec 19 19:40:32 2024 (r22579) +++ trunk/OpenMPT/src/mpt/string/buffer.hpp Thu Dec 19 21:03:22 2024 (r22580) @@ -310,13 +310,13 @@ Tchar buf[len]; public: - charbuf() { + MPT_CONSTEXPR20_FUN charbuf() { std::fill(std::begin(buf), std::end(buf), char_constants<Tchar>::null); } - charbuf(const charbuf &) = default; - charbuf(charbuf &&) = default; - charbuf & operator=(const charbuf &) = default; - charbuf & operator=(charbuf &&) = default; + constexpr charbuf(const charbuf &) = default; + constexpr charbuf(charbuf &&) = default; + constexpr charbuf & operator=(const charbuf &) = default; + constexpr charbuf & operator=(charbuf &&) = default; const Tchar & operator[](std::size_t i) const { return buf[i]; } |