Skip to content

Commit ae97da9

Browse files
committed
[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.
git-svn-id: https://source.openmpt.org/svn/openmpt/trunk/OpenMPT@22580 56274372-70c3-4bfc-bfc3-4c3a0b034d27
1 parent bb906e1 commit ae97da9

File tree

3 files changed

+23
-31
lines changed

3 files changed

+23
-31
lines changed

soundlib/ModInstrument.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,6 @@ void InstrumentEnvelope::Sanitize(uint8 maxValue)
152152
}
153153

154154

155-
ModInstrument::ModInstrument(SAMPLEINDEX sample)
156-
{
157-
SetCutoff(0, false);
158-
SetResonance(0, false);
159-
160-
pitchToTempoLock.Set(0);
161-
162-
AssignSample(sample);
163-
ResetNoteMap();
164-
}
165-
166-
167155
// Translate instrument properties between two given formats.
168156
void ModInstrument::Convert(MODTYPE fromType, MODTYPE toType)
169157
{

soundlib/ModInstrument.h

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ struct EnvelopeNode
3535
tick_t tick = 0; // Envelope node position (x axis)
3636
value_t value = 0; // Envelope node value (y axis)
3737

38-
EnvelopeNode() { }
39-
EnvelopeNode(tick_t tick, value_t value) : tick(tick), value(value) { }
38+
constexpr EnvelopeNode() = default;
39+
constexpr EnvelopeNode(tick_t tick, value_t value) : tick{tick}, value{value} { }
4040

4141
bool operator== (const EnvelopeNode &other) const { return tick == other.tick && value == other.value; }
4242
};
@@ -128,16 +128,20 @@ struct ModInstrument
128128
// WHEN adding new members here, ALSO update InstrumentExtensions.cpp
129129
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
130130

131-
explicit ModInstrument(SAMPLEINDEX sample = 0);
131+
MPT_CONSTEXPR20_FUN explicit ModInstrument(SAMPLEINDEX sample = 0)
132+
{
133+
AssignSample(sample);
134+
ResetNoteMap();
135+
}
132136

133137
// Assign all notes to a given sample.
134-
void AssignSample(SAMPLEINDEX sample)
138+
MPT_CONSTEXPR20_FUN void AssignSample(SAMPLEINDEX sample)
135139
{
136140
Keyboard.fill(sample);
137141
}
138142

139143
// Reset note mapping (i.e. every note is mapped to itself)
140-
void ResetNoteMap()
144+
MPT_CONSTEXPR20_FUN void ResetNoteMap()
141145
{
142146
std::iota(NoteMap.begin(), NoteMap.end(), static_cast<uint8>(NOTE_MIN));
143147
}
@@ -149,23 +153,23 @@ struct ModInstrument
149153
// Transpose entire note mapping by given number of semitones
150154
void Transpose(int8 amount);
151155

152-
bool IsCutoffEnabled() const { return (nIFC & 0x80) != 0; }
153-
bool IsResonanceEnabled() const { return (nIFR & 0x80) != 0; }
154-
uint8 GetCutoff() const { return (nIFC & 0x7F); }
155-
uint8 GetResonance() const { return (nIFR & 0x7F); }
156-
void SetCutoff(uint8 cutoff, bool enable) { nIFC = std::min(cutoff, uint8(0x7F)) | (enable ? 0x80 : 0x00); }
157-
void SetResonance(uint8 resonance, bool enable) { nIFR = std::min(resonance, uint8(0x7F)) | (enable ? 0x80 : 0x00); }
156+
MPT_CONSTEXPRINLINE bool IsCutoffEnabled() const { return (nIFC & 0x80) != 0; }
157+
MPT_CONSTEXPRINLINE bool IsResonanceEnabled() const { return (nIFR & 0x80) != 0; }
158+
MPT_CONSTEXPRINLINE uint8 GetCutoff() const { return (nIFC & 0x7F); }
159+
MPT_CONSTEXPRINLINE uint8 GetResonance() const { return (nIFR & 0x7F); }
160+
MPT_CONSTEXPRINLINE void SetCutoff(uint8 cutoff, bool enable) { nIFC = std::min(cutoff, uint8(0x7F)) | (enable ? 0x80 : 0x00); }
161+
MPT_CONSTEXPRINLINE void SetResonance(uint8 resonance, bool enable) { nIFR = std::min(resonance, uint8(0x7F)) | (enable ? 0x80 : 0x00); }
158162

159-
bool HasValidMIDIChannel() const { return (nMidiChannel >= 1 && nMidiChannel <= 17); }
163+
MPT_CONSTEXPRINLINE bool HasValidMIDIChannel() const { return (nMidiChannel >= 1 && nMidiChannel <= 17); }
160164
uint8 GetMIDIChannel(const ModChannel &channel, CHANNELINDEX chn) const;
161165

162-
void SetTuning(CTuning *pT)
166+
MPT_CONSTEXPRINLINE void SetTuning(CTuning *pT)
163167
{
164168
pTuning = pT;
165169
}
166170

167171
// Get a reference to a specific envelope of this instrument
168-
const InstrumentEnvelope &GetEnvelope(EnvelopeType envType) const
172+
MPT_CONSTEXPRINLINE const InstrumentEnvelope &GetEnvelope(EnvelopeType envType) const
169173
{
170174
switch(envType)
171175
{

src/mpt/string/buffer.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,13 @@ struct charbuf {
310310
Tchar buf[len];
311311

312312
public:
313-
charbuf() {
313+
MPT_CONSTEXPR20_FUN charbuf() {
314314
std::fill(std::begin(buf), std::end(buf), char_constants<Tchar>::null);
315315
}
316-
charbuf(const charbuf &) = default;
317-
charbuf(charbuf &&) = default;
318-
charbuf & operator=(const charbuf &) = default;
319-
charbuf & operator=(charbuf &&) = default;
316+
constexpr charbuf(const charbuf &) = default;
317+
constexpr charbuf(charbuf &&) = default;
318+
constexpr charbuf & operator=(const charbuf &) = default;
319+
constexpr charbuf & operator=(charbuf &&) = default;
320320
const Tchar & operator[](std::size_t i) const {
321321
return buf[i];
322322
}

0 commit comments

Comments
 (0)