Instance: use std::unique_ptr<> to manage the Database pointer

This commit is contained in:
Max Kellermann 2019-02-20 20:48:20 +01:00
parent 6c28adbcd2
commit 8b5c33cecd
8 changed files with 13 additions and 14 deletions

View File

@ -58,7 +58,7 @@ Instance::~Instance() noexcept
if (database != nullptr) { if (database != nullptr) {
database->Close(); database->Close();
delete database; database.reset();
} }
delete storage; delete storage;

View File

@ -41,7 +41,7 @@ class NeighborGlue;
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
#include "db/DatabaseListener.hxx" #include "db/DatabaseListener.hxx"
class Database; #include "db/Ptr.hxx"
class Storage; class Storage;
class UpdateService; class UpdateService;
#endif #endif
@ -104,7 +104,7 @@ struct Instance final
#endif #endif
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
Database *database; DatabasePtr database;
/** /**
* This is really a #CompositeStorage. To avoid heavy include * This is really a #CompositeStorage. To avoid heavy include
@ -155,7 +155,7 @@ struct Instance final
* music_directory was configured). * music_directory was configured).
*/ */
Database *GetDatabase() { Database *GetDatabase() {
return database; return database.get();
} }
/** /**

View File

@ -188,7 +188,7 @@ glue_db_init_and_load(const ConfigData &config)
instance->database = instance->database =
CreateConfiguredDatabase(config, instance->event_loop, CreateConfiguredDatabase(config, instance->event_loop,
instance->io_thread.GetEventLoop(), instance->io_thread.GetEventLoop(),
*instance).release(); *instance);
if (instance->database == nullptr) if (instance->database == nullptr)
return true; return true;
@ -196,8 +196,7 @@ glue_db_init_and_load(const ConfigData &config)
InitStorage(config, instance->io_thread.GetEventLoop()); InitStorage(config, instance->io_thread.GetEventLoop());
if (instance->storage == nullptr) { if (instance->storage == nullptr) {
delete instance->database; instance->database.reset();
instance->database = nullptr;
LogDefault(config_domain, LogDefault(config_domain,
"Found database setting without " "Found database setting without "
"music_directory - disabling database"); "music_directory - disabling database");
@ -216,7 +215,7 @@ glue_db_init_and_load(const ConfigData &config)
std::throw_with_nested(std::runtime_error("Failed to open database plugin")); std::throw_with_nested(std::runtime_error("Failed to open database plugin"));
} }
auto *db = dynamic_cast<SimpleDatabase *>(instance->database); auto *db = dynamic_cast<SimpleDatabase *>(instance->database.get());
if (db == nullptr) if (db == nullptr)
return true; return true;

View File

@ -119,7 +119,7 @@ try {
TextFile file(config.path); TextFile file(config.path);
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
const SongLoader song_loader(partition.instance.database, const SongLoader song_loader(partition.instance.GetDatabase(),
partition.instance.storage); partition.instance.storage);
#else #else
const SongLoader song_loader(nullptr, nullptr); const SongLoader song_loader(nullptr, nullptr);

View File

@ -124,7 +124,7 @@ stats_print(Response &r, const Partition &partition)
std::lround(partition.pc.GetTotalPlayTime().count())); std::lround(partition.pc.GetTotalPlayTime().count()));
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
const Database *db = partition.instance.database; const Database *db = partition.instance.GetDatabase();
if (db != nullptr) if (db != nullptr)
db_stats_print(r, *db); db_stats_print(r, *db);
#endif #endif

View File

@ -294,7 +294,7 @@ handle_update(Client &client, Request args, Response &r, bool discard)
if (update != nullptr) if (update != nullptr)
return handle_update(r, *update, path, discard); return handle_update(r, *update, path, discard);
Database *db = client.GetInstance().database; Database *db = client.GetInstance().GetDatabase();
if (db != nullptr) if (db != nullptr)
return handle_update(r, *db, path, discard); return handle_update(r, *db, path, discard);
#else #else

View File

@ -209,7 +209,7 @@ handle_mount(Client &client, Request args, Response &r)
instance.EmitIdle(IDLE_MOUNT); instance.EmitIdle(IDLE_MOUNT);
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
if (auto *db = dynamic_cast<SimpleDatabase *>(instance.database)) { if (auto *db = dynamic_cast<SimpleDatabase *>(instance.GetDatabase())) {
try { try {
db->Mount(local_uri, remote_uri); db->Mount(local_uri, remote_uri);
} catch (...) { } catch (...) {
@ -253,7 +253,7 @@ handle_unmount(Client &client, Request args, Response &r)
destroy here */ destroy here */
instance.update->CancelMount(local_uri); instance.update->CancelMount(local_uri);
if (auto *db = dynamic_cast<SimpleDatabase *>(instance.database)) { if (auto *db = dynamic_cast<SimpleDatabase *>(instance.GetDatabase())) {
if (db->Unmount(local_uri)) if (db->Unmount(local_uri))
// TODO: call Instance::OnDatabaseModified()? // TODO: call Instance::OnDatabaseModified()?
instance.EmitIdle(IDLE_DATABASE); instance.EmitIdle(IDLE_DATABASE);

View File

@ -106,7 +106,7 @@ storage_state_restore(const char *line, TextFile &file, Instance &instance)
return true; return true;
} }
if (auto *db = dynamic_cast<SimpleDatabase *>(instance.database)) { if (auto *db = dynamic_cast<SimpleDatabase *>(instance.GetDatabase())) {
try { try {
db->Mount(uri.c_str(), url.c_str()); db->Mount(uri.c_str(), url.c_str());
} catch (...) { } catch (...) {