Commit 8549ccfd authored by Max Kellermann's avatar Max Kellermann

playlist/CloseSongEnumerator: new wrapper class

Simplifies a lot of code, because we don't need to return both the SongEnumerator and the InputStream.
parent ffd16b55
......@@ -1150,6 +1150,8 @@ endif
libplaylist_plugins_a_SOURCES = \
src/playlist/PlaylistPlugin.hxx \
src/playlist/SongEnumerator.hxx \
src/playlist/CloseSongEnumerator.cxx \
src/playlist/CloseSongEnumerator.hxx \
src/playlist/MemorySongEnumerator.cxx \
src/playlist/MemorySongEnumerator.hxx \
src/playlist/plugins/ExtM3uPlaylistPlugin.cxx \
......
/*
* Copyright (C) 2003-2014 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "CloseSongEnumerator.hxx"
#include "input/InputStream.hxx"
CloseSongEnumerator::~CloseSongEnumerator()
{
delete other;
is->Close();
}
DetachedSong *
CloseSongEnumerator::NextSong()
{
return other->NextSong();
}
/*
* Copyright (C) 2003-2014 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_CLOSE_SONG_ENUMERATOR_HXX
#define MPD_CLOSE_SONG_ENUMERATOR_HXX
#include "SongEnumerator.hxx"
#include "Compiler.h"
struct InputStream;
/**
* A #SongEnumerator wrapper that closes an #InputStream automatically
* after deleting the #SongEnumerator
*/
class CloseSongEnumerator final : public SongEnumerator {
SongEnumerator *const other;
InputStream *const is;
public:
gcc_nonnull_all
CloseSongEnumerator(SongEnumerator *_other, InputStream *const _is)
:other(_other), is(_is) {}
virtual ~CloseSongEnumerator();
virtual DetachedSong *NextSong() override;
};
#endif
......@@ -21,6 +21,7 @@
#include "PlaylistAny.hxx"
#include "PlaylistMapper.hxx"
#include "PlaylistRegistry.hxx"
#include "CloseSongEnumerator.hxx"
#include "util/UriUtil.hxx"
#include "util/Error.hxx"
#include "input/InputStream.hxx"
......@@ -29,16 +30,13 @@
#include <assert.h>
static SongEnumerator *
playlist_open_remote(const char *uri, Mutex &mutex, Cond &cond,
InputStream **is_r)
playlist_open_remote(const char *uri, Mutex &mutex, Cond &cond)
{
assert(uri_has_scheme(uri));
SongEnumerator *playlist = playlist_list_open_uri(uri, mutex, cond);
if (playlist != nullptr) {
*is_r = nullptr;
if (playlist != nullptr)
return playlist;
}
Error error;
InputStream *is = InputStream::OpenReady(uri, mutex, cond, error);
......@@ -55,15 +53,13 @@ playlist_open_remote(const char *uri, Mutex &mutex, Cond &cond,
return nullptr;
}
*is_r = is;
return playlist;
return new CloseSongEnumerator(playlist, is);
}
SongEnumerator *
playlist_open_any(const char *uri, Mutex &mutex, Cond &cond,
InputStream **is_r)
playlist_open_any(const char *uri, Mutex &mutex, Cond &cond)
{
return uri_has_scheme(uri)
? playlist_open_remote(uri, mutex, cond, is_r)
: playlist_mapper_open(uri, mutex, cond, is_r);
? playlist_open_remote(uri, mutex, cond)
: playlist_mapper_open(uri, mutex, cond);
}
......@@ -23,19 +23,13 @@
class Mutex;
class Cond;
class SongEnumerator;
struct InputStream;
/**
* Opens a playlist from the specified URI, which can be either an
* absolute remote URI (with a scheme) or a relative path to the
* music orplaylist directory.
*
* @param is_r on success, an input_stream object may be returned
* here, which must be closed after the playlist_provider object is
* freed
*/
SongEnumerator *
playlist_open_any(const char *uri, Mutex &mutex, Cond &cond,
InputStream **is_r);
playlist_open_any(const char *uri, Mutex &mutex, Cond &cond);
#endif
......@@ -28,14 +28,11 @@
#include <assert.h>
static SongEnumerator *
playlist_open_path(const char *path_fs, Mutex &mutex, Cond &cond,
InputStream **is_r)
playlist_open_path(const char *path_fs, Mutex &mutex, Cond &cond)
{
auto playlist = playlist_list_open_uri(path_fs, mutex, cond);
if (playlist != nullptr)
*is_r = nullptr;
else
playlist = playlist_list_open_path(path_fs, mutex, cond, is_r);
if (playlist == nullptr)
playlist = playlist_list_open_path(path_fs, mutex, cond);
return playlist;
}
......@@ -44,8 +41,7 @@ playlist_open_path(const char *path_fs, Mutex &mutex, Cond &cond,
* Load a playlist from the configured playlist directory.
*/
static SongEnumerator *
playlist_open_in_playlist_dir(const char *uri, Mutex &mutex, Cond &cond,
InputStream **is_r)
playlist_open_in_playlist_dir(const char *uri, Mutex &mutex, Cond &cond)
{
assert(spl_valid_name(uri));
......@@ -61,7 +57,7 @@ playlist_open_in_playlist_dir(const char *uri, Mutex &mutex, Cond &cond,
AllocatedPath::Build(playlist_directory_fs, uri_fs);
assert(!path_fs.IsNull());
return playlist_open_path(path_fs.c_str(), mutex, cond, is_r);
return playlist_open_path(path_fs.c_str(), mutex, cond);
}
#ifdef ENABLE_DATABASE
......@@ -70,8 +66,7 @@ playlist_open_in_playlist_dir(const char *uri, Mutex &mutex, Cond &cond,
* Load a playlist from the configured music directory.
*/
static SongEnumerator *
playlist_open_in_music_dir(const char *uri, Mutex &mutex, Cond &cond,
InputStream **is_r)
playlist_open_in_music_dir(const char *uri, Mutex &mutex, Cond &cond)
{
assert(uri_safe_local(uri));
......@@ -79,26 +74,24 @@ playlist_open_in_music_dir(const char *uri, Mutex &mutex, Cond &cond,
if (path.IsNull())
return nullptr;
return playlist_open_path(path.c_str(), mutex, cond, is_r);
return playlist_open_path(path.c_str(), mutex, cond);
}
#endif
SongEnumerator *
playlist_mapper_open(const char *uri, Mutex &mutex, Cond &cond,
InputStream **is_r)
playlist_mapper_open(const char *uri, Mutex &mutex, Cond &cond)
{
if (spl_valid_name(uri)) {
auto playlist = playlist_open_in_playlist_dir(uri, mutex, cond,
is_r);
auto playlist = playlist_open_in_playlist_dir(uri,
mutex, cond);
if (playlist != nullptr)
return playlist;
}
#ifdef ENABLE_DATABASE
if (uri_safe_local(uri)) {
auto playlist = playlist_open_in_music_dir(uri, mutex, cond,
is_r);
auto playlist = playlist_open_in_music_dir(uri, mutex, cond);
if (playlist != nullptr)
return playlist;
}
......
......@@ -23,18 +23,12 @@
class Mutex;
class Cond;
class SongEnumerator;
struct InputStream;
/**
* Opens a playlist from an URI relative to the playlist or music
* directory.
*
* @param is_r on success, an input_stream object may be returned
* here, which must be closed after the playlist_provider object is
* freed
*/
SongEnumerator *
playlist_mapper_open(const char *uri, Mutex &mutex, Cond &cond,
InputStream **is_r);
playlist_mapper_open(const char *uri, Mutex &mutex, Cond &cond);
#endif
......@@ -22,9 +22,9 @@
#include "PlaylistAny.hxx"
#include "PlaylistSong.hxx"
#include "Playlist.hxx"
#include "input/InputStream.hxx"
#include "SongEnumerator.hxx"
#include "DetachedSong.hxx"
#include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
#include "fs/Traits.hxx"
......@@ -72,8 +72,7 @@ playlist_open_into_queue(const char *uri,
Mutex mutex;
Cond cond;
InputStream *is;
auto playlist = playlist_open_any(uri, mutex, cond, &is);
auto playlist = playlist_open_any(uri, mutex, cond);
if (playlist == nullptr)
return PlaylistResult::NO_SUCH_LIST;
......@@ -82,9 +81,5 @@ playlist_open_into_queue(const char *uri,
start_index, end_index,
dest, pc, loader);
delete playlist;
if (is != nullptr)
is->Close();
return result;
}
......@@ -20,6 +20,7 @@
#include "config.h"
#include "PlaylistRegistry.hxx"
#include "PlaylistPlugin.hxx"
#include "CloseSongEnumerator.hxx"
#include "plugins/ExtM3uPlaylistPlugin.hxx"
#include "plugins/M3uPlaylistPlugin.hxx"
#include "plugins/XspfPlaylistPlugin.hxx"
......@@ -279,8 +280,7 @@ playlist_suffix_supported(const char *suffix)
}
SongEnumerator *
playlist_list_open_path(const char *path_fs, Mutex &mutex, Cond &cond,
InputStream **is_r)
playlist_list_open_path(const char *path_fs, Mutex &mutex, Cond &cond)
{
const char *suffix;
......@@ -301,7 +301,7 @@ playlist_list_open_path(const char *path_fs, Mutex &mutex, Cond &cond,
auto playlist = playlist_list_open_stream_suffix(*is, suffix);
if (playlist != nullptr)
*is_r = is;
playlist = new CloseSongEnumerator(playlist, is);
else
is->Close();
......
......@@ -77,7 +77,6 @@ playlist_suffix_supported(const char *suffix);
* @return a playlist, or nullptr on error
*/
SongEnumerator *
playlist_list_open_path(const char *path_fs, Mutex &mutex, Cond &cond,
InputStream **is_r);
playlist_list_open_path(const char *path_fs, Mutex &mutex, Cond &cond);
#endif
......@@ -23,10 +23,10 @@
#include "PlaylistSong.hxx"
#include "SongEnumerator.hxx"
#include "SongPrint.hxx"
#include "input/InputStream.hxx"
#include "DetachedSong.hxx"
#include "SongLoader.hxx"
#include "fs/Traits.hxx"
#include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
static void
......@@ -59,16 +59,11 @@ playlist_file_print(Client &client, const char *uri, bool detail)
Mutex mutex;
Cond cond;
InputStream *is;
SongEnumerator *playlist = playlist_open_any(uri, mutex, cond, &is);
SongEnumerator *playlist = playlist_open_any(uri, mutex, cond);
if (playlist == nullptr)
return false;
playlist_provider_print(client, uri, *playlist, detail);
delete playlist;
if (is != nullptr)
is->Close();
return true;
}
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