From 29abf2e9c070ed708a7652e6f23d95a9ce13badf Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Mon, 20 Jun 2016 18:01:42 +0300 Subject: [PATCH] Add a helper class that implements RAII to delete an instance of not explicitly released. --- source/Base/Shared.hpp | 93 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/source/Base/Shared.hpp b/source/Base/Shared.hpp index a22da57f..eba9638d 100644 --- a/source/Base/Shared.hpp +++ b/source/Base/Shared.hpp @@ -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_