Codechange: use RAII to automatically restore _cur_dpi after use

This commit is contained in:
Rubidium
2023-01-26 19:21:36 +01:00
committed by rubidium42
parent b7a5d8e296
commit f001e84e5e
18 changed files with 122 additions and 136 deletions

View File

@@ -144,4 +144,46 @@ private:
const int line;
};
/**
* Class to backup a specific variable and restore it upon destruction of this object to prevent
* stack values going out of scope before resetting the global to its original value. Contrary to
* #Backup this restores the variable automatically and there is no manual option to restore.
*/
template <typename T>
struct AutoRestoreBackup {
/*
* There is explicitly no only original constructor version, as that would make it possible
* for the new value to go out of scope before this object goes out of scope, thus defeating
* the whole goal and reason for existing of this object.
*/
/**
* Backup variable and switch to new value.
* @param original Variable to backup.
* @param new_value New value for variable.
*/
AutoRestoreBackup(T &original, T new_value) : original(original), original_value(original)
{
original = new_value;
}
/**
* Restore the variable upon object destruction.
*/
~AutoRestoreBackup()
{
this->original = this->original_value;
}
private:
T &original;
T original_value;
/* Prevent copy, assignment and allocation on stack. */
AutoRestoreBackup(const AutoRestoreBackup&) = delete;
AutoRestoreBackup& operator=(AutoRestoreBackup&) = delete;
static void *operator new(std::size_t) = delete;
static void *operator new[](std::size_t) = delete;
};
#endif /* BACKUP_TYPE_HPP */