From: Mikko Rasa Date: Mon, 21 Aug 2023 10:05:52 +0000 (+0300) Subject: Use std::unique_ptr for managing memory X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=394e5c9969a30b604bfaf78fc05a8c2d5c98ab5b;p=libs%2Fgltk.git Use std::unique_ptr for managing memory --- diff --git a/Build b/Build index 0de8497..e8d32ed 100644 --- a/Build +++ b/Build @@ -11,7 +11,7 @@ package "mspgltk" build_info { - standard CXX "c++11"; + standard CXX "c++14"; }; library "mspgltk" diff --git a/source/container.cpp b/source/container.cpp index 5c17503..88e5952 100644 --- a/source/container.cpp +++ b/source/container.cpp @@ -21,7 +21,7 @@ Container::~Container() void Container::add(Widget &wdg) { wdg.set_parent(this); - children.push_back(new Child(*this, &wdg)); + children.push_back(make_unique(*this, &wdg)); if(wdg.get_animation_interval()) check_animation_interval(); children_rebuild_needed = true; @@ -31,14 +31,13 @@ void Container::add(Widget &wdg) void Container::remove(Widget &wdg) { - auto i = find_if(children, [&wdg](const Child *c){ return c->widget==&wdg; }); + auto i = find_if(children, [&wdg](const unique_ptr &c){ return c->widget==&wdg; }); if(i==children.end()) throw hierarchy_error("widget not in container"); if(&wdg==saved_input_focus) saved_input_focus = nullptr; wdg.set_parent(nullptr); - delete *i; children.erase(i); if(wdg.get_animation_interval()) check_animation_interval(); @@ -78,7 +77,7 @@ void Container::reposition_child(Widget &child, const Part &part) const vector Container::get_children() const { vector result; - for(const Child *c: children) + for(const unique_ptr &c: children) result.push_back(c->widget); return result; } @@ -106,13 +105,13 @@ Widget *Container::find_descendant_at(int x, int y) const void Container::raise(Widget &wdg) { - auto i = find_if(children, [&wdg](const Child *c){ return c->widget==&wdg; }); + auto i = find_if(children, [&wdg](const unique_ptr &c){ return c->widget==&wdg; }); if(i==children.end()) throw hierarchy_error("widget not in container"); - Child *c = *i; + unique_ptr c = move(*i); children.erase(i); - children.push_back(c); + children.push_back(move(c)); } void Container::set_pointer_focus(Widget *wdg, bool grab) @@ -159,7 +158,7 @@ Widget *Container::get_final_input_focus() const void Container::check_animation_interval() { Time::TimeDelta shortest; - for(const Child *c: children) + for(const unique_ptr &c: children) { const Time::TimeDelta &child_iv = c->widget->get_animation_interval(); if(child_iv && (!shortest || child_iv &c: children) c->widget->rebuild_hierarchy(); } } @@ -337,7 +336,7 @@ bool Container::navigate(Navigation nav) void Container::animate(const Time::TimeDelta &dt) { - for(Child *c: children) + for(const unique_ptr &c: children) { const Time::TimeDelta &child_iv = c->widget->get_animation_interval(); if(!child_iv) @@ -355,7 +354,7 @@ void Container::animate(const Time::TimeDelta &dt) void Container::on_reparent() { - for(const Child *c: children) + for(const unique_ptr &c: children) { if(Container *o = dynamic_cast(c->widget)) o->on_reparent(); diff --git a/source/container.h b/source/container.h index 45985bc..6311a41 100644 --- a/source/container.h +++ b/source/container.h @@ -1,6 +1,7 @@ #ifndef MSP_GLTK_CONTAINER_H_ #define MSP_GLTK_CONTAINER_H_ +#include #include #include #include @@ -37,7 +38,7 @@ protected: void rebuild_needed(); }; - std::vector children; + std::vector> children; Widget *click_focus = nullptr; unsigned click_button = 0; Widget *pointer_focus = nullptr; diff --git a/source/dialog.cpp b/source/dialog.cpp index 5f63dcf..9f9fc20 100644 --- a/source/dialog.cpp +++ b/source/dialog.cpp @@ -66,7 +66,7 @@ Dialog::Loader::Loader(Dialog &d, WidgetMap &wm): void Dialog::Loader::action_button(const string &n, int c) { - RefPtr