filelist: wrap in std::unique_ptr<>
authorMax Kellermann <max@musicpd.org>
Thu, 23 Jan 2020 19:33:21 +0000 (20:33 +0100)
committerMax Kellermann <max@musicpd.org>
Thu, 23 Jan 2020 19:43:43 +0000 (20:43 +0100)
src/FileBrowserPage.cxx
src/FileListPage.cxx
src/FileListPage.hxx
src/LibraryPage.cxx
src/SearchPage.cxx
src/filelist.cxx
src/filelist.hxx

index 64f08f3..35465e7 100644 (file)
@@ -100,9 +100,7 @@ screen_file_load_list(struct mpdclient *c, const char *current_path,
 void
 FileBrowserPage::Reload(struct mpdclient &c)
 {
-       delete filelist;
-
-       filelist = new FileList();
+       filelist = std::make_unique<FileList>();
        if (!current_path.empty())
                /* add a dummy entry for ./.. */
                filelist->emplace_back(nullptr);
index 64df2c6..5db8a74 100644 (file)
 #define HIGHLIGHT  (0x01)
 #endif
 
-FileListPage::~FileListPage() noexcept
+FileListPage::FileListPage(ScreenManager &_screen, WINDOW *_w,
+                          Size size,
+                          const char *_song_format) noexcept
+       :ListPage(_w, size),
+        screen(_screen),
+        song_format(_song_format)
 {
-       delete filelist;
 }
 
+FileListPage::~FileListPage() noexcept = default;
+
 #ifndef NCMPC_MINI
 
 /* sync highlight flags with playlist */
index 6ff6fba..8dac782 100644 (file)
@@ -27,6 +27,8 @@
 
 #include <curses.h>
 
+#include <memory>
+
 struct mpdclient;
 struct MpdQueue;
 class ScreenManager;
@@ -37,16 +39,13 @@ class FileListPage : public ListPage, ListRenderer, ListText {
 protected:
        ScreenManager &screen;
 
-       FileList *filelist = nullptr;
+       std::unique_ptr<FileList> filelist;
        const char *const song_format;
 
 public:
        FileListPage(ScreenManager &_screen, WINDOW *_w,
                     Size size,
-                    const char *_song_format)
-               :ListPage(_w, size),
-                screen(_screen),
-                song_format(_song_format) {}
+                    const char *_song_format) noexcept;
 
        ~FileListPage() noexcept override;
 
index ee83ef9..705a320 100644 (file)
@@ -143,9 +143,7 @@ SongListPage::LoadSongList(struct mpdclient &c)
 {
        auto *connection = c.GetConnection();
 
-       delete filelist;
-
-       filelist = new FileList();
+       filelist = std::make_unique<FileList>();
        /* add a dummy entry for ".." */
        filelist->emplace_back(nullptr);
 
index e90a7a0..c1b5e9e 100644 (file)
@@ -163,8 +163,7 @@ void
 SearchPage::Clear(bool clear_pattern)
 {
        if (filelist) {
-               delete filelist;
-               filelist = new FileList();
+               filelist = std::make_unique<FileList>();
                lw.SetLength(0);
        }
        if (clear_pattern)
@@ -173,11 +172,10 @@ SearchPage::Clear(bool clear_pattern)
        SetDirty();
 }
 
-static FileList *
+static std::unique_ptr<FileList>
 search_simple_query(struct mpd_connection *connection, bool exact_match,
                    int table, const char *local_pattern)
 {
-       FileList *list;
        const LocaleToUtf8 filter_utf8(local_pattern);
 
        if (table == SEARCH_ARTIST_TITLE) {
@@ -197,15 +195,16 @@ search_simple_query(struct mpd_connection *connection, bool exact_match,
 
                mpd_command_list_end(connection);
 
-               list = filelist_new_recv(connection);
+               auto list = filelist_new_recv(connection);
                list->RemoveDuplicateSongs();
+               return list;
        } else if (table == SEARCH_URI) {
                mpd_search_db_songs(connection, exact_match);
                mpd_search_add_uri_constraint(connection, MPD_OPERATOR_DEFAULT,
                                              filter_utf8.c_str());
                mpd_search_commit(connection);
 
-               list = filelist_new_recv(connection);
+               return filelist_new_recv(connection);
        } else {
                mpd_search_db_songs(connection, exact_match);
                mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
@@ -213,10 +212,8 @@ search_simple_query(struct mpd_connection *connection, bool exact_match,
                                              filter_utf8.c_str());
                mpd_search_commit(connection);
 
-               list = filelist_new_recv(connection);
+               return filelist_new_recv(connection);
        }
-
-       return list;
 }
 
 /*-----------------------------------------------------------------------
@@ -224,7 +221,7 @@ search_simple_query(struct mpd_connection *connection, bool exact_match,
  *       Its ugly and MUST be redesigned before the next release!
  *-----------------------------------------------------------------------
  */
-static FileList *
+static std::unique_ptr<FileList>
 search_advanced_query(struct mpd_connection *connection, const char *query)
 {
        advanced_search_mode = false;
@@ -304,23 +301,21 @@ search_advanced_query(struct mpd_connection *connection, const char *query)
        }
 
        mpd_search_commit(connection);
-       auto *fl = filelist_new_recv(connection);
-       if (!mpd_response_finish(connection)) {
-               delete fl;
-               fl = nullptr;
-       }
+       auto fl = filelist_new_recv(connection);
+       if (!mpd_response_finish(connection))
+               fl.reset();
 
        return fl;
 }
 
-static FileList *
+static std::unique_ptr<FileList>
 do_search(struct mpdclient *c, const char *query)
 {
        auto *connection = c->GetConnection();
        if (connection == nullptr)
                return nullptr;
 
-       auto *fl = search_advanced_query(connection, query);
+       auto fl = search_advanced_query(connection, query);
        if (fl != nullptr)
                return fl;
 
@@ -344,10 +339,9 @@ SearchPage::Reload(struct mpdclient &c)
                return;
 
        lw.EnableCursor();
-       delete filelist;
        filelist = do_search(&c, pattern.c_str());
        if (filelist == nullptr)
-               filelist = new FileList();
+               filelist = std::make_unique<FileList>();
        lw.SetLength(filelist->size());
 
        screen_browser_sync_highlights(*filelist, c.playlist);
index 5cfd86e..dc6854c 100644 (file)
@@ -169,10 +169,10 @@ FileList::Receive(struct mpd_connection &connection)
                emplace_back(entity);
 }
 
-FileList *
+std::unique_ptr<FileList>
 filelist_new_recv(struct mpd_connection *connection)
 {
-       auto *filelist = new FileList();
+       auto filelist = std::make_unique<FileList>();
        filelist->Receive(*connection);
        return filelist;
 }
index 1465833..5ecd777 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "util/Compiler.h"
 
+#include <memory>
 #include <vector>
 #include <utility>
 
@@ -113,7 +114,7 @@ public:
  * Creates a new FileList and receives entities from the connection.
  * This does not finish the response, and does not check for errors.
  */
-FileList *
+std::unique_ptr<FileList>
 filelist_new_recv(struct mpd_connection *connection);
 
 #endif