From 7bce6329e3a70130b1f2fb5aecc7d125c46291b0 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 26 Dec 2017 20:05:22 +0100 Subject: [PATCH] archive/File, input/Plugin: return InputStreamPtr --- src/TagArchive.cxx | 4 ++-- src/archive/ArchiveFile.hxx | 7 ++++--- src/archive/plugins/Bzip2ArchivePlugin.cxx | 8 ++++---- src/archive/plugins/Iso9660ArchivePlugin.cxx | 10 +++++----- src/archive/plugins/ZzipArchivePlugin.cxx | 12 ++++++------ src/input/InputPlugin.hxx | 6 ++++-- src/input/Open.cxx | 6 ++---- src/input/plugins/AlsaInputPlugin.cxx | 14 +++++++------- src/input/plugins/ArchiveInputPlugin.cxx | 4 ++-- src/input/plugins/CdioParanoiaInputPlugin.cxx | 9 +++++---- src/input/plugins/CurlInputPlugin.cxx | 8 ++++---- src/input/plugins/FfmpegInputPlugin.cxx | 4 ++-- src/input/plugins/FileInputPlugin.cxx | 8 ++++---- src/input/plugins/MmsInputPlugin.cxx | 4 ++-- src/input/plugins/NfsInputPlugin.cxx | 12 +++--------- src/input/plugins/RewindInputPlugin.cxx | 2 +- src/input/plugins/SmbclientInputPlugin.cxx | 5 +++-- 17 files changed, 60 insertions(+), 63 deletions(-) diff --git a/src/TagArchive.cxx b/src/TagArchive.cxx index cc02a16de..d7febb603 100644 --- a/src/TagArchive.cxx +++ b/src/TagArchive.cxx @@ -31,7 +31,7 @@ try { Mutex mutex; Cond cond; - InputStreamPtr is(archive.OpenStream(path_utf8, mutex, cond)); + auto is = archive.OpenStream(path_utf8, mutex, cond); if (!is) return false; @@ -47,7 +47,7 @@ try { Mutex mutex; Cond cond; - InputStreamPtr is(archive.OpenStream(path_utf8, mutex, cond)); + auto is = archive.OpenStream(path_utf8, mutex, cond); return is && tag_stream_scan(*is, builder); } catch (const std::exception &e) { return false; diff --git a/src/archive/ArchiveFile.hxx b/src/archive/ArchiveFile.hxx index 72f72d7b9..00c528e40 100644 --- a/src/archive/ArchiveFile.hxx +++ b/src/archive/ArchiveFile.hxx @@ -20,10 +20,11 @@ #ifndef MPD_ARCHIVE_FILE_HXX #define MPD_ARCHIVE_FILE_HXX +#include "input/Ptr.hxx" + class Mutex; class Cond; class ArchiveVisitor; -class InputStream; class ArchiveFile { public: @@ -41,8 +42,8 @@ public: * * @param path the path within the archive */ - virtual InputStream *OpenStream(const char *path, - Mutex &mutex, Cond &cond) = 0; + virtual InputStreamPtr OpenStream(const char *path, + Mutex &mutex, Cond &cond) = 0; }; #endif diff --git a/src/archive/plugins/Bzip2ArchivePlugin.cxx b/src/archive/plugins/Bzip2ArchivePlugin.cxx index b8bad8826..0cd849c19 100644 --- a/src/archive/plugins/Bzip2ArchivePlugin.cxx +++ b/src/archive/plugins/Bzip2ArchivePlugin.cxx @@ -53,8 +53,8 @@ public: visitor.VisitArchiveEntry(name.c_str()); } - InputStream *OpenStream(const char *path, - Mutex &mutex, Cond &cond) override; + InputStreamPtr OpenStream(const char *path, + Mutex &mutex, Cond &cond) override; }; class Bzip2InputStream final : public InputStream { @@ -127,11 +127,11 @@ Bzip2InputStream::~Bzip2InputStream() BZ2_bzDecompressEnd(&bzstream); } -InputStream * +InputStreamPtr Bzip2ArchiveFile::OpenStream(const char *path, Mutex &mutex, Cond &cond) { - return new Bzip2InputStream(istream, path, mutex, cond); + return std::make_unique(istream, path, mutex, cond); } inline bool diff --git a/src/archive/plugins/Iso9660ArchivePlugin.cxx b/src/archive/plugins/Iso9660ArchivePlugin.cxx index 62f61d596..5a20c637c 100644 --- a/src/archive/plugins/Iso9660ArchivePlugin.cxx +++ b/src/archive/plugins/Iso9660ArchivePlugin.cxx @@ -74,8 +74,8 @@ public: virtual void Visit(ArchiveVisitor &visitor) override; - InputStream *OpenStream(const char *path, - Mutex &mutex, Cond &cond) override; + InputStreamPtr OpenStream(const char *path, + Mutex &mutex, Cond &cond) override; }; /* archive open && listing routine */ @@ -156,7 +156,7 @@ public: size_t Read(void *ptr, size_t size) override; }; -InputStream * +InputStreamPtr Iso9660ArchiveFile::OpenStream(const char *pathname, Mutex &mutex, Cond &cond) { @@ -165,8 +165,8 @@ Iso9660ArchiveFile::OpenStream(const char *pathname, throw FormatRuntimeError("not found in the ISO file: %s", pathname); - return new Iso9660InputStream(iso, pathname, mutex, cond, - statbuf); + return std::make_unique(iso, pathname, mutex, cond, + statbuf); } size_t diff --git a/src/archive/plugins/ZzipArchivePlugin.cxx b/src/archive/plugins/ZzipArchivePlugin.cxx index fc40e7689..5660e4286 100644 --- a/src/archive/plugins/ZzipArchivePlugin.cxx +++ b/src/archive/plugins/ZzipArchivePlugin.cxx @@ -59,8 +59,8 @@ public: virtual void Visit(ArchiveVisitor &visitor) override; - InputStream *OpenStream(const char *path, - Mutex &mutex, Cond &cond) override; + InputStreamPtr OpenStream(const char *path, + Mutex &mutex, Cond &cond) override; }; /* archive open && listing routine */ @@ -116,7 +116,7 @@ public: void Seek(offset_type offset) override; }; -InputStream * +InputStreamPtr ZzipArchiveFile::OpenStream(const char *pathname, Mutex &mutex, Cond &cond) { @@ -125,9 +125,9 @@ ZzipArchiveFile::OpenStream(const char *pathname, throw FormatRuntimeError("not found in the ZIP file: %s", pathname); - return new ZzipInputStream(dir, pathname, - mutex, cond, - _file); + return std::make_unique(dir, pathname, + mutex, cond, + _file); } size_t diff --git a/src/input/InputPlugin.hxx b/src/input/InputPlugin.hxx index 3fc48b478..c9698818f 100644 --- a/src/input/InputPlugin.hxx +++ b/src/input/InputPlugin.hxx @@ -20,6 +20,8 @@ #ifndef MPD_INPUT_PLUGIN_HXX #define MPD_INPUT_PLUGIN_HXX +#include "Ptr.hxx" + struct ConfigBlock; class Mutex; class Cond; @@ -48,8 +50,8 @@ struct InputPlugin { /** * Throws std::runtime_error on error. */ - InputStream *(*open)(const char *uri, - Mutex &mutex, Cond &cond); + InputStreamPtr (*open)(const char *uri, + Mutex &mutex, Cond &cond); }; #endif diff --git a/src/input/Open.cxx b/src/input/Open.cxx index a62899853..1d318b899 100644 --- a/src/input/Open.cxx +++ b/src/input/Open.cxx @@ -39,11 +39,9 @@ InputStream::Open(const char *url, } input_plugins_for_each_enabled(plugin) { - InputStream *is; - - is = plugin->open(url, mutex, cond); + auto is = plugin->open(url, mutex, cond); if (is != nullptr) - return input_rewind_open(InputStreamPtr(is)); + return input_rewind_open(std::move(is)); } throw std::runtime_error("Unrecognized URI"); diff --git a/src/input/plugins/AlsaInputPlugin.cxx b/src/input/plugins/AlsaInputPlugin.cxx index dfc524b22..71e910d86 100644 --- a/src/input/plugins/AlsaInputPlugin.cxx +++ b/src/input/plugins/AlsaInputPlugin.cxx @@ -110,8 +110,8 @@ public: snd_pcm_close(capture_handle); } - static InputStream *Create(EventLoop &event_loop, const char *uri, - Mutex &mutex, Cond &cond); + static InputStreamPtr Create(EventLoop &event_loop, const char *uri, + Mutex &mutex, Cond &cond); protected: /* virtual methods from AsyncInputStream */ @@ -146,7 +146,7 @@ private: void DispatchSockets() noexcept override; }; -inline InputStream * +inline InputStreamPtr AlsaInputStream::Create(EventLoop &event_loop, const char *uri, Mutex &mutex, Cond &cond) { @@ -167,9 +167,9 @@ AlsaInputStream::Create(EventLoop &event_loop, const char *uri, snd_pcm_t *handle = OpenDevice(device, rate, format, channels); int frame_size = snd_pcm_format_width(format) / 8 * channels; - return new AlsaInputStream(event_loop, - uri, mutex, cond, - device, handle, frame_size); + return std::make_unique(event_loop, + uri, mutex, cond, + device, handle, frame_size); } std::chrono::steady_clock::duration @@ -396,7 +396,7 @@ alsa_input_init(EventLoop &event_loop, const ConfigBlock &) alsa_input_event_loop = &event_loop; } -static InputStream * +static InputStreamPtr alsa_input_open(const char *uri, Mutex &mutex, Cond &cond) { return AlsaInputStream::Create(*alsa_input_event_loop, uri, diff --git a/src/input/plugins/ArchiveInputPlugin.cxx b/src/input/plugins/ArchiveInputPlugin.cxx index 72caf7fa2..84565548a 100644 --- a/src/input/plugins/ArchiveInputPlugin.cxx +++ b/src/input/plugins/ArchiveInputPlugin.cxx @@ -66,10 +66,10 @@ OpenArchiveInputStream(Path path, Mutex &mutex, Cond &cond) delete file; }; - return InputStreamPtr(file->OpenStream(filename, mutex, cond)); + return file->OpenStream(filename, mutex, cond); } -static InputStream * +static InputStreamPtr input_archive_open(gcc_unused const char *filename, gcc_unused Mutex &mutex, gcc_unused Cond &cond) { diff --git a/src/input/plugins/CdioParanoiaInputPlugin.cxx b/src/input/plugins/CdioParanoiaInputPlugin.cxx index 2b9bced5e..d7d93909a 100644 --- a/src/input/plugins/CdioParanoiaInputPlugin.cxx +++ b/src/input/plugins/CdioParanoiaInputPlugin.cxx @@ -182,7 +182,7 @@ cdio_detect_device(void) return path; } -static InputStream * +static InputStreamPtr input_cdio_open(const char *uri, Mutex &mutex, Cond &cond) { @@ -250,9 +250,10 @@ input_cdio_open(const char *uri, lsn_to = cdio_get_disc_last_lsn(cdio); } - return new CdioParanoiaInputStream(uri, mutex, cond, - drv, cdio, reverse_endian, - lsn_from, lsn_to); + return std::make_unique(uri, mutex, cond, + drv, cdio, + reverse_endian, + lsn_from, lsn_to); } void diff --git a/src/input/plugins/CurlInputPlugin.cxx b/src/input/plugins/CurlInputPlugin.cxx index 45fc74ef1..dd80f8ff7 100644 --- a/src/input/plugins/CurlInputPlugin.cxx +++ b/src/input/plugins/CurlInputPlugin.cxx @@ -88,7 +88,7 @@ struct CurlInputStream final : public AsyncInputStream, CurlResponseHandler { CurlInputStream(const CurlInputStream &) = delete; CurlInputStream &operator=(const CurlInputStream &) = delete; - static InputStream *Open(const char *url, Mutex &mutex, Cond &cond); + static InputStreamPtr Open(const char *url, Mutex &mutex, Cond &cond); /** * Create and initialize a new #CurlRequest instance. After @@ -435,7 +435,7 @@ CurlInputStream::DoSeek(offset_type new_offset) }); } -inline InputStream * +inline InputStreamPtr CurlInputStream::Open(const char *url, Mutex &mutex, Cond &cond) { auto c = std::make_unique((*curl_init)->GetEventLoop(), @@ -447,10 +447,10 @@ CurlInputStream::Open(const char *url, Mutex &mutex, Cond &cond) }); auto icy = c->icy; - return new IcyInputStream(std::move(c), std::move(icy)); + return std::make_unique(std::move(c), std::move(icy)); } -static InputStream * +static InputStreamPtr input_curl_open(const char *url, Mutex &mutex, Cond &cond) { if (strncmp(url, "http://", 7) != 0 && diff --git a/src/input/plugins/FfmpegInputPlugin.cxx b/src/input/plugins/FfmpegInputPlugin.cxx index 781567dac..b78d1dd13 100644 --- a/src/input/plugins/FfmpegInputPlugin.cxx +++ b/src/input/plugins/FfmpegInputPlugin.cxx @@ -81,7 +81,7 @@ input_ffmpeg_init(EventLoop &, const ConfigBlock &) throw PluginUnavailable("No protocol"); } -static InputStream * +static InputStreamPtr input_ffmpeg_open(const char *uri, Mutex &mutex, Cond &cond) { @@ -98,7 +98,7 @@ input_ffmpeg_open(const char *uri, if (result != 0) throw MakeFfmpegError(result); - return new FfmpegInputStream(uri, mutex, cond, h); + return std::make_unique(uri, mutex, cond, h); } size_t diff --git a/src/input/plugins/FileInputPlugin.cxx b/src/input/plugins/FileInputPlugin.cxx index 2d2850c74..c73211b6d 100644 --- a/src/input/plugins/FileInputPlugin.cxx +++ b/src/input/plugins/FileInputPlugin.cxx @@ -70,12 +70,12 @@ OpenFileInputStream(Path path, POSIX_FADV_SEQUENTIAL); #endif - return InputStreamPtr(new FileInputStream(path.ToUTF8().c_str(), - std::move(reader), info.GetSize(), - mutex, cond)); + return std::make_unique(path.ToUTF8().c_str(), + std::move(reader), info.GetSize(), + mutex, cond); } -static InputStream * +static InputStreamPtr input_file_open(gcc_unused const char *filename, gcc_unused Mutex &mutex, gcc_unused Cond &cond) { diff --git a/src/input/plugins/MmsInputPlugin.cxx b/src/input/plugins/MmsInputPlugin.cxx index c06748adf..5ba10db43 100644 --- a/src/input/plugins/MmsInputPlugin.cxx +++ b/src/input/plugins/MmsInputPlugin.cxx @@ -66,7 +66,7 @@ MmsInputStream::Open() SetMimeType("audio/x-ms-wma"); } -static InputStream * +static InputStreamPtr input_mms_open(const char *url, Mutex &mutex, Cond &cond) { @@ -76,7 +76,7 @@ input_mms_open(const char *url, !StringStartsWith(url, "mmsu://")) return nullptr; - auto m = new MmsInputStream(url, mutex, cond); + auto m = std::make_unique(url, mutex, cond); m->Start(); return m; } diff --git a/src/input/plugins/NfsInputPlugin.cxx b/src/input/plugins/NfsInputPlugin.cxx index fd49b3aa5..ffee929b6 100644 --- a/src/input/plugins/NfsInputPlugin.cxx +++ b/src/input/plugins/NfsInputPlugin.cxx @@ -216,21 +216,15 @@ input_nfs_finish() noexcept nfs_finish(); } -static InputStream * +static InputStreamPtr input_nfs_open(const char *uri, Mutex &mutex, Cond &cond) { if (!StringStartsWith(uri, "nfs://")) return nullptr; - NfsInputStream *is = new NfsInputStream(uri, mutex, cond); - try { - is->Open(); - } catch (...) { - delete is; - throw; - } - + auto is = std::make_unique(uri, mutex, cond); + is->Open(); return is; } diff --git a/src/input/plugins/RewindInputPlugin.cxx b/src/input/plugins/RewindInputPlugin.cxx index 839f19d76..2bce86236 100644 --- a/src/input/plugins/RewindInputPlugin.cxx +++ b/src/input/plugins/RewindInputPlugin.cxx @@ -147,5 +147,5 @@ input_rewind_open(InputStreamPtr is) /* seekable resources don't need this plugin */ return is; - return InputStreamPtr(new RewindInputStream(std::move(is))); + return std::make_unique(std::move(is)); } diff --git a/src/input/plugins/SmbclientInputPlugin.cxx b/src/input/plugins/SmbclientInputPlugin.cxx index b40eadb9a..517c29c2b 100644 --- a/src/input/plugins/SmbclientInputPlugin.cxx +++ b/src/input/plugins/SmbclientInputPlugin.cxx @@ -83,7 +83,7 @@ input_smbclient_init(EventLoop &, const ConfigBlock &) // TODO: evaluate ConfigBlock, call smbc_setOption*() } -static InputStream * +static InputStreamPtr input_smbclient_open(const char *uri, Mutex &mutex, Cond &cond) { @@ -119,7 +119,8 @@ input_smbclient_open(const char *uri, throw MakeErrno(e, "smbc_fstat() failed"); } - return new SmbclientInputStream(uri, mutex, cond, ctx, fd, st); + return std::make_unique(uri, mutex, cond, + ctx, fd, st); } size_t