From e99e6259f7fb863ac8acd454921d486efd42d807 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Fri, 19 Aug 2016 19:45:43 +0300 Subject: [PATCH] Add a helper class to make sure a certain value is assigned to a variable regardless of the thrown exceptions. --- source/Base/Shared.hpp | 84 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 4 deletions(-) diff --git a/source/Base/Shared.hpp b/source/Base/Shared.hpp index 0628cc56..856865d0 100644 --- a/source/Base/Shared.hpp +++ b/source/Base/Shared.hpp @@ -254,12 +254,12 @@ public: } /* -------------------------------------------------------------------------------------------- - * Copy constructor. + * Copy constructor. (disabled) */ AutoDelete(const AutoDelete & o) = delete; /* -------------------------------------------------------------------------------------------- - * Move constructor. + * Move constructor. (disabled) */ AutoDelete(AutoDelete && o) = delete; @@ -275,12 +275,12 @@ public: } /* -------------------------------------------------------------------------------------------- - * Copy assignment operator. + * Copy assignment operator. (disabled) */ AutoDelete & operator = (const AutoDelete & o) = delete; /* -------------------------------------------------------------------------------------------- - * Move assignment operator. + * Move assignment operator. (disabled) */ AutoDelete & operator = (AutoDelete && o) = delete; @@ -335,6 +335,82 @@ public: } }; +/* ------------------------------------------------------------------------------------------------ + * RAII approach to make sure a value is assigned regardless of what exceptions are thrown. +*/ +template < typename T > class AutoAssign +{ +private: + + // -------------------------------------------------------------------------------------------- + T & m_Var; // Variable to receive the value. + T m_Val; // Value to be assigned. + +public: + + /* -------------------------------------------------------------------------------------------- + * Default constructor. + */ + AutoAssign(T & variable, T value) + : m_Var(variable), m_Val(value) + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * Default constructor. + */ + AutoAssign(T & variable, T value, T start) + : m_Var(variable), m_Val(value) + { + m_Var = start; + } + + /* -------------------------------------------------------------------------------------------- + * Copy constructor. (disabled) + */ + AutoAssign(const AutoAssign & o) = delete; + + /* -------------------------------------------------------------------------------------------- + * Move constructor. (disabled) + */ + AutoAssign(AutoAssign && o) = delete; + + /* -------------------------------------------------------------------------------------------- + * Destructor. + */ + ~AutoAssign() + { + m_Var = m_Val; + } + + /* -------------------------------------------------------------------------------------------- + * Copy assignment operator. (disabled) + */ + AutoAssign & operator = (const AutoAssign & o) = delete; + + /* -------------------------------------------------------------------------------------------- + * Move assignment operator. (disabled) + */ + AutoAssign & operator = (AutoAssign && o) = delete; + + /* -------------------------------------------------------------------------------------------- + * Direct value assignment. + */ + template < typename U > AutoAssign & operator = (U value) + { + m_Var = value; + } + + /* -------------------------------------------------------------------------------------------- + * Direct value assignment. + */ + template < typename U > void Set(U value) + { + m_Var = value; + } +}; + /* ------------------------------------------------------------------------------------------------ * Retrieve the string delimiter of a base type. */