mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-01-19 12:07:13 +01:00
Add a helper class that implements RAII to delete an instance of not explicitly released.
This commit is contained in:
parent
3a8d4952c1
commit
29abf2e9c0
@ -177,6 +177,99 @@ Color3 GetColor(CSStr 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.
|
||||
*/
|
||||
AutoDelete(const AutoDelete & o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor.
|
||||
*/
|
||||
AutoDelete(AutoDelete && o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Destructor.
|
||||
*/
|
||||
~AutoDelete()
|
||||
{
|
||||
if (m_Inst)
|
||||
{
|
||||
delete m_Inst;
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy assignment operator.
|
||||
*/
|
||||
AutoDelete & operator = (const AutoDelete & o) = delete;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move assignment operator.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the managed instance.
|
||||
*/
|
||||
T * Get()
|
||||
{
|
||||
return m_Inst;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Retrieve the managed instance.
|
||||
*/
|
||||
const T * Get() const
|
||||
{
|
||||
return m_Inst;
|
||||
}
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
#endif // _BASE_SHARED_HPP_
|
||||
|
Loading…
x
Reference in New Issue
Block a user