Commit 49619fbd authored by Max Kellermann's avatar Max Kellermann

input/Proxy: use InputStreamPtr

parent fb9a2c54
...@@ -22,9 +22,9 @@ ...@@ -22,9 +22,9 @@
#include "IcyMetaDataParser.hxx" #include "IcyMetaDataParser.hxx"
#include "tag/Tag.hxx" #include "tag/Tag.hxx"
IcyInputStream::IcyInputStream(InputStream *_input, IcyInputStream::IcyInputStream(InputStreamPtr _input,
std::shared_ptr<IcyMetaDataParser> _parser) noexcept std::shared_ptr<IcyMetaDataParser> _parser) noexcept
:ProxyInputStream(_input), parser(std::move(_parser)) :ProxyInputStream(std::move(_input)), parser(std::move(_parser))
{ {
} }
......
...@@ -53,7 +53,7 @@ public: ...@@ -53,7 +53,7 @@ public:
* needs to feed parameters (e.g. from the "icy-metaint" * needs to feed parameters (e.g. from the "icy-metaint"
* header) into it * header) into it
*/ */
IcyInputStream(InputStream *_input, IcyInputStream(InputStreamPtr _input,
std::shared_ptr<IcyMetaDataParser> _parser) noexcept; std::shared_ptr<IcyMetaDataParser> _parser) noexcept;
virtual ~IcyInputStream() noexcept; virtual ~IcyInputStream() noexcept;
......
...@@ -42,11 +42,8 @@ InputStream::Open(const char *url, ...@@ -42,11 +42,8 @@ InputStream::Open(const char *url,
InputStream *is; InputStream *is;
is = plugin->open(url, mutex, cond); is = plugin->open(url, mutex, cond);
if (is != nullptr) { if (is != nullptr)
is = input_rewind_open(is); return input_rewind_open(InputStreamPtr(is));
return InputStreamPtr(is);
}
} }
throw std::runtime_error("Unrecognized URI"); throw std::runtime_error("Unrecognized URI");
......
...@@ -21,77 +21,77 @@ ...@@ -21,77 +21,77 @@
#include "ProxyInputStream.hxx" #include "ProxyInputStream.hxx"
#include "tag/Tag.hxx" #include "tag/Tag.hxx"
ProxyInputStream::ProxyInputStream(InputStream *_input) noexcept ProxyInputStream::ProxyInputStream(InputStreamPtr _input) noexcept
:InputStream(_input->GetURI(), _input->mutex, _input->cond), :InputStream(_input->GetURI(), _input->mutex, _input->cond),
input(*_input) {} input(std::move(_input))
ProxyInputStream::~ProxyInputStream() noexcept
{ {
delete &input; assert(input);
} }
ProxyInputStream::~ProxyInputStream() noexcept = default;
void void
ProxyInputStream::CopyAttributes() ProxyInputStream::CopyAttributes()
{ {
if (input.IsReady()) { if (input->IsReady()) {
if (!IsReady()) { if (!IsReady()) {
if (input.HasMimeType()) if (input->HasMimeType())
SetMimeType(input.GetMimeType()); SetMimeType(input->GetMimeType());
size = input.KnownSize() size = input->KnownSize()
? input.GetSize() ? input->GetSize()
: UNKNOWN_SIZE; : UNKNOWN_SIZE;
seekable = input.IsSeekable(); seekable = input->IsSeekable();
SetReady(); SetReady();
} }
offset = input.GetOffset(); offset = input->GetOffset();
} }
} }
void void
ProxyInputStream::Check() ProxyInputStream::Check()
{ {
input.Check(); input->Check();
} }
void void
ProxyInputStream::Update() noexcept ProxyInputStream::Update() noexcept
{ {
input.Update(); input->Update();
CopyAttributes(); CopyAttributes();
} }
void void
ProxyInputStream::Seek(offset_type new_offset) ProxyInputStream::Seek(offset_type new_offset)
{ {
input.Seek(new_offset); input->Seek(new_offset);
CopyAttributes(); CopyAttributes();
} }
bool bool
ProxyInputStream::IsEOF() noexcept ProxyInputStream::IsEOF() noexcept
{ {
return input.IsEOF(); return input->IsEOF();
} }
std::unique_ptr<Tag> std::unique_ptr<Tag>
ProxyInputStream::ReadTag() ProxyInputStream::ReadTag()
{ {
return input.ReadTag(); return input->ReadTag();
} }
bool bool
ProxyInputStream::IsAvailable() noexcept ProxyInputStream::IsAvailable() noexcept
{ {
return input.IsAvailable(); return input->IsAvailable();
} }
size_t size_t
ProxyInputStream::Read(void *ptr, size_t read_size) 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(); CopyAttributes();
return nbytes; return nbytes;
} }
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#define MPD_PROXY_INPUT_STREAM_HXX #define MPD_PROXY_INPUT_STREAM_HXX
#include "InputStream.hxx" #include "InputStream.hxx"
#include "Ptr.hxx"
struct Tag; struct Tag;
...@@ -31,11 +32,11 @@ struct Tag; ...@@ -31,11 +32,11 @@ struct Tag;
*/ */
class ProxyInputStream : public InputStream { class ProxyInputStream : public InputStream {
protected: protected:
InputStream &input; InputStreamPtr input;
public: public:
gcc_nonnull_all gcc_nonnull_all
ProxyInputStream(InputStream *_input) noexcept; explicit ProxyInputStream(InputStreamPtr _input) noexcept;
virtual ~ProxyInputStream() noexcept; virtual ~ProxyInputStream() noexcept;
......
...@@ -438,21 +438,16 @@ CurlInputStream::DoSeek(offset_type new_offset) ...@@ -438,21 +438,16 @@ CurlInputStream::DoSeek(offset_type new_offset)
inline InputStream * inline InputStream *
CurlInputStream::Open(const char *url, Mutex &mutex, Cond &cond) CurlInputStream::Open(const char *url, Mutex &mutex, Cond &cond)
{ {
CurlInputStream *c = new CurlInputStream((*curl_init)->GetEventLoop(), auto c = std::make_unique<CurlInputStream>((*curl_init)->GetEventLoop(),
url, mutex, cond); url, mutex, cond);
try { BlockingCall(c->GetEventLoop(), [&c](){
BlockingCall(c->GetEventLoop(), [c](){ c->InitEasy();
c->InitEasy(); c->StartRequest();
c->StartRequest(); });
});
} catch (...) {
delete c;
throw;
}
auto icy = c->icy; auto icy = c->icy;
return new IcyInputStream(c, std::move(icy)); return new IcyInputStream(std::move(c), std::move(icy));
} }
static InputStream * static InputStream *
......
...@@ -47,9 +47,8 @@ class RewindInputStream final : public ProxyInputStream { ...@@ -47,9 +47,8 @@ class RewindInputStream final : public ProxyInputStream {
char buffer[64 * 1024]; char buffer[64 * 1024];
public: public:
RewindInputStream(InputStream *_input) explicit RewindInputStream(InputStreamPtr _input)
:ProxyInputStream(_input) { :ProxyInputStream(std::move(_input)) {}
}
/* virtual methods from InputStream */ /* virtual methods from InputStream */
...@@ -71,7 +70,7 @@ private: ...@@ -71,7 +70,7 @@ private:
* buffer contain more data for the next read operation? * buffer contain more data for the next read operation?
*/ */
bool ReadingFromBuffer() const noexcept { 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) ...@@ -82,7 +81,7 @@ RewindInputStream::Read(void *ptr, size_t read_size)
/* buffered read */ /* buffered read */
assert(head == (size_t)offset); assert(head == (size_t)offset);
assert(tail == (size_t)input.GetOffset()); assert(tail == (size_t)input->GetOffset());
if (read_size > tail - head) if (read_size > tail - head)
read_size = tail - head; read_size = tail - head;
...@@ -95,9 +94,9 @@ RewindInputStream::Read(void *ptr, size_t read_size) ...@@ -95,9 +94,9 @@ RewindInputStream::Read(void *ptr, size_t read_size)
} else { } else {
/* pass method call to underlying stream */ /* 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 */ /* disable buffering */
tail = 0; tail = 0;
else if (tail == (size_t)offset) { else if (tail == (size_t)offset) {
...@@ -106,7 +105,7 @@ RewindInputStream::Read(void *ptr, size_t read_size) ...@@ -106,7 +105,7 @@ RewindInputStream::Read(void *ptr, size_t read_size)
memcpy(buffer + tail, ptr, nbytes); memcpy(buffer + tail, ptr, nbytes);
tail += nbytes; tail += nbytes;
assert(tail == (size_t)input.GetOffset()); assert(tail == (size_t)input->GetOffset());
} }
CopyAttributes(); CopyAttributes();
...@@ -125,7 +124,7 @@ RewindInputStream::Seek(offset_type new_offset) ...@@ -125,7 +124,7 @@ RewindInputStream::Seek(offset_type new_offset)
assert(!ReadingFromBuffer() || assert(!ReadingFromBuffer() ||
head == (size_t)offset); head == (size_t)offset);
assert(tail == (size_t)input.GetOffset()); assert(tail == (size_t)input->GetOffset());
head = (size_t)new_offset; head = (size_t)new_offset;
offset = new_offset; offset = new_offset;
...@@ -138,8 +137,8 @@ RewindInputStream::Seek(offset_type new_offset) ...@@ -138,8 +137,8 @@ RewindInputStream::Seek(offset_type new_offset)
} }
} }
InputStream * InputStreamPtr
input_rewind_open(InputStream *is) input_rewind_open(InputStreamPtr is)
{ {
assert(is != nullptr); assert(is != nullptr);
assert(!is->IsReady() || is->GetOffset() == 0); assert(!is->IsReady() || is->GetOffset() == 0);
...@@ -148,5 +147,5 @@ input_rewind_open(InputStream *is) ...@@ -148,5 +147,5 @@ input_rewind_open(InputStream *is)
/* seekable resources don't need this plugin */ /* seekable resources don't need this plugin */
return is; return is;
return new RewindInputStream(is); return InputStreamPtr(new RewindInputStream(std::move(is)));
} }
...@@ -28,10 +28,9 @@ ...@@ -28,10 +28,9 @@
#define MPD_INPUT_REWIND_HXX #define MPD_INPUT_REWIND_HXX
#include "check.h" #include "check.h"
#include "input/Ptr.hxx"
class InputStream; InputStreamPtr
input_rewind_open(InputStreamPtr is);
InputStream *
input_rewind_open(InputStream *is);
#endif #endif
...@@ -61,8 +61,8 @@ public: ...@@ -61,8 +61,8 @@ public:
"foo bar"); "foo bar");
CPPUNIT_ASSERT(sis->IsReady()); CPPUNIT_ASSERT(sis->IsReady());
InputStream *ris = input_rewind_open(sis); auto ris = input_rewind_open(InputStreamPtr(sis));
CPPUNIT_ASSERT(ris != sis); CPPUNIT_ASSERT(ris.get() != sis);
CPPUNIT_ASSERT(ris != nullptr); CPPUNIT_ASSERT(ris != nullptr);
const std::lock_guard<Mutex> protect(mutex); const std::lock_guard<Mutex> protect(mutex);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment