]> git.tdb.fi Git - libs/gltk.git/commitdiff
Use std::unique_ptr for managing memory
authorMikko Rasa <tdb@tdb.fi>
Mon, 21 Aug 2023 10:05:52 +0000 (13:05 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 21 Aug 2023 12:51:08 +0000 (15:51 +0300)
16 files changed:
Build
source/container.cpp
source/container.h
source/dialog.cpp
source/layout.cpp
source/layout.h
source/list.cpp
source/list.h
source/panel.cpp
source/panel.h
source/partcache.cpp
source/partcache.h
source/resources.cpp
source/resources.h
source/root.cpp
source/root.h

diff --git a/Build b/Build
index 0de849752693355fbe28b4d897256b78bbbaf548..e8d32ed0d5282635391b823e8672278146c983cc 100644 (file)
--- a/Build
+++ b/Build
@@ -11,7 +11,7 @@ package "mspgltk"
 
        build_info
        {
-               standard CXX "c++11";
+               standard CXX "c++14";
        };
 
        library "mspgltk"
index 5c175031d76515e8b2bd2c269bdd9806baeb39bc..88e59524e8ddac754a69b7506d230d6d019957be 100644 (file)
@@ -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<Child>(*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<Child> &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<Widget *> Container::get_children() const
 {
        vector<Widget *> result;
-       for(const Child *c: children)
+       for(const unique_ptr<Child> &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<Child> &c){ return c->widget==&wdg; });
        if(i==children.end())
                throw hierarchy_error("widget not in container");
 
-       Child *c = *i;
+       unique_ptr<Child> 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<Child> &c: children)
        {
                const Time::TimeDelta &child_iv = c->widget->get_animation_interval();
                if(child_iv && (!shortest || child_iv<shortest))
@@ -177,7 +176,7 @@ void Container::rebuild_hierarchy()
        if(children_rebuild_needed)
        {
                children_rebuild_needed = false;
-               for(Child *c: children)
+               for(const unique_ptr<Child> &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<Child> &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<Child> &c: children)
        {
                if(Container *o = dynamic_cast<Container *>(c->widget))
                        o->on_reparent();
index 45985bc0ad585c304d5735f2ad037b016bbdc53b..6311a411a57fb1b16e03dcdd24564242a2ac5902 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GLTK_CONTAINER_H_
 #define MSP_GLTK_CONTAINER_H_
 
+#include <memory>
 #include <stdexcept>
 #include <vector>
 #include <sigc++/trackable.h>
@@ -37,7 +38,7 @@ protected:
                void rebuild_needed();
        };
 
-       std::vector<Child *> children;
+       std::vector<std::unique_ptr<Child>> children;
        Widget *click_focus = nullptr;
        unsigned click_button = 0;
        Widget *pointer_focus = nullptr;
index 5f63dcf75ed519fbf52c65e723422304fe395234..9f9fc20d15388f69ece201db0105265ddae7c74e 100644 (file)
@@ -66,7 +66,7 @@ Dialog::Loader::Loader(Dialog &d, WidgetMap &wm):
 
 void Dialog::Loader::action_button(const string &n, int c)
 {
-       RefPtr<Button> btn = new Button();
+       unique_ptr<Button> btn = make_unique<Button>();
        load_sub(*btn);
        obj.add_button(*btn.get(), c);
        last_widget = wdg_map[n] = btn.release();
index e9461b5eddf855e72fed34b8e6b4fa31bd90eefa..495d6261c6483b55f3f76bee50b7980eb0fd33a1 100644 (file)
@@ -85,12 +85,6 @@ Layout::Pointers Layout::pointers[2] =
 } };
 
 
-Layout::~Layout()
-{
-       for(Slot *s: slots)
-               delete s;
-}
-
 void Layout::set_container(Container &c)
 {
        if(container)
@@ -163,7 +157,7 @@ void Layout::add_widget(Widget &wdg)
        if(!container)
                throw logic_error("!container");
 
-       slots.push_back(new Slot(*this, wdg));
+       slots.emplace_back(make_unique<Slot>(*this, wdg));
        update_slot_indices();
        if(!arrangement_stack.empty())
                arrangement_stack.back()->arrange(wdg);
@@ -173,23 +167,22 @@ void Layout::add_widget(Widget &wdg)
 
 void Layout::remove_widget(Widget &wdg)
 {
-       auto i = find_if(slots, [&wdg](Slot *s){ return &s->widget==&wdg; });
+       auto i = find_if(slots, [&wdg](const unique_ptr<Slot> &s){ return &s->widget==&wdg; });
        if(i==slots.end())
                return;
 
-       for(Slot *s: slots)
+       for(const unique_ptr<Slot> &s: slots)
                if(s!=*i)
                {
                        for(auto k=s->constraints.begin(); k!=s->constraints.end(); )
                        {
-                               if(k->target==*i)
+                               if(k->target==i->get())
                                        k = s->constraints.erase(k);
                                else
                                        ++k;
                        }
                }
 
-       delete *i;
        slots.erase(i);
 
        update_slot_indices();
@@ -200,7 +193,7 @@ void Layout::update_slot_indices()
 {
        n_active_slots = 0;
        size_t n_floating = 0;
-       for(Slot *s: slots)
+       for(const unique_ptr<Slot> &s: slots)
        {
                if(s->widget.is_visible() || s->ghost)
                {
@@ -214,13 +207,13 @@ void Layout::update_slot_indices()
 
        n_slack_vars[0] = n_floating*2;
        n_slack_vars[1] = n_floating*2;
-       for(const Slot *s: slots)
+       for(const unique_ptr<Slot> &s: slots)
                if(s->index>=0)
                {
                        if(!s->floating)
                        {
                                for(unsigned j=0; j<2; ++j)
-                                       if((s->*(pointers[j].packing)).gravity==0)
+                                       if((s.get()->*(pointers[j].packing)).gravity==0)
                                                n_slack_vars[j] += 2;
                        }
 
@@ -232,7 +225,7 @@ void Layout::update_slot_indices()
 
 Layout::Slot &Layout::get_slot_for_widget(Widget &wdg)
 {
-       auto i = find_if(slots, [&wdg](const Slot *s){ return &s->widget==&wdg; });
+       auto i = find_if(slots, [&wdg](const unique_ptr<Slot> &s){ return &s->widget==&wdg; });
        if(i==slots.end())
                throw hierarchy_error("widget not in layout");
 
@@ -324,7 +317,7 @@ void Layout::update()
        solve_constraints(HORIZONTAL, UPDATE);
        solve_constraints(VERTICAL, UPDATE);
 
-       for(const Slot *s: slots)
+       for(const unique_ptr<Slot> &s: slots)
                s->widget.set_geometry(s->geom);
 }
 
@@ -352,7 +345,7 @@ void Layout::solve_constraints(int dir, SolveMode mode)
        LinearProgram linprog(n_active_slots*5+n_slack_vars[dir]+1);
        float weight = slots.size()+1;
        size_t k = n_active_slots*5;
-       for(const Slot *s: slots)
+       for(const unique_ptr<Slot> &s: slots)
        {
                if(s->index<0)
                        continue;
@@ -366,8 +359,8 @@ void Layout::solve_constraints(int dir, SolveMode mode)
                else
                {
                        if(!s->floating)
-                               objective[s->index*5] = (s->*(ptrs.packing)).gravity/weight;
-                       objective[s->index*5+1] = ((s->*(ptrs.packing)).expand ? weight : -1);
+                               objective[s->index*5] = (s.get()->*(ptrs.packing)).gravity/weight;
+                       objective[s->index*5+1] = ((s.get()->*(ptrs.packing)).expand ? weight : -1);
                }
 
                {
@@ -388,12 +381,12 @@ void Layout::solve_constraints(int dir, SolveMode mode)
                        row.back() = geom.*(ptrs.dim)-margin.*(ptrs.high_margin);
                }
 
-               if(s->floating || (s->*(ptrs.packing)).gravity==0)
+               if(s->floating || (s.get()->*(ptrs.packing)).gravity==0)
                {
                        /* Try to keep the widget as close to a target position as possible.
                        Since linear programs can't express absolute values directly, use two
                        opposing slack variables that are optimized for a low value. */
-                       float a = (s->*(ptrs.packing)).gravity*0.5+0.5;
+                       float a = (s.get()->*(ptrs.packing)).gravity*0.5+0.5;
                        LinearProgram::Row row = linprog.add_row();
                        row[s->index*5] = 1;
                        row[s->index*5+1] = a;
@@ -449,7 +442,7 @@ void Layout::solve_constraints(int dir, SolveMode mode)
        if(mode==AUTOSIZE)
        {
                autosize_geom.*(ptrs.dim) = 0;
-               for(const Slot *s: slots)
+               for(const unique_ptr<Slot> &s: slots)
                        if(s->index>=0)
                        {
                                int high_edge = linprog.get_variable(s->index*5)+linprog.get_variable(s->index*5+1);
@@ -458,7 +451,7 @@ void Layout::solve_constraints(int dir, SolveMode mode)
        }
        else
        {
-               for(Slot *s: slots)
+               for(const unique_ptr<Slot> &s: slots)
                        if(s->index>=0)
                        {
                                s->geom.*(ptrs.pos) = linprog.get_variable(s->index*5);
index 2d34b7ad43b3e7be75c973540606ba0337a2cbbb..274660c6464b5da55e3506bd370e77c3377a8f4c 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GLTK_LAYOUT_H_
 #define MSP_GLTK_LAYOUT_H_
 
+#include <memory>
 #include <vector>
 #include <sigc++/trackable.h>
 #include <msp/strings/lexicalcast.h>
@@ -172,7 +173,7 @@ private:
        struct Pointers;
 
        Container *container = nullptr;
-       std::vector<Slot *> slots;
+       std::vector<std::unique_ptr<Slot>> slots;
        std::size_t n_active_slots = 0;
        std::size_t n_slack_vars[2] = { 0, 0 };
        Sides margin{ 8 };
@@ -184,8 +185,6 @@ private:
        static Pointers pointers[2];
 
 public:
-       ~Layout();
-
        void set_container(Container &);
        void set_margin(const Sides &);
        const Sides &get_margin() const { return margin; }
index 4b6854f72a91fd5cf1785af6adf2a76b70dc1b25..a028571c5a1eabd0f56c43a8367b65be7b8b048d 100644 (file)
@@ -20,15 +20,19 @@ incompatible_data::incompatible_data(const type_info &ti):
 { }
 
 
-List::List():
-       List(*new BasicListData<string>)
+List::List(unique_ptr<ListData> d):
+       List(*d)
 {
-       own_data = true;
+       own_data = move(d);
 }
 
+List::List():
+       List(make_unique<BasicListData<string>>())
+{ }
+
 List::List(ListData &d):
        data(&d),
-       observer(new DataObserver(*this))
+       observer(make_unique<DataObserver>(*this))
 {
        input_type = INPUT_NAVIGATION;
 
@@ -37,14 +41,6 @@ List::List(ListData &d):
        slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
 }
 
-List::~List()
-{
-       delete item_factory;
-       delete observer;
-       if(own_data)
-               delete data;
-}
-
 void List::autosize_special(const Part &part, Geometry &ageom) const
 {
        if(part.get_name()=="items")
@@ -89,23 +85,16 @@ void List::set_data(ListData &d)
        if(item_factory)
                item_factory->set_data(d);
 
-       delete observer;
-       if(own_data)
-               delete data;
+       observer.reset();
+       own_data.reset();
 
        data = &d;
-       own_data = false;
-       observer = new DataObserver(*this);
+       observer = make_unique<DataObserver>(*this);
 
-       for(Item *i: items)
-               delete i;
        items.clear();
        size_t n_items = data->size();
        for(size_t i=0; i<n_items; ++i)
-       {
-               Item *item = create_item(i);
-               items.push_back(item);
-       }
+               items.emplace_back(create_item(i));
 
        items_changed();
 }
@@ -116,18 +105,18 @@ void List::items_changed()
        mark_rebuild();
 }
 
-List::Item *List::create_item(size_t index)
+unique_ptr<List::Item> List::create_item(size_t index)
 {
-       Item *item = nullptr;
+       unique_ptr<Item> item;
        if(item_factory)
                item = item_factory->create_item(index);
        else
-               item = new BasicItem(data->get_string(index));
+               item = make_unique<BasicItem>(data->get_string(index));
        if(index==sel_index)
                item->set_active(true);
        add(*item);
        item->autosize();
-       item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item));
+       item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item.get()));
        return item;
 }
 
@@ -170,7 +159,7 @@ void List::set_selected_index(size_t i)
        {
                items[sel_index]->set_active(true);
                if(state&FOCUS)
-                       set_input_focus(items[focus_index]);
+                       set_input_focus(items[focus_index].get());
                signal_item_selected.emit(sel_index);
        }
 }
@@ -178,7 +167,7 @@ void List::set_selected_index(size_t i)
 void List::set_selected_item(Widget *item)
 {
        for(size_t i=rows[first_row].first; (i<items.size() && items[i]->is_visible()); ++i)
-               if(item==items[i])
+               if(item==items[i].get())
                        return set_selected_index(i);
 }
 
@@ -305,7 +294,7 @@ void List::focus_in()
 {
        Container::focus_in();
        if(focus_index!=INVALID_INDEX && items[focus_index]->is_visible())
-               set_input_focus(items[focus_index]);
+               set_input_focus(items[focus_index].get());
        else
        {
                if(sel_index!=INVALID_INDEX && items[sel_index]->is_visible())
@@ -372,7 +361,7 @@ void List::set_focus_index(size_t i)
        {
                scroll_to_focus();
                if(state&FOCUS)
-                       set_input_focus(items[focus_index]);
+                       set_input_focus(items[focus_index].get());
        }
 }
 
@@ -533,8 +522,7 @@ void List::DataObserver::item_added(size_t i)
        adjust_index(list.sel_index, i, 1);
        adjust_index(list.focus_index, i, 1);
 
-       Item *item = list.create_item(i);
-       list.items.insert(list.items.begin()+i, item);
+       list.items.insert(list.items.begin()+i, list.create_item(i));
        list.items_changed();
 }
 
@@ -544,7 +532,6 @@ void List::DataObserver::item_removed(size_t i)
        adjust_index(list.sel_index, i, -1);
        adjust_index(list.focus_index, i, -1);
 
-       delete list.items[i];
        list.items.erase(list.items.begin()+i);
        list.items_changed();
 
@@ -556,8 +543,6 @@ void List::DataObserver::cleared()
 {
        list.sel_index = INVALID_INDEX;
        list.focus_index = INVALID_INDEX;
-       for(Item *i: list.items)
-               delete i;
        list.items.clear();
        list.items_changed();
 
@@ -566,9 +551,8 @@ void List::DataObserver::cleared()
 
 void List::DataObserver::refresh_item(size_t i)
 {
-       delete list.items[i];
-       // Avoid stale pointer while create_item is executing
-       list.items[i] = nullptr;
+       // Destroy the old item before calling create_item
+       list.items[i].reset();
        list.items[i] = list.create_item(i);
        list.items_changed();
 }
@@ -584,7 +568,7 @@ void List::Item::autosize_special(const Part &part, Geometry &ageom) const
        if(part.get_name()=="children")
        {
                const Sides &margin = part.get_margin();
-               for(const Child *c: children)
+               for(const unique_ptr<Child> &c: children)
                {
                        Geometry cgeom;
                        c->widget->autosize(cgeom);
@@ -603,7 +587,7 @@ void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
 {
        if(part.get_name()=="children")
        {
-               for(const Child *c: children)
+               for(const unique_ptr<Child> &c: children)
                        c->widget->render(renderer);
        }
 }
@@ -630,7 +614,7 @@ void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
                widths.resize(children.size(), 0);
 
        size_t n = 0;
-       for(const Child *c: children)
+       for(const unique_ptr<Child> &c: children)
        {
                Geometry cgeom;
                c->widget->autosize(cgeom);
@@ -652,7 +636,7 @@ void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
        const Sides &margin = part->get_margin();
        int x = margin.left;
        size_t n = 0;
-       for(const Child *c: children)
+       for(const unique_ptr<Child> &c: children)
        {
                c->widget->set_position(x, margin.bottom);
                x += widths[n++];
@@ -664,14 +648,14 @@ void List::MultiColumnItem::on_style_change()
        if(!style)
                return;
 
-       for(const Child *c: children)
+       for(const unique_ptr<Child> &c: children)
                c->widget->autosize();
 
        vector<unsigned> widths;
        List *list = static_cast<List *>(parent);
-       for(Item *i: list->items)
-               if(i!=this)
-                       if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(i))
+       for(const unique_ptr<Item> &i: list->items)
+               if(i.get()!=this)
+                       if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(i.get()))
                                mci->check_widths(widths);
 
        vector<unsigned> self_widths(widths);
@@ -682,8 +666,8 @@ void List::MultiColumnItem::on_style_change()
 
        if(update_all)
        {
-               for(Item *i: list->items)
-                       if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(i))
+               for(const unique_ptr<Item> &i: list->items)
+                       if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(i.get()))
                                mci->set_widths(self_widths);
        }
 
index 72b5b8890668fdb0c31946c9e17632f7569a6528..5fa687ddbc8ec0ff9712b4fa48baa465373fc897 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GLTK_LIST_H_
 #define MSP_GLTK_LIST_H_
 
+#include <memory>
 #include <stdexcept>
 #include <typeinfo>
 #include <sigc++/signal.h>
@@ -117,7 +118,7 @@ private:
                virtual ~ItemFactory() = default;
 
                virtual void set_data(const ListData &) = 0;
-               virtual Item *create_item(std::size_t) const = 0;
+               virtual std::unique_ptr<Item> create_item(std::size_t) const = 0;
        };
 
        template<typename I>
@@ -139,9 +140,9 @@ private:
                                throw incompatible_data(typeid(ValueType));
                }
 
-               Item *create_item(std::size_t i) const override
+               std::unique_ptr<Item> create_item(std::size_t i) const override
                {
-                       return new I(data->get(i));
+                       return std::make_unique<I>(data->get(i));
                }
        };
 
@@ -158,10 +159,10 @@ public:
        sigc::signal<void> signal_selection_cleared;
 
 private:
+       std::unique_ptr<ListData> own_data;
        ListData *data = nullptr;
-       bool own_data = false;
-       DataObserver *observer = nullptr;
-       ItemFactory *item_factory = nullptr;
+       std::unique_ptr<DataObserver> observer = nullptr;
+       std::unique_ptr<ItemFactory> item_factory = nullptr;
        ViewMode view_mode = LIST;
        std::size_t sel_index = INVALID_INDEX;
        std::size_t focus_index = INVALID_INDEX;
@@ -176,13 +177,13 @@ private:
        int drag_start_y = 0;
 
        VSlider slider;
-       std::vector<Item *> items;
+       std::vector<std::unique_ptr<Item>> items;
        std::vector<Row> rows;
 
+       List(std::unique_ptr<ListData>);
 public:
        List();
        List(ListData &);
-       virtual ~List();
 
        const char *get_class() const override { return "list"; }
 
@@ -198,14 +199,9 @@ private:
 
 public:
        template<typename I>
-       void set_item_type()
-       {
-               ItemFactory *f = new TypedItemFactory<I>(*data);
-               delete item_factory;
-               item_factory = f;
-       }
+       void set_item_type() { item_factory = std::make_unique<TypedItemFactory<I>>(*data); }
 private:
-       Item *create_item(std::size_t);
+       std::unique_ptr<Item> create_item(std::size_t);
 
 public:
        void set_view_mode(ViewMode);
index 56aa2dcb5545003fd3feb2975be9fd451bc029ac..d4e737bee7de6e9b48ef459b02f35f111a8e453f 100644 (file)
@@ -1,6 +1,5 @@
 #include <msp/core/algorithm.h>
 #include <msp/core/maputils.h>
-#include <msp/core/refptr.h>
 #include "button.h"
 #include "column.h"
 #include "draghandle.h"
@@ -32,17 +31,11 @@ Panel::Panel()
        input_type = INPUT_NAVIGATION;
 }
 
-Panel::~Panel()
-{
-       delete layout;
-       layout = nullptr;
-}
-
 Layout &Panel::get_or_create_layout()
 {
        if(!layout)
        {
-               layout = new Layout;
+               layout = make_unique<Layout>();
                layout->set_container(*this);
        }
 
@@ -59,7 +52,7 @@ void Panel::render_special(const Part &part, GL::Renderer &renderer) const
 {
        if(part.get_name()=="children")
        {
-               for(const Child *c: children)
+               for(const unique_ptr<Child> &c: children)
                        if(c->widget->is_visible())
                                c->widget->render(renderer);
        }
@@ -140,7 +133,7 @@ Widget *Panel::find_next_child(int origin_x, int origin_y, int origin_dim, int n
 {
        Widget *sibling = nullptr;
        int best_score = 0;
-       for(const Child *c: children)
+       for(const unique_ptr<Child> &c: children)
        {
                if(c->widget==input_focus || !c->widget->is_focusable())
                        continue;
@@ -300,7 +293,7 @@ void Panel::Loader::layout()
 template<>
 void Panel::Loader::unnamed_child<Panel>()
 {
-       RefPtr<Panel> pnl = new Panel();
+       unique_ptr<Panel> pnl = make_unique<Panel>();
        load_sub(*pnl, wdg_map);
        obj.add(*pnl.get());
        last_widget = pnl.release();
index 43c0f41b64d91a0241d9211f761e5702f9d5a47e..3abfee1cadb072c74f9f0b4229fda6acfb98b6cd 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GLTK_PANEL_H_
 #define MSP_GLTK_PANEL_H_
 
+#include <memory>
 #include <msp/core/typeregistry.h>
 #include "container.h"
 #include "layout.h"
@@ -66,14 +67,13 @@ private:
 
 protected:
        std::vector<Widget *> nav_order;
-       Layout *layout = nullptr;
+       std::unique_ptr<Layout> layout;
 
        static TypeRegistry<Loader::AddChildType, Loader &> widget_registry;
        static bool widget_registry_init_done;
 
 public:
        Panel();
-       virtual ~Panel();
 
        template<typename T>
        static void register_child_type(const std::string &);
@@ -115,7 +115,7 @@ void Panel::Loader::child(const std::string &n)
 template<typename T>
 void Panel::Loader::unnamed_child()
 {
-       RefPtr<T> chl = new T();
+       std::unique_ptr<T> chl = std::make_unique<T>();
        load_sub(*chl);
        obj.add(*chl.get());
        last_widget = chl.release();
index 8b425c2b0a5f255c553d91e67fcca67497c6dd0a..560db298e8edca248b42c32bbc0926f370b70e68 100644 (file)
@@ -6,30 +6,6 @@ using namespace std;
 namespace Msp {
 namespace GLtk {
 
-CachedPart::CachedPart(CachedPart &&other):
-       part(other.part),
-       texture(other.texture),
-       mesh(other.mesh)
-{
-       other.mesh = nullptr;
-}
-
-CachedPart &CachedPart::operator=(CachedPart &&other)
-{
-       delete mesh;
-       part = other.part;
-       texture = other.texture;
-       mesh = other.mesh;
-       other.mesh = nullptr;
-       return *this;
-}
-
-CachedPart::~CachedPart()
-{
-       delete mesh;
-}
-
-
 void PartCache::begin_rebuild()
 {
        if(rebuilding)
@@ -80,7 +56,7 @@ GL::Mesh &PartCache::create_mesh(const Part &part, const GL::Texture2D &tex)
        {
                current = parts.insert(next, CachedPart());
                current->texture = &tex;
-               current->mesh = new GL::Mesh((GL::TEXCOORD2, GL::COLOR4,GL::UNSIGNED_BYTE, GL::VERTEX2));
+               current->mesh = make_unique<GL::Mesh>((GL::TEXCOORD2, GL::COLOR4,GL::UNSIGNED_BYTE, GL::VERTEX2));
        }
        else
                current->mesh->clear();
index 71684738f0980922de8db5346d88249f53a058b5..a10efdd084e2a9b5c463fd3ac555886635bb7c36 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GLTK_PARTCACHE_H_
 #define MSP_GLTK_PARTCACHE_H_
 
+#include <memory>
 #include <msp/gl/mesh.h>
 #include <msp/gl/texture2d.h>
 #include "mspgltk_api.h"
@@ -14,12 +15,7 @@ struct CachedPart
 {
        const Part *part = nullptr;
        const GL::Texture2D *texture = nullptr;
-       GL::Mesh *mesh = nullptr;
-
-       CachedPart() = default;
-       CachedPart(CachedPart &&);
-       CachedPart &operator=(CachedPart &&);
-       ~CachedPart();
+       std::unique_ptr<GL::Mesh> mesh;
 };
 
 class MSPGLTK_API PartCache
index 078b58b71696559b16b12267f0e39c8759852373..dc678a9480d74aa276039d65b5b0b5cf1c0ed8d5 100644 (file)
@@ -22,18 +22,13 @@ Resources::Resources()
 Resources::Resources(const FS::Path &fn):
        Resources()
 {
-       dir_src = new DataFile::DirectorySource;
+       dir_src = make_unique<DataFile::DirectorySource>();
        dir_src->add_directory(FS::dirname(fn));
        add_source(*dir_src);
 
        DataFile::load(*this, fn.str());
 }
 
-Resources::~Resources()
-{
-       delete dir_src;
-}
-
 const GL::Font &Resources::get_default_font() const
 {
        if(!default_font)
@@ -47,18 +42,18 @@ GL::Module *Resources::create_module(const string &name)
        if(name!="ui.glsl")
                return nullptr;
 
-       GL::Module *mod = nullptr;
+       unique_ptr<GL::Module> mod;
        if(GL::get_backend_api()==GL::VULKAN)
-               mod = new GL::SpirVModule;
+               mod = make_unique<GL::SpirVModule>();
        else
-               mod = new GL::GlslModule;
+               mod = make_unique<GL::GlslModule>();
 
        mod->set_source("import msp_interface; import common;\n"
                "uniform sampler2D ui_tex;\n"
                "#pragma MSP stage(fragment)\n"
                "void main() { frag_color = texture(ui_tex, texcoord.xy)*color; }\n");
 
-       return mod;
+       return mod.release();
 }
 
 GL::Program *Resources::create_program(const string &name)
@@ -74,10 +69,10 @@ GL::Sampler *Resources::create_sampler(const string &name)
        static const Regex r_name("^(linear|nearest)_clamp.samp$");
        if(RegMatch m = r_name.match(name))
        {
-               GL::Sampler *sampler = new GL::Sampler;
+               unique_ptr<GL::Sampler> sampler = make_unique<GL::Sampler>();
                sampler->set_filter(lexical_cast<GL::TextureFilter>(toupper(m.group(1).str)));
                sampler->set_wrap(GL::CLAMP_TO_EDGE);
-               return sampler;
+               return sampler.release();
        }
 
        return nullptr;
@@ -87,15 +82,15 @@ GL::Texture2D *Resources::create_texture(const string &name)
 {
        string ext = FS::extpart(name);
        if(ext==".png" || ext==".jpg")
-               if(IO::Seekable *io = open_raw(name))
+               if(auto io = unique_ptr<IO::Seekable>(open_raw(name)))
                {
                        Graphics::Image image;
                        image.load_io(*io);
-                       delete io;
+                       io.reset();
 
-                       GL::Texture2D *tex = new GL::Texture2D;
+                       unique_ptr<GL::Texture2D> tex = make_unique<GL::Texture2D>();
                        tex->image(image);
-                       return tex;
+                       return tex.release();
                }
 
        return nullptr;
@@ -117,7 +112,7 @@ void Resources::Loader::default_font(const string &name)
 
 void Resources::Loader::font(const string &name)
 {
-       RefPtr<GL::Font> fnt = new GL::Font;
+       unique_ptr<GL::Font> fnt = make_unique<GL::Font>();
        load_sub(*fnt, res);
        res.add(name, fnt.get());
        if(!res.default_font)
index 31c573470ec9015e181849a909376796b2f3abb2..7569119a35109d6d46c6b94501b70bc7eabd9472 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GLTK_RESOURCES_H_
 #define MSP_GLTK_RESOURCES_H_
 
+#include <memory>
 #include <msp/gl/font.h>
 #include <msp/gl/module.h>
 #include <msp/gl/program.h>
@@ -39,12 +40,11 @@ public:
 private:
        FS::Path path;
        GL::Font *default_font = nullptr;
-       DataFile::DirectorySource *dir_src = nullptr;
+       std::unique_ptr<DataFile::DirectorySource> dir_src;
 
 public:
        Resources();
        Resources(const FS::Path &);
-       ~Resources();
 
        const GL::Font &get_default_font() const;
        const GL::Sampler &get_default_sampler() const;
index 167b3f798c8963768b11c320cb9d82bf9dc5d916..8d32c467b26eb5924e1825d6d51ff37d01de65a7 100644 (file)
@@ -7,15 +7,22 @@
 #include "root.h"
 #include "systemkeyboardinput.h"
 
+using namespace std;
+
 namespace Msp {
 namespace GLtk {
 
-Root::Root(Resources &r, Graphics::Window &window):
-       Root(r, &window, new Input::Keyboard(window), new Input::Mouse(window), nullptr)
+Root::Root(Resources &r, Graphics::Window &window, unique_ptr<Input::Keyboard> k, unique_ptr<Input::Mouse> m):
+       Root(r, &window, k.get(), m.get(), nullptr)
 {
-       own_input = true;
+       own_input[0] = move(k);
+       own_input[1] = move(m);
 }
 
+Root::Root(Resources &r, Graphics::Window &window):
+       Root(r, window, make_unique<Input::Keyboard>(window), make_unique<Input::Mouse>(window))
+{ }
+
 Root::Root(Resources &r, Graphics::Window *window, Input::Keyboard *k, Input::Mouse *m, Input::Touchscreen *t):
        resources(r),
        keyboard(k),
@@ -47,7 +54,7 @@ Root::Root(Resources &r, Graphics::Window *window, Input::Keyboard *k, Input::Mo
        }
 
        if(keyboard && !input_method)
-               input_method = new SystemKeyboardInput(*this, *keyboard);
+               input_method = make_unique<SystemKeyboardInput>(*this, *keyboard);
 
        if(touchscreen)
        {
@@ -57,16 +64,6 @@ Root::Root(Resources &r, Graphics::Window *window, Input::Keyboard *k, Input::Mo
        }
 }
 
-Root::~Root()
-{
-       delete input_method;
-       if(own_input)
-       {
-               delete keyboard;
-               delete mouse;
-       }
-}
-
 void Root::tick()
 {
        Time::TimeStamp t = Time::now();
index c71d50d6f7577d4ec3e710149c70115d50850ae3..29fff237430c156e0a2b94ef3ad89bbb269d38fb 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GLTK_ROOT_H_
 #define MSP_GLTK_ROOT_H_
 
+#include <memory>
 #include <sigc++/trackable.h>
 #include <msp/gl/blend.h>
 #include <msp/gl/camera.h>
@@ -33,10 +34,10 @@ public:
 private:
        Resources &resources;
        Input::Keyboard *keyboard = nullptr;
-       InputMethod *input_method = nullptr;
+       std::unique_ptr<InputMethod> input_method;
        Input::Mouse *mouse = nullptr;
        Input::Touchscreen *touchscreen = nullptr;
-       bool own_input = false;
+       std::unique_ptr<Input::Device> own_input[2];
        Label *lbl_tooltip = nullptr;
        int pointer_x = 0;
        int pointer_y = 0;
@@ -47,6 +48,8 @@ private:
        Msp::GL::Program *shprog = nullptr;
        Msp::GL::Blend blend;
 
+       Root(Resources &, Graphics::Window &, std::unique_ptr<Input::Keyboard>, std::unique_ptr<Input::Mouse>);
+
 public:
        /** Creates a Root widget for a window.  The geometry is set to match the
        window's size, and input devices are created automatically. */
@@ -55,7 +58,6 @@ public:
        /** Creates a Root widget with custom input devices.  If window is not null,
        it is used to set the widget's initial geometry. */
        Root(Resources &, Graphics::Window *, Input::Keyboard *, Input::Mouse *, Input::Touchscreen * = nullptr);
-       virtual ~Root();
 
        const char *get_class() const override { return "root"; }