storage/Plugin: return std::unique_ptr<Storage>
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
#include "config.h"
|
||||
#include "Configured.hxx"
|
||||
#include "Registry.hxx"
|
||||
#include "StorageInterface.hxx"
|
||||
#include "plugins/LocalStorage.hxx"
|
||||
#include "config/ConfigGlobal.hxx"
|
||||
#include "config/ConfigError.hxx"
|
||||
@@ -30,10 +31,10 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
static Storage *
|
||||
static std::unique_ptr<Storage>
|
||||
CreateConfiguredStorageUri(EventLoop &event_loop, const char *uri)
|
||||
{
|
||||
Storage *storage = CreateStorageURI(event_loop, uri);
|
||||
auto storage = CreateStorageURI(event_loop, uri);
|
||||
if (storage == nullptr)
|
||||
throw FormatRuntimeError("Unrecognized storage URI: %s", uri);
|
||||
|
||||
@@ -50,7 +51,7 @@ GetConfiguredMusicDirectory()
|
||||
return path;
|
||||
}
|
||||
|
||||
static Storage *
|
||||
static std::unique_ptr<Storage>
|
||||
CreateConfiguredStorageLocal()
|
||||
{
|
||||
AllocatedPath path = GetConfiguredMusicDirectory();
|
||||
@@ -62,7 +63,7 @@ CreateConfiguredStorageLocal()
|
||||
return CreateLocalStorage(path);
|
||||
}
|
||||
|
||||
Storage *
|
||||
std::unique_ptr<Storage>
|
||||
CreateConfiguredStorage(EventLoop &event_loop)
|
||||
{
|
||||
auto uri = config_get_string(ConfigOption::MUSIC_DIR);
|
||||
|
@@ -23,6 +23,8 @@
|
||||
#include "check.h"
|
||||
#include "Compiler.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
class Storage;
|
||||
class EventLoop;
|
||||
|
||||
@@ -32,7 +34,7 @@ class EventLoop;
|
||||
*
|
||||
* Throws #std::runtime_error on error.
|
||||
*/
|
||||
Storage *
|
||||
std::unique_ptr<Storage>
|
||||
CreateConfiguredStorage(EventLoop &event_loop);
|
||||
|
||||
/**
|
||||
|
@@ -20,6 +20,7 @@
|
||||
#include "config.h"
|
||||
#include "Registry.hxx"
|
||||
#include "StoragePlugin.hxx"
|
||||
#include "StorageInterface.hxx"
|
||||
#include "plugins/LocalStorage.hxx"
|
||||
#include "plugins/SmbclientStorage.hxx"
|
||||
#include "plugins/NfsStorage.hxx"
|
||||
@@ -54,7 +55,7 @@ GetStoragePluginByName(const char *name) noexcept
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Storage *
|
||||
std::unique_ptr<Storage>
|
||||
CreateStorageURI(EventLoop &event_loop, const char *uri)
|
||||
{
|
||||
for (auto i = storage_plugins; *i != nullptr; ++i) {
|
||||
@@ -63,7 +64,7 @@ CreateStorageURI(EventLoop &event_loop, const char *uri)
|
||||
if (plugin.create_uri == nullptr)
|
||||
continue;
|
||||
|
||||
Storage *storage = plugin.create_uri(event_loop, uri);
|
||||
auto storage = plugin.create_uri(event_loop, uri);
|
||||
if (storage != nullptr)
|
||||
return storage;
|
||||
}
|
||||
|
@@ -23,6 +23,8 @@
|
||||
#include "check.h"
|
||||
#include "Compiler.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
struct StoragePlugin;
|
||||
class Storage;
|
||||
class EventLoop;
|
||||
@@ -37,8 +39,8 @@ gcc_nonnull_all gcc_pure
|
||||
const StoragePlugin *
|
||||
GetStoragePluginByName(const char *name) noexcept;
|
||||
|
||||
gcc_nonnull_all gcc_malloc
|
||||
Storage *
|
||||
gcc_nonnull_all
|
||||
std::unique_ptr<Storage>
|
||||
CreateStorageURI(EventLoop &event_loop, const char *uri);
|
||||
|
||||
#endif
|
||||
|
@@ -22,6 +22,8 @@
|
||||
|
||||
#include "check.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
class Storage;
|
||||
class EventLoop;
|
||||
|
||||
@@ -31,7 +33,8 @@ struct StoragePlugin {
|
||||
/**
|
||||
* Throws #std::runtime_error on error.
|
||||
*/
|
||||
Storage *(*create_uri)(EventLoop &event_loop, const char *uri);
|
||||
std::unique_ptr<Storage> (*create_uri)(EventLoop &event_loop,
|
||||
const char *uri);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@@ -101,7 +101,7 @@ storage_state_restore(const char *line, TextFile &file, Instance &instance)
|
||||
FormatDebug(storage_domain, "Restoring mount %s => %s", uri.c_str(), url.c_str());
|
||||
|
||||
auto &event_loop = instance.io_thread.GetEventLoop();
|
||||
Storage *storage = CreateStorageURI(event_loop, url.c_str());
|
||||
auto storage = CreateStorageURI(event_loop, url.c_str());
|
||||
if (storage == nullptr) {
|
||||
FormatError(storage_domain, "Unrecognized storage URI: %s", url.c_str());
|
||||
return true;
|
||||
@@ -112,7 +112,6 @@ storage_state_restore(const char *line, TextFile &file, Instance &instance)
|
||||
try {
|
||||
((SimpleDatabase *)db)->Mount(uri.c_str(), url.c_str());
|
||||
} catch (...) {
|
||||
delete storage;
|
||||
FormatError(std::current_exception(),
|
||||
"Failed to restore mount to %s",
|
||||
url.c_str());
|
||||
@@ -120,7 +119,8 @@ storage_state_restore(const char *line, TextFile &file, Instance &instance)
|
||||
}
|
||||
}
|
||||
|
||||
((CompositeStorage*)instance.storage)->Mount(uri.c_str(), storage);
|
||||
((CompositeStorage*)instance.storage)->Mount(uri.c_str(),
|
||||
storage.release());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@@ -549,14 +549,14 @@ CurlStorage::OpenDirectory(const char *uri_utf8)
|
||||
return HttpListDirectoryOperation(*curl, uri.c_str()).Perform();
|
||||
}
|
||||
|
||||
static Storage *
|
||||
static std::unique_ptr<Storage>
|
||||
CreateCurlStorageURI(EventLoop &event_loop, const char *uri)
|
||||
{
|
||||
if (strncmp(uri, "http://", 7) != 0 &&
|
||||
strncmp(uri, "https://", 8) != 0)
|
||||
return nullptr;
|
||||
|
||||
return new CurlStorage(event_loop, uri);
|
||||
return std::make_unique<CurlStorage>(event_loop, uri);
|
||||
}
|
||||
|
||||
const StoragePlugin curl_storage_plugin = {
|
||||
|
@@ -179,10 +179,10 @@ LocalDirectoryReader::GetInfo(bool follow)
|
||||
return Stat(AllocatedPath::Build(base_fs, reader.GetEntry()), follow);
|
||||
}
|
||||
|
||||
Storage *
|
||||
std::unique_ptr<Storage>
|
||||
CreateLocalStorage(Path base_fs)
|
||||
{
|
||||
return new LocalStorage(base_fs);
|
||||
return std::make_unique<LocalStorage>(base_fs);
|
||||
}
|
||||
|
||||
const StoragePlugin local_storage_plugin = {
|
||||
|
@@ -23,14 +23,16 @@
|
||||
#include "check.h"
|
||||
#include "Compiler.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
struct StoragePlugin;
|
||||
class Storage;
|
||||
class Path;
|
||||
|
||||
extern const StoragePlugin local_storage_plugin;
|
||||
|
||||
gcc_malloc gcc_nonnull_all
|
||||
Storage *
|
||||
gcc_nonnull_all
|
||||
std::unique_ptr<Storage>
|
||||
CreateLocalStorage(Path base_fs);
|
||||
|
||||
#endif
|
||||
|
@@ -390,7 +390,7 @@ NfsStorage::OpenDirectory(const char *uri_utf8)
|
||||
return operation.ToReader();
|
||||
}
|
||||
|
||||
static Storage *
|
||||
static std::unique_ptr<Storage>
|
||||
CreateNfsStorageURI(EventLoop &event_loop, const char *base)
|
||||
{
|
||||
if (strncmp(base, "nfs://", 6) != 0)
|
||||
@@ -406,7 +406,8 @@ CreateNfsStorageURI(EventLoop &event_loop, const char *base)
|
||||
|
||||
nfs_set_base(server.c_str(), mount);
|
||||
|
||||
return new NfsStorage(event_loop, base, server.c_str(), mount);
|
||||
return std::make_unique<NfsStorage>(event_loop, base,
|
||||
server.c_str(), mount);
|
||||
}
|
||||
|
||||
const StoragePlugin nfs_storage_plugin = {
|
||||
|
@@ -179,7 +179,7 @@ SmbclientDirectoryReader::GetInfo(gcc_unused bool follow)
|
||||
return ::GetInfo(path.c_str());
|
||||
}
|
||||
|
||||
static Storage *
|
||||
static std::unique_ptr<Storage>
|
||||
CreateSmbclientStorageURI(gcc_unused EventLoop &event_loop, const char *base)
|
||||
{
|
||||
if (strncmp(base, "smb://", 6) != 0)
|
||||
@@ -198,7 +198,7 @@ CreateSmbclientStorageURI(gcc_unused EventLoop &event_loop, const char *base)
|
||||
throw MakeErrno("smbc_new_context() failed");
|
||||
}
|
||||
|
||||
return new SmbclientStorage(base, ctx2);
|
||||
return std::make_unique<SmbclientStorage>(base, ctx2);
|
||||
}
|
||||
|
||||
const StoragePlugin smbclient_storage_plugin = {
|
||||
|
Reference in New Issue
Block a user