diff --git a/source/gui/CGUI.cpp b/source/gui/CGUI.cpp index a2e2973073..f08981a94a 100644 --- a/source/gui/CGUI.cpp +++ b/source/gui/CGUI.cpp @@ -105,11 +105,7 @@ CGUI::CGUI(ScriptContext& context) m_ScriptInterface->LoadGlobalScripts(); } -CGUI::~CGUI() -{ - for (const std::pair& p : m_pAllObjects) - delete p.second; -} +CGUI::~CGUI() = default; InReaction CGUI::HandleEvent(const SDL_Event_* ev) { @@ -400,7 +396,7 @@ void CGUI::UpdateResolution() m_BaseObject->RecurseObject(nullptr, &IGUIObject::UpdateCachedSize); } -IGUIObject* CGUI::ConstructObject(const CStr& str) +std::unique_ptr CGUI::ConstructObject(const CStr& str) { std::map::iterator it = m_ObjectTypes.find(str); @@ -410,23 +406,22 @@ IGUIObject* CGUI::ConstructObject(const CStr& str) return (*it->second)(*this); } -bool CGUI::AddObject(IGUIObject& parent, IGUIObject& child) +void CGUI::AddObject(IGUIObject& parent, std::unique_ptr child) { - if (child.m_Name.empty()) + if (child->m_Name.empty()) { LOGERROR("Can't register an object without name!"); - return false; + return; } - if (m_pAllObjects.find(child.m_Name) != m_pAllObjects.end()) + if (m_pAllObjects.find(child->m_Name) != m_pAllObjects.end()) { - LOGERROR("Can't register more than one object of the name %s", child.m_Name.c_str()); - return false; + LOGERROR("Can't register more than one object of the name %s", child->m_Name.c_str()); + return; } - m_pAllObjects[child.m_Name] = &child; - parent.RegisterChild(&child); - return true; + parent.RegisterChild(child.get()); + m_pAllObjects[child->m_Name] = std::move(child); } IGUIObject* CGUI::GetBaseObject() @@ -446,7 +441,7 @@ IGUIObject* CGUI::FindObjectByName(const CStr& Name) const if (it == m_pAllObjects.end()) return nullptr; - return it->second; + return it->second.get(); } IGUIObject* CGUI::FindObjectUnderMouse() @@ -669,7 +664,7 @@ void CGUI::Xeromyces_ReadObject(const XMBData& xmb, XMBElement element, IGUIObje // Construct object from specified type // henceforth, we need to do a rollback before aborting. // i.e. releasing this object - IGUIObject* object = ConstructObject(type); + std::unique_ptr object = ConstructObject(type); if (!object) { @@ -953,8 +948,7 @@ void CGUI::Xeromyces_ReadObject(const XMBData& xmb, XMBElement element, IGUIObje object->m_Z.Set(10.f, false); } - if (!AddObject(parent, *object)) - delete object; + AddObject(parent, std::move(object)); } void CGUI::Xeromyces_ReadRepeat(const XMBData& xmb, XMBElement element, IGUIObject& parent, @@ -1333,7 +1327,7 @@ void CGUI::Xeromyces_ReadIcon(const XMBData& xmb, XMBElement element) void CGUI::Xeromyces_ReadTooltip(const XMBData& xmb, XMBElement element) { - IGUIObject* object = new CTooltip(*this); + std::unique_ptr object{std::make_unique(*this)}; for (XMBAttribute attr : element.GetAttributes()) { @@ -1346,8 +1340,7 @@ void CGUI::Xeromyces_ReadTooltip(const XMBData& xmb, XMBElement element) object->SetSettingFromString(std::string(attr_name), attr_value.FromUTF8(), true); } - if (!AddObject(*m_BaseObject, *object)) - delete object; + AddObject(*m_BaseObject, std::move(object)); } void CGUI::Xeromyces_ReadColor(const XMBData& xmb, XMBElement element) diff --git a/source/gui/CGUI.h b/source/gui/CGUI.h index a6be52f807..b4a4974082 100644 --- a/source/gui/CGUI.h +++ b/source/gui/CGUI.h @@ -71,7 +71,7 @@ struct SGUIImageEffects; extern const double SELECT_DBLCLICK_RATE; -using map_pObjects = std::map; +using map_pObjects = std::map>; /** * The main object that represents a whole GUI page. @@ -82,7 +82,7 @@ class CGUI private: // Private typedefs - using ConstructObjectFunction = IGUIObject* (*)(CGUI&); + using ConstructObjectFunction = std::unique_ptr (*)(CGUI&); public: CGUI(ScriptContext& context); @@ -288,7 +288,7 @@ private: * The CGUI takes ownership of the child object and links the parent with the child. * Returns false on failure to take over ownership of the child object. */ - bool AddObject(IGUIObject& parent, IGUIObject& child); + void AddObject(IGUIObject& parent, std::unique_ptr child); /** * You input the name of the object type, and let's @@ -298,7 +298,7 @@ private: * @param str Name of object type * @return Newly constructed IGUIObject (but constructed as a subclass) */ - IGUIObject* ConstructObject(const CStr& str); + std::unique_ptr ConstructObject(const CStr& str); public: /** diff --git a/source/gui/ObjectBases/IGUIObject.h b/source/gui/ObjectBases/IGUIObject.h index d7c9fb0578..88e2472a7e 100644 --- a/source/gui/ObjectBases/IGUIObject.h +++ b/source/gui/ObjectBases/IGUIObject.h @@ -53,8 +53,8 @@ struct SDL_Event_; #define GUI_OBJECT(obj) \ public: \ - static IGUIObject* ConstructObject(CGUI& pGUI) \ - { return new obj(pGUI); } + static std::unique_ptr ConstructObject(CGUI& pGUI) \ + { return std::make_unique(pGUI); } /** * GUI object such as a button or an input-box.