From 8e276ab2ab09bae394570b209702f645a2a69b96 Mon Sep 17 00:00:00 2001 From: nedko Date: Fri, 12 Dec 2025 15:35:32 +0200 Subject: [PATCH] Move to more memory safe implementation of widget holding --- Widgets/Widget.h | 1 + Widgets/WidgetRect.cpp | 4 ++-- Widgets/WidgetRect.h | 2 +- Widgets/WidgetText.cpp | 4 ++-- Widgets/WidgetText.h | 2 +- main.cpp | 20 +++++++++----------- 6 files changed, 16 insertions(+), 17 deletions(-) diff --git a/Widgets/Widget.h b/Widgets/Widget.h index 6801d4f..6b7b964 100644 --- a/Widgets/Widget.h +++ b/Widgets/Widget.h @@ -3,6 +3,7 @@ #include #include "../json.hpp" +#include class Widget { diff --git a/Widgets/WidgetRect.cpp b/Widgets/WidgetRect.cpp index 2051450..f92971c 100644 --- a/Widgets/WidgetRect.cpp +++ b/Widgets/WidgetRect.cpp @@ -97,7 +97,7 @@ void WidgetRect::draw() } } -Widget* WidgetRect::builder(const nlohmann::json& j) +std::unique_ptr WidgetRect::builder(const nlohmann::json& j) { int x = 0; int y = 0; @@ -115,7 +115,7 @@ Widget* WidgetRect::builder(const nlohmann::json& j) json_extract(j, "stroke", stroke_size); json_extract(j, "color", color); - return new WidgetRect(x, y, width, height, radius, stroke_size, color); + return std::make_unique(x, y, width, height, radius, stroke_size, color); } void WidgetRect::draw_circle_corner(int x, int y, int quadrant) diff --git a/Widgets/WidgetRect.h b/Widgets/WidgetRect.h index f5ab905..b69ab97 100644 --- a/Widgets/WidgetRect.h +++ b/Widgets/WidgetRect.h @@ -28,7 +28,7 @@ public: virtual void draw() override; - static Widget* builder(const nlohmann::json& j); + static std::unique_ptr builder(const nlohmann::json& j); private: // x, y - center of circle diff --git a/Widgets/WidgetText.cpp b/Widgets/WidgetText.cpp index 770d30e..640fb53 100644 --- a/Widgets/WidgetText.cpp +++ b/Widgets/WidgetText.cpp @@ -262,7 +262,7 @@ void WidgetText::draw() SDL_FreeSurface(txt_surface); } -Widget* WidgetText::builder(const nlohmann::json& j) +std::unique_ptr WidgetText::builder(const nlohmann::json& j) { int x = 0; int y = 0; @@ -299,7 +299,7 @@ Widget* WidgetText::builder(const nlohmann::json& j) return nullptr; } - return new WidgetText(x, y, width, height, + return std::make_unique(x, y, width, height, text, fit, should_wrap, halign, valign, halign_via_visible, valign_via_visible, diff --git a/Widgets/WidgetText.h b/Widgets/WidgetText.h index e0fc988..bd8394f 100644 --- a/Widgets/WidgetText.h +++ b/Widgets/WidgetText.h @@ -73,7 +73,7 @@ public: virtual void draw() override; - static Widget* builder(const nlohmann::json& j); + static std::unique_ptr builder(const nlohmann::json& j); }; #endif // WIDGET_TEXT_H_ diff --git a/main.cpp b/main.cpp index 4f82b02..1b3ca0b 100644 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -17,14 +18,16 @@ using std::cout; using std::endl; using std::map; +using std::shared_ptr; using std::string; +using std::unique_ptr; using std::vector; using nlohmann::json; -void init_builders(map& widget_builders) +void init_builders(map(*)(const json&)>& widget_builders) { - widget_builders["text"] = &WidgetText::builder; widget_builders["rect"] = &WidgetRect::builder; + widget_builders["text"] = &WidgetText::builder; } int main(int argc, char **argv) @@ -41,8 +44,8 @@ int main(int argc, char **argv) SDL_Surface* main_surface = nullptr; vector widgets_cfg; - vector widgets; - map widget_builders; + vector> widgets; + map(*)(const json&)> widget_builders; ok = init_sdl(); if (!ok) @@ -102,7 +105,7 @@ int main(int argc, char **argv) } // Construct and add widget to vector - Widget* widget = widget_builders[widget_name](j); + shared_ptr widget = widget_builders[widget_name](j); if (nullptr != widget) { widgets.push_back(widget); @@ -113,7 +116,7 @@ int main(int argc, char **argv) SDL_FillRect(main_surface, nullptr, SDL_MapRGBA(main_surface->format, 255, 255, 255, SDL_ALPHA_OPAQUE)); // Draw and apply all widgets - for (Widget* widget : widgets) + for (shared_ptr& widget : widgets) { widget->draw(); SDL_Rect rect = widget->get_rect(); @@ -125,11 +128,6 @@ int main(int argc, char **argv) IMG_SavePNG(main_surface, output_filename.c_str()); cleanup: - // Destroy All Widgets - for (Widget* widget : widgets) - { - delete widget; - } widgets.clear(); clean_sdl();