diff --git a/shared/Base/Utility.hpp b/shared/Base/Utility.hpp index 3cfffeaa..91fd23df 100644 --- a/shared/Base/Utility.hpp +++ b/shared/Base/Utility.hpp @@ -1286,6 +1286,83 @@ typedef BitGuard< Uint8 > BitGuardU8; typedef BitGuard< Uint16 > BitGuardU16; typedef BitGuard< Uint32 > BitGuardU32; +/* ------------------------------------------------------------------------------------------------ + * RAII approach to make sure an instance is deleted regardless of what exceptions are thrown. +*/ +template < typename T > struct DeleteGuard +{ +private: + + // -------------------------------------------------------------------------------------------- + T * m_Ptr; // Pointer to the instance to manage. + +public: + + /* -------------------------------------------------------------------------------------------- + * Default constructor. + */ + DeleteGuard(T * ptr) + : m_Ptr(ptr) + { + /* ... */ + } + + /* -------------------------------------------------------------------------------------------- + * Copy constructor. (disabled) + */ + DeleteGuard(const DeleteGuard & o) = delete; + + /* -------------------------------------------------------------------------------------------- + * Move constructor. (disabled) + */ + DeleteGuard(DeleteGuard && o) = delete; + + /* -------------------------------------------------------------------------------------------- + * Destructor. + */ + ~DeleteGuard() + { + if (m_Ptr) + { + delete m_Ptr; + } + } + + /* -------------------------------------------------------------------------------------------- + * Copy assignment operator. (disabled) + */ + DeleteGuard & operator = (const DeleteGuard & o) = delete; + + /* -------------------------------------------------------------------------------------------- + * Move assignment operator. (disabled) + */ + DeleteGuard & operator = (DeleteGuard && o) = delete; + + /* -------------------------------------------------------------------------------------------- + * Implicit conversion the managed instance type. + */ + operator T * () const + { + return m_Ptr; + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the managed instance. + */ + T * Get() const + { + return m_Ptr; + } + + /* -------------------------------------------------------------------------------------------- + * Release the managed instance. + */ + void Release() + { + m_Ptr = nullptr; + } +}; + /* ------------------------------------------------------------------------------------------------ * RAII approach to make sure a value is assigned regardless of what exceptions are thrown. */ @@ -1300,7 +1377,7 @@ private: public: /* -------------------------------------------------------------------------------------------- - * Default constructor. + * Base constructor. */ AutoAssign(T & variable, T value) : m_Var(variable), m_Val(value) @@ -1309,7 +1386,7 @@ public: } /* -------------------------------------------------------------------------------------------- - * Default constructor. + * Base constructor. */ AutoAssign(T & variable, T value, T start) : m_Var(variable), m_Val(value)