archive/File, input/Plugin: return InputStreamPtr

This commit is contained in:
Max Kellermann 2017-12-26 20:05:22 +01:00
parent 49619fbd77
commit 7bce6329e3
17 changed files with 60 additions and 63 deletions

View File

@ -31,7 +31,7 @@ try {
Mutex mutex; Mutex mutex;
Cond cond; Cond cond;
InputStreamPtr is(archive.OpenStream(path_utf8, mutex, cond)); auto is = archive.OpenStream(path_utf8, mutex, cond);
if (!is) if (!is)
return false; return false;
@ -47,7 +47,7 @@ try {
Mutex mutex; Mutex mutex;
Cond cond; 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); return is && tag_stream_scan(*is, builder);
} catch (const std::exception &e) { } catch (const std::exception &e) {
return false; return false;

View File

@ -20,10 +20,11 @@
#ifndef MPD_ARCHIVE_FILE_HXX #ifndef MPD_ARCHIVE_FILE_HXX
#define MPD_ARCHIVE_FILE_HXX #define MPD_ARCHIVE_FILE_HXX
#include "input/Ptr.hxx"
class Mutex; class Mutex;
class Cond; class Cond;
class ArchiveVisitor; class ArchiveVisitor;
class InputStream;
class ArchiveFile { class ArchiveFile {
public: public:
@ -41,8 +42,8 @@ public:
* *
* @param path the path within the archive * @param path the path within the archive
*/ */
virtual InputStream *OpenStream(const char *path, virtual InputStreamPtr OpenStream(const char *path,
Mutex &mutex, Cond &cond) = 0; Mutex &mutex, Cond &cond) = 0;
}; };
#endif #endif

View File

@ -53,8 +53,8 @@ public:
visitor.VisitArchiveEntry(name.c_str()); visitor.VisitArchiveEntry(name.c_str());
} }
InputStream *OpenStream(const char *path, InputStreamPtr OpenStream(const char *path,
Mutex &mutex, Cond &cond) override; Mutex &mutex, Cond &cond) override;
}; };
class Bzip2InputStream final : public InputStream { class Bzip2InputStream final : public InputStream {
@ -127,11 +127,11 @@ Bzip2InputStream::~Bzip2InputStream()
BZ2_bzDecompressEnd(&bzstream); BZ2_bzDecompressEnd(&bzstream);
} }
InputStream * InputStreamPtr
Bzip2ArchiveFile::OpenStream(const char *path, Bzip2ArchiveFile::OpenStream(const char *path,
Mutex &mutex, Cond &cond) Mutex &mutex, Cond &cond)
{ {
return new Bzip2InputStream(istream, path, mutex, cond); return std::make_unique<Bzip2InputStream>(istream, path, mutex, cond);
} }
inline bool inline bool

View File

@ -74,8 +74,8 @@ public:
virtual void Visit(ArchiveVisitor &visitor) override; virtual void Visit(ArchiveVisitor &visitor) override;
InputStream *OpenStream(const char *path, InputStreamPtr OpenStream(const char *path,
Mutex &mutex, Cond &cond) override; Mutex &mutex, Cond &cond) override;
}; };
/* archive open && listing routine */ /* archive open && listing routine */
@ -156,7 +156,7 @@ public:
size_t Read(void *ptr, size_t size) override; size_t Read(void *ptr, size_t size) override;
}; };
InputStream * InputStreamPtr
Iso9660ArchiveFile::OpenStream(const char *pathname, Iso9660ArchiveFile::OpenStream(const char *pathname,
Mutex &mutex, Cond &cond) Mutex &mutex, Cond &cond)
{ {
@ -165,8 +165,8 @@ Iso9660ArchiveFile::OpenStream(const char *pathname,
throw FormatRuntimeError("not found in the ISO file: %s", throw FormatRuntimeError("not found in the ISO file: %s",
pathname); pathname);
return new Iso9660InputStream(iso, pathname, mutex, cond, return std::make_unique<Iso9660InputStream>(iso, pathname, mutex, cond,
statbuf); statbuf);
} }
size_t size_t

View File

@ -59,8 +59,8 @@ public:
virtual void Visit(ArchiveVisitor &visitor) override; virtual void Visit(ArchiveVisitor &visitor) override;
InputStream *OpenStream(const char *path, InputStreamPtr OpenStream(const char *path,
Mutex &mutex, Cond &cond) override; Mutex &mutex, Cond &cond) override;
}; };
/* archive open && listing routine */ /* archive open && listing routine */
@ -116,7 +116,7 @@ public:
void Seek(offset_type offset) override; void Seek(offset_type offset) override;
}; };
InputStream * InputStreamPtr
ZzipArchiveFile::OpenStream(const char *pathname, ZzipArchiveFile::OpenStream(const char *pathname,
Mutex &mutex, Cond &cond) Mutex &mutex, Cond &cond)
{ {
@ -125,9 +125,9 @@ ZzipArchiveFile::OpenStream(const char *pathname,
throw FormatRuntimeError("not found in the ZIP file: %s", throw FormatRuntimeError("not found in the ZIP file: %s",
pathname); pathname);
return new ZzipInputStream(dir, pathname, return std::make_unique<ZzipInputStream>(dir, pathname,
mutex, cond, mutex, cond,
_file); _file);
} }
size_t size_t

View File

@ -20,6 +20,8 @@
#ifndef MPD_INPUT_PLUGIN_HXX #ifndef MPD_INPUT_PLUGIN_HXX
#define MPD_INPUT_PLUGIN_HXX #define MPD_INPUT_PLUGIN_HXX
#include "Ptr.hxx"
struct ConfigBlock; struct ConfigBlock;
class Mutex; class Mutex;
class Cond; class Cond;
@ -48,8 +50,8 @@ struct InputPlugin {
/** /**
* Throws std::runtime_error on error. * Throws std::runtime_error on error.
*/ */
InputStream *(*open)(const char *uri, InputStreamPtr (*open)(const char *uri,
Mutex &mutex, Cond &cond); Mutex &mutex, Cond &cond);
}; };
#endif #endif

View File

@ -39,11 +39,9 @@ InputStream::Open(const char *url,
} }
input_plugins_for_each_enabled(plugin) { input_plugins_for_each_enabled(plugin) {
InputStream *is; auto is = plugin->open(url, mutex, cond);
is = plugin->open(url, mutex, cond);
if (is != nullptr) if (is != nullptr)
return input_rewind_open(InputStreamPtr(is)); return input_rewind_open(std::move(is));
} }
throw std::runtime_error("Unrecognized URI"); throw std::runtime_error("Unrecognized URI");

View File

@ -110,8 +110,8 @@ public:
snd_pcm_close(capture_handle); snd_pcm_close(capture_handle);
} }
static InputStream *Create(EventLoop &event_loop, const char *uri, static InputStreamPtr Create(EventLoop &event_loop, const char *uri,
Mutex &mutex, Cond &cond); Mutex &mutex, Cond &cond);
protected: protected:
/* virtual methods from AsyncInputStream */ /* virtual methods from AsyncInputStream */
@ -146,7 +146,7 @@ private:
void DispatchSockets() noexcept override; void DispatchSockets() noexcept override;
}; };
inline InputStream * inline InputStreamPtr
AlsaInputStream::Create(EventLoop &event_loop, const char *uri, AlsaInputStream::Create(EventLoop &event_loop, const char *uri,
Mutex &mutex, Cond &cond) 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); snd_pcm_t *handle = OpenDevice(device, rate, format, channels);
int frame_size = snd_pcm_format_width(format) / 8 * channels; int frame_size = snd_pcm_format_width(format) / 8 * channels;
return new AlsaInputStream(event_loop, return std::make_unique<AlsaInputStream>(event_loop,
uri, mutex, cond, uri, mutex, cond,
device, handle, frame_size); device, handle, frame_size);
} }
std::chrono::steady_clock::duration std::chrono::steady_clock::duration
@ -396,7 +396,7 @@ alsa_input_init(EventLoop &event_loop, const ConfigBlock &)
alsa_input_event_loop = &event_loop; alsa_input_event_loop = &event_loop;
} }
static InputStream * static InputStreamPtr
alsa_input_open(const char *uri, Mutex &mutex, Cond &cond) alsa_input_open(const char *uri, Mutex &mutex, Cond &cond)
{ {
return AlsaInputStream::Create(*alsa_input_event_loop, uri, return AlsaInputStream::Create(*alsa_input_event_loop, uri,

View File

@ -66,10 +66,10 @@ OpenArchiveInputStream(Path path, Mutex &mutex, Cond &cond)
delete file; 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, input_archive_open(gcc_unused const char *filename,
gcc_unused Mutex &mutex, gcc_unused Cond &cond) gcc_unused Mutex &mutex, gcc_unused Cond &cond)
{ {

View File

@ -182,7 +182,7 @@ cdio_detect_device(void)
return path; return path;
} }
static InputStream * static InputStreamPtr
input_cdio_open(const char *uri, input_cdio_open(const char *uri,
Mutex &mutex, Cond &cond) Mutex &mutex, Cond &cond)
{ {
@ -250,9 +250,10 @@ input_cdio_open(const char *uri,
lsn_to = cdio_get_disc_last_lsn(cdio); lsn_to = cdio_get_disc_last_lsn(cdio);
} }
return new CdioParanoiaInputStream(uri, mutex, cond, return std::make_unique<CdioParanoiaInputStream>(uri, mutex, cond,
drv, cdio, reverse_endian, drv, cdio,
lsn_from, lsn_to); reverse_endian,
lsn_from, lsn_to);
} }
void void

View File

@ -88,7 +88,7 @@ struct CurlInputStream final : public AsyncInputStream, CurlResponseHandler {
CurlInputStream(const CurlInputStream &) = delete; CurlInputStream(const CurlInputStream &) = delete;
CurlInputStream &operator=(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 * 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) CurlInputStream::Open(const char *url, Mutex &mutex, Cond &cond)
{ {
auto c = std::make_unique<CurlInputStream>((*curl_init)->GetEventLoop(), auto c = std::make_unique<CurlInputStream>((*curl_init)->GetEventLoop(),
@ -447,10 +447,10 @@ CurlInputStream::Open(const char *url, Mutex &mutex, Cond &cond)
}); });
auto icy = c->icy; auto icy = c->icy;
return new IcyInputStream(std::move(c), std::move(icy)); return std::make_unique<IcyInputStream>(std::move(c), std::move(icy));
} }
static InputStream * static InputStreamPtr
input_curl_open(const char *url, Mutex &mutex, Cond &cond) input_curl_open(const char *url, Mutex &mutex, Cond &cond)
{ {
if (strncmp(url, "http://", 7) != 0 && if (strncmp(url, "http://", 7) != 0 &&

View File

@ -81,7 +81,7 @@ input_ffmpeg_init(EventLoop &, const ConfigBlock &)
throw PluginUnavailable("No protocol"); throw PluginUnavailable("No protocol");
} }
static InputStream * static InputStreamPtr
input_ffmpeg_open(const char *uri, input_ffmpeg_open(const char *uri,
Mutex &mutex, Cond &cond) Mutex &mutex, Cond &cond)
{ {
@ -98,7 +98,7 @@ input_ffmpeg_open(const char *uri,
if (result != 0) if (result != 0)
throw MakeFfmpegError(result); throw MakeFfmpegError(result);
return new FfmpegInputStream(uri, mutex, cond, h); return std::make_unique<FfmpegInputStream>(uri, mutex, cond, h);
} }
size_t size_t

View File

@ -70,12 +70,12 @@ OpenFileInputStream(Path path,
POSIX_FADV_SEQUENTIAL); POSIX_FADV_SEQUENTIAL);
#endif #endif
return InputStreamPtr(new FileInputStream(path.ToUTF8().c_str(), return std::make_unique<FileInputStream>(path.ToUTF8().c_str(),
std::move(reader), info.GetSize(), std::move(reader), info.GetSize(),
mutex, cond)); mutex, cond);
} }
static InputStream * static InputStreamPtr
input_file_open(gcc_unused const char *filename, input_file_open(gcc_unused const char *filename,
gcc_unused Mutex &mutex, gcc_unused Cond &cond) gcc_unused Mutex &mutex, gcc_unused Cond &cond)
{ {

View File

@ -66,7 +66,7 @@ MmsInputStream::Open()
SetMimeType("audio/x-ms-wma"); SetMimeType("audio/x-ms-wma");
} }
static InputStream * static InputStreamPtr
input_mms_open(const char *url, input_mms_open(const char *url,
Mutex &mutex, Cond &cond) Mutex &mutex, Cond &cond)
{ {
@ -76,7 +76,7 @@ input_mms_open(const char *url,
!StringStartsWith(url, "mmsu://")) !StringStartsWith(url, "mmsu://"))
return nullptr; return nullptr;
auto m = new MmsInputStream(url, mutex, cond); auto m = std::make_unique<MmsInputStream>(url, mutex, cond);
m->Start(); m->Start();
return m; return m;
} }

View File

@ -216,21 +216,15 @@ input_nfs_finish() noexcept
nfs_finish(); nfs_finish();
} }
static InputStream * static InputStreamPtr
input_nfs_open(const char *uri, input_nfs_open(const char *uri,
Mutex &mutex, Cond &cond) Mutex &mutex, Cond &cond)
{ {
if (!StringStartsWith(uri, "nfs://")) if (!StringStartsWith(uri, "nfs://"))
return nullptr; return nullptr;
NfsInputStream *is = new NfsInputStream(uri, mutex, cond); auto is = std::make_unique<NfsInputStream>(uri, mutex, cond);
try { is->Open();
is->Open();
} catch (...) {
delete is;
throw;
}
return is; return is;
} }

View File

@ -147,5 +147,5 @@ input_rewind_open(InputStreamPtr is)
/* seekable resources don't need this plugin */ /* seekable resources don't need this plugin */
return is; return is;
return InputStreamPtr(new RewindInputStream(std::move(is))); return std::make_unique<RewindInputStream>(std::move(is));
} }

View File

@ -83,7 +83,7 @@ input_smbclient_init(EventLoop &, const ConfigBlock &)
// TODO: evaluate ConfigBlock, call smbc_setOption*() // TODO: evaluate ConfigBlock, call smbc_setOption*()
} }
static InputStream * static InputStreamPtr
input_smbclient_open(const char *uri, input_smbclient_open(const char *uri,
Mutex &mutex, Cond &cond) Mutex &mutex, Cond &cond)
{ {
@ -119,7 +119,8 @@ input_smbclient_open(const char *uri,
throw MakeErrno(e, "smbc_fstat() failed"); throw MakeErrno(e, "smbc_fstat() failed");
} }
return new SmbclientInputStream(uri, mutex, cond, ctx, fd, st); return std::make_unique<SmbclientInputStream>(uri, mutex, cond,
ctx, fd, st);
} }
size_t size_t