diff options
author | Max Kellermann <max@musicpd.org> | 2019-02-20 20:32:11 +0100 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2019-02-20 20:43:31 +0100 |
commit | 6c28adbcd2e0e9b78d7d68193f435e80548565fe (patch) | |
tree | ee19b60447a91cd04683b1e55507c6a5ec2afcd7 /src | |
parent | 2125e3ed5708898f18da21a33ddd7b4d3ea9c41e (diff) |
db/Plugin: use std::unique_ptr<> to manage Database pointers
Diffstat (limited to 'src')
-rw-r--r-- | src/Main.cxx | 2 | ||||
-rw-r--r-- | src/db/Configured.cxx | 5 | ||||
-rw-r--r-- | src/db/Configured.hxx | 7 | ||||
-rw-r--r-- | src/db/DatabaseGlue.cxx | 5 | ||||
-rw-r--r-- | src/db/DatabaseGlue.hxx | 7 | ||||
-rw-r--r-- | src/db/DatabasePlugin.hxx | 13 | ||||
-rw-r--r-- | src/db/Ptr.hxx | 29 | ||||
-rw-r--r-- | src/db/plugins/ProxyDatabasePlugin.cxx | 12 | ||||
-rw-r--r-- | src/db/plugins/simple/Directory.cxx | 2 | ||||
-rw-r--r-- | src/db/plugins/simple/Directory.hxx | 4 | ||||
-rw-r--r-- | src/db/plugins/simple/SimpleDatabasePlugin.cxx | 34 | ||||
-rw-r--r-- | src/db/plugins/simple/SimpleDatabasePlugin.hxx | 18 | ||||
-rw-r--r-- | src/db/plugins/upnp/UpnpDatabasePlugin.cxx | 14 | ||||
-rw-r--r-- | src/db/update/Service.cxx | 4 |
14 files changed, 89 insertions, 67 deletions
diff --git a/src/Main.cxx b/src/Main.cxx index 77b20d82d..89b6eaf8f 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -188,7 +188,7 @@ glue_db_init_and_load(const ConfigData &config) instance->database = CreateConfiguredDatabase(config, instance->event_loop, instance->io_thread.GetEventLoop(), - *instance); + *instance).release(); if (instance->database == nullptr) return true; diff --git a/src/db/Configured.cxx b/src/db/Configured.cxx index ab2bde405..8b47114da 100644 --- a/src/db/Configured.cxx +++ b/src/db/Configured.cxx @@ -1,5 +1,5 @@ /* - * Copyright 2003-2018 The Music Player Daemon Project + * Copyright 2003-2019 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -19,6 +19,7 @@ #include "Configured.hxx" #include "DatabaseGlue.hxx" +#include "Interface.hxx" #include "config/Data.hxx" #include "config/Param.hxx" #include "config/Block.hxx" @@ -26,7 +27,7 @@ #include "fs/StandardDirectory.hxx" #include "util/RuntimeError.hxx" -Database * +DatabasePtr CreateConfiguredDatabase(const ConfigData &config, EventLoop &main_event_loop, EventLoop &io_event_loop, DatabaseListener &listener) diff --git a/src/db/Configured.hxx b/src/db/Configured.hxx index ae776d38f..8a2fef637 100644 --- a/src/db/Configured.hxx +++ b/src/db/Configured.hxx @@ -1,5 +1,5 @@ /* - * Copyright 2003-2018 The Music Player Daemon Project + * Copyright 2003-2019 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -20,10 +20,11 @@ #ifndef MPD_DB_CONFIG_HXX #define MPD_DB_CONFIG_HXX +#include "Ptr.hxx" + struct ConfigData; class EventLoop; class DatabaseListener; -class Database; /** * Read database configuration settings and create a #Database @@ -32,7 +33,7 @@ class Database; * * Throws #std::runtime_error on error. */ -Database * +DatabasePtr CreateConfiguredDatabase(const ConfigData &config, EventLoop &main_event_loop, EventLoop &io_event_loop, DatabaseListener &listener); diff --git a/src/db/DatabaseGlue.cxx b/src/db/DatabaseGlue.cxx index 9f001467a..faedcb6de 100644 --- a/src/db/DatabaseGlue.cxx +++ b/src/db/DatabaseGlue.cxx @@ -1,5 +1,5 @@ /* - * Copyright 2003-2018 The Music Player Daemon Project + * Copyright 2003-2019 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -18,13 +18,14 @@ */ #include "DatabaseGlue.hxx" +#include "Interface.hxx" #include "Registry.hxx" #include "DatabaseError.hxx" #include "util/RuntimeError.hxx" #include "config/Block.hxx" #include "DatabasePlugin.hxx" -Database * +DatabasePtr DatabaseGlobalInit(EventLoop &main_event_loop, EventLoop &io_event_loop, DatabaseListener &listener, diff --git a/src/db/DatabaseGlue.hxx b/src/db/DatabaseGlue.hxx index fae76e062..43b80692a 100644 --- a/src/db/DatabaseGlue.hxx +++ b/src/db/DatabaseGlue.hxx @@ -1,5 +1,5 @@ /* - * Copyright 2003-2018 The Music Player Daemon Project + * Copyright 2003-2019 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -20,12 +20,11 @@ #ifndef MPD_DATABASE_GLUE_HXX #define MPD_DATABASE_GLUE_HXX -#include "util/Compiler.h" +#include "Ptr.hxx" struct ConfigBlock; class EventLoop; class DatabaseListener; -class Database; /** * Initialize the database library. @@ -34,7 +33,7 @@ class Database; * * @param block the database configuration block */ -Database * +DatabasePtr DatabaseGlobalInit(EventLoop &main_event_loop, EventLoop &io_event_loop, DatabaseListener &listener, diff --git a/src/db/DatabasePlugin.hxx b/src/db/DatabasePlugin.hxx index 819f22991..f1e34e437 100644 --- a/src/db/DatabasePlugin.hxx +++ b/src/db/DatabasePlugin.hxx @@ -1,5 +1,5 @@ /* - * Copyright 2003-2018 The Music Player Daemon Project + * Copyright 2003-2019 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -26,10 +26,11 @@ #ifndef MPD_DATABASE_PLUGIN_HXX #define MPD_DATABASE_PLUGIN_HXX +#include "Ptr.hxx" + struct ConfigBlock; class EventLoop; class DatabaseListener; -class Database; struct DatabasePlugin { /** @@ -52,10 +53,10 @@ struct DatabasePlugin { * @param io_event_loop the #EventLoop running on the * #EventThread, i.e. the one used for background I/O */ - Database *(*create)(EventLoop &main_event_loop, - EventLoop &io_event_loop, - DatabaseListener &listener, - const ConfigBlock &block); + DatabasePtr (*create)(EventLoop &main_event_loop, + EventLoop &io_event_loop, + DatabaseListener &listener, + const ConfigBlock &block); constexpr bool RequireStorage() const { return flags & FLAG_REQUIRE_STORAGE; diff --git a/src/db/Ptr.hxx b/src/db/Ptr.hxx new file mode 100644 index 000000000..b6ea63200 --- /dev/null +++ b/src/db/Ptr.hxx @@ -0,0 +1,29 @@ +/* + * Copyright 2003-2019 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_DATABASE_PTR_HXX +#define MPD_DATABASE_PTR_HXX + +#include <memory> + +class Database; + +typedef std::unique_ptr<Database> DatabasePtr; + +#endif diff --git a/src/db/plugins/ProxyDatabasePlugin.cxx b/src/db/plugins/ProxyDatabasePlugin.cxx index 9c5d5e9f3..c4fdce0a1 100644 --- a/src/db/plugins/ProxyDatabasePlugin.cxx +++ b/src/db/plugins/ProxyDatabasePlugin.cxx @@ -112,10 +112,10 @@ public: ProxyDatabase(EventLoop &_loop, DatabaseListener &_listener, const ConfigBlock &block); - static Database *Create(EventLoop &main_event_loop, - EventLoop &io_event_loop, - DatabaseListener &listener, - const ConfigBlock &block); + static DatabasePtr Create(EventLoop &main_event_loop, + EventLoop &io_event_loop, + DatabaseListener &listener, + const ConfigBlock &block); void Open() override; void Close() noexcept override; @@ -440,12 +440,12 @@ ProxyDatabase::ProxyDatabase(EventLoop &_loop, DatabaseListener &_listener, { } -Database * +DatabasePtr ProxyDatabase::Create(EventLoop &loop, EventLoop &, DatabaseListener &listener, const ConfigBlock &block) { - return new ProxyDatabase(loop, listener, block); + return std::make_unique<ProxyDatabase>(loop, listener, block); } void diff --git a/src/db/plugins/simple/Directory.cxx b/src/db/plugins/simple/Directory.cxx index 8e6ad02aa..a1f2d15e4 100644 --- a/src/db/plugins/simple/Directory.cxx +++ b/src/db/plugins/simple/Directory.cxx @@ -45,8 +45,6 @@ Directory::Directory(std::string &&_path_utf8, Directory *_parent) noexcept Directory::~Directory() noexcept { - delete mounted_database; - songs.clear_and_dispose(Song::Disposer()); children.clear_and_dispose(DeleteDisposer()); } diff --git a/src/db/plugins/simple/Directory.hxx b/src/db/plugins/simple/Directory.hxx index b8d9813b3..c8a429e4f 100644 --- a/src/db/plugins/simple/Directory.hxx +++ b/src/db/plugins/simple/Directory.hxx @@ -23,6 +23,7 @@ #include "util/Compiler.h" #include "db/Visitor.hxx" #include "db/PlaylistVector.hxx" +#include "db/Ptr.hxx" #include "Song.hxx" #include <boost/intrusive/list.hpp> @@ -43,7 +44,6 @@ static constexpr unsigned DEVICE_INARCHIVE = -1; static constexpr unsigned DEVICE_CONTAINER = -2; class SongFilter; -class Database; struct Directory { static constexpr auto link_mode = boost::intrusive::normal_link; @@ -96,7 +96,7 @@ struct Directory { * If this is not nullptr, then this directory does not really * exist, but is a mount point for another #Database. */ - Database *mounted_database = nullptr; + DatabasePtr mounted_database; public: Directory(std::string &&_path_utf8, Directory *_parent) noexcept; diff --git a/src/db/plugins/simple/SimpleDatabasePlugin.cxx b/src/db/plugins/simple/SimpleDatabasePlugin.cxx index 056091110..112cb9fdd 100644 --- a/src/db/plugins/simple/SimpleDatabasePlugin.cxx +++ b/src/db/plugins/simple/SimpleDatabasePlugin.cxx @@ -1,5 +1,5 @@ /* - * Copyright 2003-2018 The Music Player Daemon Project + * Copyright 2003-2019 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -84,12 +84,12 @@ inline SimpleDatabase::SimpleDatabase(AllocatedPath &&_path, prefixed_light_song(nullptr) { } -Database * +DatabasePtr SimpleDatabase::Create(EventLoop &, EventLoop &, gcc_unused DatabaseListener &listener, const ConfigBlock &block) { - return new SimpleDatabase(block); + return std::make_unique<SimpleDatabase>(block); } void @@ -390,13 +390,13 @@ SimpleDatabase::Save() } void -SimpleDatabase::Mount(const char *uri, Database *db) +SimpleDatabase::Mount(const char *uri, DatabasePtr db) { #if !CLANG_CHECK_VERSION(3,6) /* disabled on clang due to -Wtautological-pointer-compare */ assert(uri != nullptr); - assert(db != nullptr); #endif + assert(db != nullptr); assert(*uri != 0); ScopeDatabaseLock protect; @@ -411,7 +411,7 @@ SimpleDatabase::Mount(const char *uri, Database *db) "Parent not found"); Directory *mnt = r.directory->CreateChild(r.uri); - mnt->mounted_database = db; + mnt->mounted_database = std::move(db); } static constexpr bool @@ -441,27 +441,21 @@ SimpleDatabase::Mount(const char *local_uri, const char *storage_uri) #ifndef ENABLE_ZLIB constexpr bool compress = false; #endif - auto db = new SimpleDatabase(cache_path / name_fs, - compress); - try { - db->Open(); - } catch (...) { - delete db; - throw; - } + auto db = std::make_unique<SimpleDatabase>(cache_path / name_fs, + compress); + db->Open(); // TODO: update the new database instance? try { - Mount(local_uri, db); + Mount(local_uri, std::move(db)); } catch (...) { db->Close(); - delete db; throw; } } -inline Database * +inline DatabasePtr SimpleDatabase::LockUmountSteal(const char *uri) noexcept { ScopeDatabaseLock protect; @@ -470,8 +464,7 @@ SimpleDatabase::LockUmountSteal(const char *uri) noexcept if (r.uri != nullptr || !r.directory->IsMount()) return nullptr; - Database *db = r.directory->mounted_database; - r.directory->mounted_database = nullptr; + auto db = std::move(r.directory->mounted_database); r.directory->Delete(); return db; @@ -480,12 +473,11 @@ SimpleDatabase::LockUmountSteal(const char *uri) noexcept bool SimpleDatabase::Unmount(const char *uri) noexcept { - Database *db = LockUmountSteal(uri); + auto db = LockUmountSteal(uri); if (db == nullptr) return false; db->Close(); - delete db; return true; } diff --git a/src/db/plugins/simple/SimpleDatabasePlugin.hxx b/src/db/plugins/simple/SimpleDatabasePlugin.hxx index f1493b2e8..53b07cb55 100644 --- a/src/db/plugins/simple/SimpleDatabasePlugin.hxx +++ b/src/db/plugins/simple/SimpleDatabasePlugin.hxx @@ -1,5 +1,5 @@ /* - * Copyright 2003-2018 The Music Player Daemon Project + * Copyright 2003-2019 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -21,6 +21,7 @@ #define MPD_SIMPLE_DATABASE_PLUGIN_HXX #include "db/Interface.hxx" +#include "db/Ptr.hxx" #include "fs/AllocatedPath.hxx" #include "song/LightSong.hxx" #include "util/Manual.hxx" @@ -68,15 +69,14 @@ class SimpleDatabase : public Database { mutable unsigned borrowed_song_count; #endif +public: SimpleDatabase(const ConfigBlock &block); - SimpleDatabase(AllocatedPath &&_path, bool _compress) noexcept; -public: - static Database *Create(EventLoop &main_event_loop, - EventLoop &io_event_loop, - DatabaseListener &listener, - const ConfigBlock &block); + static DatabasePtr Create(EventLoop &main_event_loop, + EventLoop &io_event_loop, + DatabaseListener &listener, + const ConfigBlock &block); gcc_pure Directory &GetRoot() noexcept { @@ -99,7 +99,7 @@ public: * success, this object gains ownership of the given #Database */ gcc_nonnull_all - void Mount(const char *uri, Database *db); + void Mount(const char *uri, DatabasePtr db); /** * Throws #std::runtime_error on error. @@ -142,7 +142,7 @@ private: */ void Load(); - Database *LockUmountSteal(const char *uri) noexcept; + DatabasePtr LockUmountSteal(const char *uri) noexcept; }; extern const DatabasePlugin simple_db_plugin; diff --git a/src/db/plugins/upnp/UpnpDatabasePlugin.cxx b/src/db/plugins/upnp/UpnpDatabasePlugin.cxx index 6db9da940..4263773a4 100644 --- a/src/db/plugins/upnp/UpnpDatabasePlugin.cxx +++ b/src/db/plugins/upnp/UpnpDatabasePlugin.cxx @@ -1,5 +1,5 @@ /* - * Copyright 2003-2018 The Music Player Daemon Project + * Copyright 2003-2019 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -82,10 +82,10 @@ public: :Database(upnp_db_plugin), event_loop(_event_loop) {} - static Database *Create(EventLoop &main_event_loop, - EventLoop &io_event_loop, - DatabaseListener &listener, - const ConfigBlock &block) noexcept; + static DatabasePtr Create(EventLoop &main_event_loop, + EventLoop &io_event_loop, + DatabaseListener &listener, + const ConfigBlock &block) noexcept; void Open() override; void Close() noexcept override; @@ -146,12 +146,12 @@ private: const UPnPDirObject& dirent) const; }; -Database * +DatabasePtr UpnpDatabase::Create(EventLoop &, EventLoop &io_event_loop, gcc_unused DatabaseListener &listener, const ConfigBlock &) noexcept { - return new UpnpDatabase(io_event_loop); + return std::make_unique<UpnpDatabase>(io_event_loop); } void diff --git a/src/db/update/Service.cxx b/src/db/update/Service.cxx index 377e80147..dafe21850 100644 --- a/src/db/update/Service.cxx +++ b/src/db/update/Service.cxx @@ -94,7 +94,7 @@ UpdateService::CancelMount(const char *uri) cancel_current = next.IsDefined() && next.storage == storage2; } - if (auto *db2 = dynamic_cast<SimpleDatabase *>(lr.directory->mounted_database)) { + if (auto *db2 = dynamic_cast<SimpleDatabase *>(lr.directory->mounted_database.get())) { queue.Erase(*db2); cancel_current |= next.IsDefined() && next.db == db2; } @@ -188,7 +188,7 @@ UpdateService::Enqueue(const char *path, bool discard) /* follow the mountpoint, update the mounted database */ - db2 = dynamic_cast<SimpleDatabase *>(lr.directory->mounted_database); + db2 = dynamic_cast<SimpleDatabase *>(lr.directory->mounted_database.get()); if (db2 == nullptr) throw std::runtime_error("Cannot update this type of database"); |