input/InputStream: ReadTag() returns std::unique_ptr<Tag>

This commit is contained in:
Max Kellermann 2017-12-20 15:15:27 +01:00
parent 4c4fa68268
commit 73e69edac3
11 changed files with 41 additions and 37 deletions

View File

@ -223,7 +223,7 @@ bool
DecoderBridge::UpdateStreamTag(InputStream *is)
{
auto *tag = is != nullptr
? is->LockReadTag()
? is->LockReadTag().release()
: nullptr;
if (tag == nullptr) {
tag = song_tag;

View File

@ -45,15 +45,19 @@ AsyncInputStream::AsyncInputStream(EventLoop &event_loop, const char *_url,
AsyncInputStream::~AsyncInputStream()
{
delete tag;
buffer.Clear();
}
void
AsyncInputStream::SetTag(Tag *_tag) noexcept
AsyncInputStream::SetTag(std::unique_ptr<Tag> _tag) noexcept
{
delete std::exchange(tag, _tag);
tag = std::move(_tag);
}
void
AsyncInputStream::ClearTag() noexcept
{
tag.reset();
}
void
@ -151,7 +155,7 @@ AsyncInputStream::SeekDone() noexcept
cond.broadcast();
}
Tag *
std::unique_ptr<Tag>
AsyncInputStream::ReadTag()
{
return std::exchange(tag, nullptr);

View File

@ -61,7 +61,7 @@ class AsyncInputStream : public InputStream {
* The #Tag object ready to be requested via
* InputStream::ReadTag().
*/
Tag *tag = nullptr;
std::unique_ptr<Tag> tag;
offset_type seek_offset;
@ -84,7 +84,7 @@ public:
void Check() final;
bool IsEOF() noexcept final;
void Seek(offset_type new_offset) final;
Tag *ReadTag() final;
std::unique_ptr<Tag> ReadTag() final;
bool IsAvailable() noexcept final;
size_t Read(void *ptr, size_t read_size) final;
@ -92,11 +92,8 @@ protected:
/**
* Pass an tag from the I/O thread to the client thread.
*/
void SetTag(Tag *_tag) noexcept;
void ClearTag() noexcept {
SetTag(nullptr);
}
void SetTag(std::unique_ptr<Tag> _tag) noexcept;
void ClearTag() noexcept;
void Pause() noexcept;

View File

@ -38,22 +38,23 @@ IcyInputStream::Update()
offset = override_offset;
}
Tag *
std::unique_ptr<Tag>
IcyInputStream::ReadTag()
{
Tag *new_input_tag = ProxyInputStream::ReadTag();
auto new_input_tag = ProxyInputStream::ReadTag();
if (!IsEnabled())
return new_input_tag;
const bool had_new_input_tag = !!new_input_tag;
if (new_input_tag != nullptr)
input_tag.reset(new_input_tag);
input_tag = std::move(new_input_tag);
auto new_icy_tag = parser.ReadTag();
const bool had_new_icy_tag = !!new_icy_tag;
if (new_icy_tag != nullptr)
icy_tag = std::move(new_icy_tag);
if (new_input_tag == nullptr && !had_new_icy_tag)
if (!had_new_input_tag && !had_new_icy_tag)
/* no change */
return nullptr;
@ -62,12 +63,12 @@ IcyInputStream::ReadTag()
return nullptr;
if (input_tag == nullptr)
return new Tag(*icy_tag);
return std::make_unique<Tag>(*icy_tag);
if (icy_tag == nullptr)
return new Tag(*input_tag);
return std::make_unique<Tag>(*input_tag);
return Tag::Merge(*input_tag, *icy_tag).release();
return Tag::Merge(*input_tag, *icy_tag);
}
size_t

View File

@ -63,7 +63,7 @@ public:
/* virtual methods from InputStream */
void Update() override;
Tag *ReadTag() override;
std::unique_ptr<Tag> ReadTag() override;
size_t Read(void *ptr, size_t size) override;
};

View File

@ -19,6 +19,7 @@
#include "config.h"
#include "InputStream.hxx"
#include "tag/Tag.hxx"
#include "thread/Cond.hxx"
#include "util/StringCompare.hxx"
@ -107,13 +108,13 @@ InputStream::LockSkip(offset_type _offset)
Skip(_offset);
}
Tag *
std::unique_ptr<Tag>
InputStream::ReadTag()
{
return nullptr;
}
Tag *
std::unique_ptr<Tag>
InputStream::LockReadTag()
{
const std::lock_guard<Mutex> protect(mutex);

View File

@ -27,6 +27,7 @@
#include "Compiler.h"
#include <string>
#include <memory>
#include <assert.h>
@ -322,18 +323,16 @@ public:
*
* The caller must lock the mutex.
*
* @return a tag object which must be freed by the caller, or
* nullptr if the tag has not changed since the last call
* @return a tag object or nullptr if the tag has not changed
* since the last call
*/
gcc_malloc
virtual Tag *ReadTag();
virtual std::unique_ptr<Tag> ReadTag();
/**
* Wrapper for ReadTag() which locks and unlocks the mutex;
* the caller must not be holding it already.
*/
gcc_malloc
Tag *LockReadTag();
std::unique_ptr<Tag> LockReadTag();
/**
* Returns true if the next read operation will not block: either data

View File

@ -19,6 +19,7 @@
#include "config.h"
#include "ProxyInputStream.hxx"
#include "tag/Tag.hxx"
ProxyInputStream::ProxyInputStream(InputStream *_input)
:InputStream(_input->GetURI(), _input->mutex, _input->cond),
@ -75,7 +76,7 @@ ProxyInputStream::IsEOF() noexcept
return input.IsEOF();
}
Tag *
std::unique_ptr<Tag>
ProxyInputStream::ReadTag()
{
return input.ReadTag();

View File

@ -47,7 +47,7 @@ public:
void Update() override;
void Seek(offset_type new_offset) override;
bool IsEOF() noexcept override;
Tag *ReadTag() override;
std::unique_ptr<Tag> ReadTag() override;
bool IsAvailable() noexcept override;
size_t Read(void *ptr, size_t read_size) override;

View File

@ -223,7 +223,7 @@ CurlInputStream::OnHeaders(unsigned status,
TagBuilder tag_builder;
tag_builder.AddItem(TAG_NAME, i->second.c_str());
SetTag(tag_builder.CommitNew().release());
SetTag(tag_builder.CommitNew());
}
if (!icy->IsEnabled()) {

View File

@ -82,11 +82,12 @@ dump_input_stream(InputStream *is)
/* read data and tags from the stream */
while (!is->IsEOF()) {
Tag *tag = is->ReadTag();
if (tag != NULL) {
fprintf(stderr, "Received a tag:\n");
tag_save(stderr, *tag);
delete tag;
{
auto tag = is->ReadTag();
if (tag) {
fprintf(stderr, "Received a tag:\n");
tag_save(stderr, *tag);
}
}
char buffer[4096];