Skip to content

Commit

Permalink
Add guard to safely reset bools
Browse files Browse the repository at this point in the history
Add the new local class `BoolGuard` which uses RAII to safely set a
boolean to a value and later reset it to the old value.

The guard is needed to ensure that the boolean `m_presetMode` is safely
set to `true` in `savePreset` and `loadPreset` and reset even in case of
exceptions.

For this to work `m_presetMode` had to be changed to protected
visibility. On this other hand this enables the removal of
`setPresetMode`.

Add comments to the two places where previously the mode was reset so
that people do not wonder why nothing is reset and potentially put the
code back in there.
  • Loading branch information
michaelgregorius committed Aug 16, 2024
1 parent eb8633a commit e3bccb0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
9 changes: 3 additions & 6 deletions include/Track.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,9 @@ public slots:

protected:
bool isPresetMode() const { return m_presetMode; }
void setPresetMode(bool presetMode = true)
{
m_presetMode = presetMode;
}

protected:
bool m_presetMode = false;

private:
TrackContainer* m_trackContainer;
Expand All @@ -224,8 +223,6 @@ public slots:
BoolModel m_soloModel;
bool m_mutedBeforeSolo;

bool m_presetMode = false;

clipVector m_clips;

QMutex m_processingLock;
Expand Down
4 changes: 2 additions & 2 deletions src/core/Track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void Track::saveSettings( QDomDocument & doc, QDomElement & element )

if (isPresetMode())
{
setPresetMode(false);
// No need to unset preset mode here as this will done by the guard in InstrumentTrack::savePreset
return;
}

Expand Down Expand Up @@ -279,7 +279,7 @@ void Track::loadSettings( const QDomElement & element )
node = node.nextSibling();
}

setPresetMode(false);
// No need to unset preset mode here as this will done by the guard in InstrumentTrack::loadPreset

return;
}
Expand Down
34 changes: 32 additions & 2 deletions src/tracks/InstrumentTrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,15 +994,45 @@ void InstrumentTrack::loadTrackSpecificSettings( const QDomElement & thisElement
unlock();
}

/**
* @brief RAII "guard" used to safely change and reset booleans even during exceptions
*
* Needed to safely reset m_presetMode in savePreset and loadPreset. For this reason
* only defined locally because at least the pattern used by these methods should not
* spread outside.
*/
class BoolGuard
{
public:
BoolGuard(bool& member, bool temporaryValue) :
m_member(member),
m_oldValue(member)
{
m_member = temporaryValue;
}

~BoolGuard()
{
m_member = m_oldValue;
}

BoolGuard(const BoolGuard&) = delete;
BoolGuard& operator=(const BoolGuard&) = delete;

private:
bool & m_member;
bool const m_oldValue;
};

void InstrumentTrack::savePreset(QDomDocument & doc, QDomElement & element)
{
setPresetMode();
BoolGuard guard(m_presetMode, true);
saveSettings(doc, element);
}

void InstrumentTrack::loadPreset(const QDomElement & element)
{
setPresetMode();
BoolGuard guard(m_presetMode, true);
loadSettings(element);
}

Expand Down

0 comments on commit e3bccb0

Please sign in to comment.