mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-04-01 01:47:13 +02:00
Make the AutoDelete helper available to modules.
This commit is contained in:
parent
df5eedfced
commit
40c16ca5fc
@ -1447,6 +1447,109 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* RAII approach to delete an instance of a class if not released.
|
||||
*/
|
||||
template < typename T > class AutoDelete
|
||||
{
|
||||
private:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
T * m_Inst; // The managed instance.
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Default constructor.
|
||||
*/
|
||||
AutoDelete(T * inst)
|
||||
: m_Inst(inst)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor. (disabled)
|
||||
*/
|
||||
AutoDelete(const AutoDelete & o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor. (disabled)
|
||||
*/
|
||||
AutoDelete(AutoDelete && o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Destructor.
|
||||
*/
|
||||
~AutoDelete()
|
||||
{
|
||||
if (m_Inst)
|
||||
{
|
||||
delete m_Inst;
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy assignment operator. (disabled)
|
||||
*/
|
||||
AutoDelete & operator = (const AutoDelete & o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move assignment operator. (disabled)
|
||||
*/
|
||||
AutoDelete & operator = (AutoDelete && o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit cast to the managed instance.
|
||||
*/
|
||||
operator T * ()
|
||||
{
|
||||
return m_Inst;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit cast to the managed instance.
|
||||
*/
|
||||
operator const T * () const
|
||||
{
|
||||
return m_Inst;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Released the managed instance.
|
||||
*/
|
||||
void Release()
|
||||
{
|
||||
m_Inst = nullptr;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Released the managed instance.
|
||||
*/
|
||||
T * Grab()
|
||||
{
|
||||
T * ptr = m_Inst;
|
||||
m_Inst = nullptr;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the managed instance.
|
||||
*/
|
||||
T * Get()
|
||||
{
|
||||
return m_Inst;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the managed instance.
|
||||
*/
|
||||
const T * Get() const
|
||||
{
|
||||
return m_Inst;
|
||||
}
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the string representation of a certain type.
|
||||
*/
|
||||
|
@ -238,109 +238,6 @@ Color3 GetColor(const StackStrF & name);
|
||||
*/
|
||||
void SqThrowLastF(CSStr msg, ...);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* RAII approach to delete an instance of a class if not released.
|
||||
*/
|
||||
template < typename T > class AutoDelete
|
||||
{
|
||||
private:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
T * m_Inst; // The managed instance.
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Default constructor.
|
||||
*/
|
||||
AutoDelete(T * inst)
|
||||
: m_Inst(inst)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor. (disabled)
|
||||
*/
|
||||
AutoDelete(const AutoDelete & o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor. (disabled)
|
||||
*/
|
||||
AutoDelete(AutoDelete && o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Destructor.
|
||||
*/
|
||||
~AutoDelete()
|
||||
{
|
||||
if (m_Inst)
|
||||
{
|
||||
delete m_Inst;
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy assignment operator. (disabled)
|
||||
*/
|
||||
AutoDelete & operator = (const AutoDelete & o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move assignment operator. (disabled)
|
||||
*/
|
||||
AutoDelete & operator = (AutoDelete && o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit cast to the managed instance.
|
||||
*/
|
||||
operator T * ()
|
||||
{
|
||||
return m_Inst;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Implicit cast to the managed instance.
|
||||
*/
|
||||
operator const T * () const
|
||||
{
|
||||
return m_Inst;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Released the managed instance.
|
||||
*/
|
||||
void Release()
|
||||
{
|
||||
m_Inst = nullptr;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Released the managed instance.
|
||||
*/
|
||||
T * Grab()
|
||||
{
|
||||
T * ptr = m_Inst;
|
||||
m_Inst = nullptr;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the managed instance.
|
||||
*/
|
||||
T * Get()
|
||||
{
|
||||
return m_Inst;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the managed instance.
|
||||
*/
|
||||
const T * Get() const
|
||||
{
|
||||
return m_Inst;
|
||||
}
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the string delimiter of a base type.
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user