storage/Composite: use std::unique_ptr<Storage>

This commit is contained in:
Max Kellermann 2018-01-02 16:18:34 +01:00
parent 3c5e4e2788
commit dcd483bd99
5 changed files with 12 additions and 23 deletions

View File

@ -181,7 +181,7 @@ InitStorage(EventLoop &event_loop)
CompositeStorage *composite = new CompositeStorage(); CompositeStorage *composite = new CompositeStorage();
instance->storage = composite; instance->storage = composite;
composite->Mount("", storage.release()); composite->Mount("", std::move(storage));
} }
/** /**

View File

@ -205,7 +205,7 @@ handle_mount(Client &client, Request args, Response &r)
return CommandResult::ERROR; return CommandResult::ERROR;
} }
composite.Mount(local_uri, storage.release()); composite.Mount(local_uri, std::move(storage));
instance.EmitIdle(IDLE_MOUNT); instance.EmitIdle(IDLE_MOUNT);
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE

View File

@ -103,11 +103,6 @@ NextSegment(const char *&uri_r)
} }
} }
CompositeStorage::Directory::~Directory()
{
delete storage;
}
const CompositeStorage::Directory * const CompositeStorage::Directory *
CompositeStorage::Directory::Find(const char *uri) const noexcept CompositeStorage::Directory::Find(const char *uri) const noexcept
{ {
@ -144,8 +139,7 @@ CompositeStorage::Directory::Unmount() noexcept
if (storage == nullptr) if (storage == nullptr)
return false; return false;
delete storage; storage.reset();
storage = nullptr;
return true; return true;
} }
@ -210,18 +204,16 @@ CompositeStorage::GetMount(const char *uri) noexcept
/* not a mount point */ /* not a mount point */
return nullptr; return nullptr;
return result.directory->storage; return result.directory->storage.get();
} }
void void
CompositeStorage::Mount(const char *uri, Storage *storage) CompositeStorage::Mount(const char *uri, std::unique_ptr<Storage> storage)
{ {
const std::lock_guard<Mutex> protect(mutex); const std::lock_guard<Mutex> protect(mutex);
Directory &directory = root.Make(uri); Directory &directory = root.Make(uri);
if (directory.storage != nullptr) directory.storage = std::move(storage);
delete directory.storage;
directory.storage = storage;
} }
bool bool

View File

@ -25,6 +25,7 @@
#include "thread/Mutex.hxx" #include "thread/Mutex.hxx"
#include "Compiler.h" #include "Compiler.h"
#include <memory>
#include <string> #include <string>
#include <map> #include <map>
@ -47,13 +48,10 @@ class CompositeStorage final : public Storage {
* Other Directory instances may have one, and child * Other Directory instances may have one, and child
* mounts will be "mixed" in. * mounts will be "mixed" in.
*/ */
Storage *storage; std::unique_ptr<Storage> storage;
std::map<std::string, Directory> children; std::map<std::string, Directory> children;
Directory():storage(nullptr) {}
~Directory();
gcc_pure gcc_pure
bool IsEmpty() const noexcept { bool IsEmpty() const noexcept {
return storage == nullptr && children.empty(); return storage == nullptr && children.empty();
@ -115,7 +113,7 @@ public:
VisitMounts(uri, root, t); VisitMounts(uri, root, t);
} }
void Mount(const char *uri, Storage *storage); void Mount(const char *uri, std::unique_ptr<Storage> storage);
bool Unmount(const char *uri); bool Unmount(const char *uri);
/* virtual methods from class Storage */ /* virtual methods from class Storage */
@ -133,9 +131,8 @@ private:
template<typename T> template<typename T>
void VisitMounts(std::string &uri, const Directory &directory, void VisitMounts(std::string &uri, const Directory &directory,
T t) const { T t) const {
const Storage *const storage = directory.storage; if (directory.storage)
if (storage != nullptr) t(uri.c_str(), *directory.storage);
t(uri.c_str(), *storage);
if (!uri.empty()) if (!uri.empty())
uri.push_back('/'); uri.push_back('/');

View File

@ -120,7 +120,7 @@ storage_state_restore(const char *line, TextFile &file, Instance &instance)
} }
((CompositeStorage*)instance.storage)->Mount(uri.c_str(), ((CompositeStorage*)instance.storage)->Mount(uri.c_str(),
storage.release()); std::move(storage));
return true; return true;
} }