mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2024-11-08 00:37:15 +01:00
Implement a utility to help with deleting instances even when exceptions are thrown.
This commit is contained in:
parent
4ad9402d8a
commit
ad876f6a8f
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user