input/Proxy: use InputStreamPtr

This commit is contained in:
Max Kellermann 2017-12-26 11:37:29 +01:00
parent fb9a2c5431
commit 49619fbd77
9 changed files with 50 additions and 59 deletions

View File

@ -22,9 +22,9 @@
#include "IcyMetaDataParser.hxx"
#include "tag/Tag.hxx"
IcyInputStream::IcyInputStream(InputStream *_input,
IcyInputStream::IcyInputStream(InputStreamPtr _input,
std::shared_ptr<IcyMetaDataParser> _parser) noexcept
:ProxyInputStream(_input), parser(std::move(_parser))
:ProxyInputStream(std::move(_input)), parser(std::move(_parser))
{
}

View File

@ -53,7 +53,7 @@ public:
* needs to feed parameters (e.g. from the "icy-metaint"
* header) into it
*/
IcyInputStream(InputStream *_input,
IcyInputStream(InputStreamPtr _input,
std::shared_ptr<IcyMetaDataParser> _parser) noexcept;
virtual ~IcyInputStream() noexcept;

View File

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

View File

@ -21,77 +21,77 @@
#include "ProxyInputStream.hxx"
#include "tag/Tag.hxx"
ProxyInputStream::ProxyInputStream(InputStream *_input) noexcept
ProxyInputStream::ProxyInputStream(InputStreamPtr _input) noexcept
:InputStream(_input->GetURI(), _input->mutex, _input->cond),
input(*_input) {}
ProxyInputStream::~ProxyInputStream() noexcept
input(std::move(_input))
{
delete &input;
assert(input);
}
ProxyInputStream::~ProxyInputStream() noexcept = default;
void
ProxyInputStream::CopyAttributes()
{
if (input.IsReady()) {
if (input->IsReady()) {
if (!IsReady()) {
if (input.HasMimeType())
SetMimeType(input.GetMimeType());
if (input->HasMimeType())
SetMimeType(input->GetMimeType());
size = input.KnownSize()
? input.GetSize()
size = input->KnownSize()
? input->GetSize()
: UNKNOWN_SIZE;
seekable = input.IsSeekable();
seekable = input->IsSeekable();
SetReady();
}
offset = input.GetOffset();
offset = input->GetOffset();
}
}
void
ProxyInputStream::Check()
{
input.Check();
input->Check();
}
void
ProxyInputStream::Update() noexcept
{
input.Update();
input->Update();
CopyAttributes();
}
void
ProxyInputStream::Seek(offset_type new_offset)
{
input.Seek(new_offset);
input->Seek(new_offset);
CopyAttributes();
}
bool
ProxyInputStream::IsEOF() noexcept
{
return input.IsEOF();
return input->IsEOF();
}
std::unique_ptr<Tag>
ProxyInputStream::ReadTag()
{
return input.ReadTag();
return input->ReadTag();
}
bool
ProxyInputStream::IsAvailable() noexcept
{
return input.IsAvailable();
return input->IsAvailable();
}
size_t
ProxyInputStream::Read(void *ptr, size_t read_size)
{
size_t nbytes = input.Read(ptr, read_size);
size_t nbytes = input->Read(ptr, read_size);
CopyAttributes();
return nbytes;
}

View File

@ -21,6 +21,7 @@
#define MPD_PROXY_INPUT_STREAM_HXX
#include "InputStream.hxx"
#include "Ptr.hxx"
struct Tag;
@ -31,11 +32,11 @@ struct Tag;
*/
class ProxyInputStream : public InputStream {
protected:
InputStream &input;
InputStreamPtr input;
public:
gcc_nonnull_all
ProxyInputStream(InputStream *_input) noexcept;
explicit ProxyInputStream(InputStreamPtr _input) noexcept;
virtual ~ProxyInputStream() noexcept;

View File

@ -438,21 +438,16 @@ CurlInputStream::DoSeek(offset_type new_offset)
inline InputStream *
CurlInputStream::Open(const char *url, Mutex &mutex, Cond &cond)
{
CurlInputStream *c = new CurlInputStream((*curl_init)->GetEventLoop(),
url, mutex, cond);
auto c = std::make_unique<CurlInputStream>((*curl_init)->GetEventLoop(),
url, mutex, cond);
try {
BlockingCall(c->GetEventLoop(), [c](){
c->InitEasy();
c->StartRequest();
});
} catch (...) {
delete c;
throw;
}
BlockingCall(c->GetEventLoop(), [&c](){
c->InitEasy();
c->StartRequest();
});
auto icy = c->icy;
return new IcyInputStream(c, std::move(icy));
return new IcyInputStream(std::move(c), std::move(icy));
}
static InputStream *

View File

@ -47,9 +47,8 @@ class RewindInputStream final : public ProxyInputStream {
char buffer[64 * 1024];
public:
RewindInputStream(InputStream *_input)
:ProxyInputStream(_input) {
}
explicit RewindInputStream(InputStreamPtr _input)
:ProxyInputStream(std::move(_input)) {}
/* virtual methods from InputStream */
@ -71,7 +70,7 @@ private:
* buffer contain more data for the next read operation?
*/
bool ReadingFromBuffer() const noexcept {
return tail > 0 && offset < input.GetOffset();
return tail > 0 && offset < input->GetOffset();
}
};
@ -82,7 +81,7 @@ RewindInputStream::Read(void *ptr, size_t read_size)
/* buffered read */
assert(head == (size_t)offset);
assert(tail == (size_t)input.GetOffset());
assert(tail == (size_t)input->GetOffset());
if (read_size > tail - head)
read_size = tail - head;
@ -95,9 +94,9 @@ RewindInputStream::Read(void *ptr, size_t read_size)
} else {
/* pass method call to underlying stream */
size_t nbytes = input.Read(ptr, read_size);
size_t nbytes = input->Read(ptr, read_size);
if (input.GetOffset() > (offset_type)sizeof(buffer))
if (input->GetOffset() > (offset_type)sizeof(buffer))
/* disable buffering */
tail = 0;
else if (tail == (size_t)offset) {
@ -106,7 +105,7 @@ RewindInputStream::Read(void *ptr, size_t read_size)
memcpy(buffer + tail, ptr, nbytes);
tail += nbytes;
assert(tail == (size_t)input.GetOffset());
assert(tail == (size_t)input->GetOffset());
}
CopyAttributes();
@ -125,7 +124,7 @@ RewindInputStream::Seek(offset_type new_offset)
assert(!ReadingFromBuffer() ||
head == (size_t)offset);
assert(tail == (size_t)input.GetOffset());
assert(tail == (size_t)input->GetOffset());
head = (size_t)new_offset;
offset = new_offset;
@ -138,8 +137,8 @@ RewindInputStream::Seek(offset_type new_offset)
}
}
InputStream *
input_rewind_open(InputStream *is)
InputStreamPtr
input_rewind_open(InputStreamPtr is)
{
assert(is != nullptr);
assert(!is->IsReady() || is->GetOffset() == 0);
@ -148,5 +147,5 @@ input_rewind_open(InputStream *is)
/* seekable resources don't need this plugin */
return is;
return new RewindInputStream(is);
return InputStreamPtr(new RewindInputStream(std::move(is)));
}

View File

@ -28,10 +28,9 @@
#define MPD_INPUT_REWIND_HXX
#include "check.h"
#include "input/Ptr.hxx"
class InputStream;
InputStream *
input_rewind_open(InputStream *is);
InputStreamPtr
input_rewind_open(InputStreamPtr is);
#endif

View File

@ -61,8 +61,8 @@ public:
"foo bar");
CPPUNIT_ASSERT(sis->IsReady());
InputStream *ris = input_rewind_open(sis);
CPPUNIT_ASSERT(ris != sis);
auto ris = input_rewind_open(InputStreamPtr(sis));
CPPUNIT_ASSERT(ris.get() != sis);
CPPUNIT_ASSERT(ris != nullptr);
const std::lock_guard<Mutex> protect(mutex);