From dcd483bd99446622c8fde6ad3e0df50ed40b4164 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 2 Jan 2018 16:18:34 +0100 Subject: [PATCH] storage/Composite: use std::unique_ptr --- src/Main.cxx | 2 +- src/command/StorageCommands.cxx | 2 +- src/storage/CompositeStorage.cxx | 16 ++++------------ src/storage/CompositeStorage.hxx | 13 +++++-------- src/storage/StorageState.cxx | 2 +- 5 files changed, 12 insertions(+), 23 deletions(-) diff --git a/src/Main.cxx b/src/Main.cxx index 2a556af62..195acce33 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -181,7 +181,7 @@ InitStorage(EventLoop &event_loop) CompositeStorage *composite = new CompositeStorage(); instance->storage = composite; - composite->Mount("", storage.release()); + composite->Mount("", std::move(storage)); } /** diff --git a/src/command/StorageCommands.cxx b/src/command/StorageCommands.cxx index bb06f821e..cb6920719 100644 --- a/src/command/StorageCommands.cxx +++ b/src/command/StorageCommands.cxx @@ -205,7 +205,7 @@ handle_mount(Client &client, Request args, Response &r) return CommandResult::ERROR; } - composite.Mount(local_uri, storage.release()); + composite.Mount(local_uri, std::move(storage)); instance.EmitIdle(IDLE_MOUNT); #ifdef ENABLE_DATABASE diff --git a/src/storage/CompositeStorage.cxx b/src/storage/CompositeStorage.cxx index a04ed1ce9..84ee67fc6 100644 --- a/src/storage/CompositeStorage.cxx +++ b/src/storage/CompositeStorage.cxx @@ -103,11 +103,6 @@ NextSegment(const char *&uri_r) } } -CompositeStorage::Directory::~Directory() -{ - delete storage; -} - const CompositeStorage::Directory * CompositeStorage::Directory::Find(const char *uri) const noexcept { @@ -144,8 +139,7 @@ CompositeStorage::Directory::Unmount() noexcept if (storage == nullptr) return false; - delete storage; - storage = nullptr; + storage.reset(); return true; } @@ -210,18 +204,16 @@ CompositeStorage::GetMount(const char *uri) noexcept /* not a mount point */ return nullptr; - return result.directory->storage; + return result.directory->storage.get(); } void -CompositeStorage::Mount(const char *uri, Storage *storage) +CompositeStorage::Mount(const char *uri, std::unique_ptr storage) { const std::lock_guard protect(mutex); Directory &directory = root.Make(uri); - if (directory.storage != nullptr) - delete directory.storage; - directory.storage = storage; + directory.storage = std::move(storage); } bool diff --git a/src/storage/CompositeStorage.hxx b/src/storage/CompositeStorage.hxx index 668d55a6a..6bc871bab 100644 --- a/src/storage/CompositeStorage.hxx +++ b/src/storage/CompositeStorage.hxx @@ -25,6 +25,7 @@ #include "thread/Mutex.hxx" #include "Compiler.h" +#include #include #include @@ -47,13 +48,10 @@ class CompositeStorage final : public Storage { * Other Directory instances may have one, and child * mounts will be "mixed" in. */ - Storage *storage; + std::unique_ptr storage; std::map children; - Directory():storage(nullptr) {} - ~Directory(); - gcc_pure bool IsEmpty() const noexcept { return storage == nullptr && children.empty(); @@ -115,7 +113,7 @@ public: VisitMounts(uri, root, t); } - void Mount(const char *uri, Storage *storage); + void Mount(const char *uri, std::unique_ptr storage); bool Unmount(const char *uri); /* virtual methods from class Storage */ @@ -133,9 +131,8 @@ private: template void VisitMounts(std::string &uri, const Directory &directory, T t) const { - const Storage *const storage = directory.storage; - if (storage != nullptr) - t(uri.c_str(), *storage); + if (directory.storage) + t(uri.c_str(), *directory.storage); if (!uri.empty()) uri.push_back('/'); diff --git a/src/storage/StorageState.cxx b/src/storage/StorageState.cxx index de60954a6..99299f886 100644 --- a/src/storage/StorageState.cxx +++ b/src/storage/StorageState.cxx @@ -120,7 +120,7 @@ storage_state_restore(const char *line, TextFile &file, Instance &instance) } ((CompositeStorage*)instance.storage)->Mount(uri.c_str(), - storage.release()); + std::move(storage)); return true; }