]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/list.h
Use std::unique_ptr for managing memory
[libs/gltk.git] / source / list.h
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);