diff options
author | Max Kellermann <max@duempel.org> | 2014-02-07 20:27:24 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2014-02-07 20:27:24 +0100 |
commit | 8549ccfd8c24c5666435ac4dd00ba3c7d14e0351 (patch) | |
tree | 3b7820e956b3b494dff1a21be97baa311f1ff1cb | |
parent | ffd16b55a69a01b906805752acc11e26491138bc (diff) | |
download | mpd-8549ccfd8c24c5666435ac4dd00ba3c7d14e0351.tar.gz mpd-8549ccfd8c24c5666435ac4dd00ba3c7d14e0351.tar.xz mpd-8549ccfd8c24c5666435ac4dd00ba3c7d14e0351.zip |
playlist/CloseSongEnumerator: new wrapper class
Simplifies a lot of code, because we don't need to return both the
SongEnumerator and the InputStream.
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | src/playlist/CloseSongEnumerator.cxx | 34 | ||||
-rw-r--r-- | src/playlist/CloseSongEnumerator.hxx | 47 | ||||
-rw-r--r-- | src/playlist/PlaylistAny.cxx | 18 | ||||
-rw-r--r-- | src/playlist/PlaylistAny.hxx | 8 | ||||
-rw-r--r-- | src/playlist/PlaylistMapper.cxx | 29 | ||||
-rw-r--r-- | src/playlist/PlaylistMapper.hxx | 8 | ||||
-rw-r--r-- | src/playlist/PlaylistQueue.cxx | 9 | ||||
-rw-r--r-- | src/playlist/PlaylistRegistry.cxx | 6 | ||||
-rw-r--r-- | src/playlist/PlaylistRegistry.hxx | 3 | ||||
-rw-r--r-- | src/playlist/Print.cxx | 9 |
11 files changed, 111 insertions, 62 deletions
diff --git a/Makefile.am b/Makefile.am index 374c09f52..19e4b255d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/playlist/CloseSongEnumerator.cxx b/src/playlist/CloseSongEnumerator.cxx new file mode 100644 index 000000000..6a4042395 --- /dev/null +++ b/src/playlist/CloseSongEnumerator.cxx @@ -0,0 +1,34 @@ +/* + * 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(); +} diff --git a/src/playlist/CloseSongEnumerator.hxx b/src/playlist/CloseSongEnumerator.hxx new file mode 100644 index 000000000..5a95cc28b --- /dev/null +++ b/src/playlist/CloseSongEnumerator.hxx @@ -0,0 +1,47 @@ +/* + * 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 diff --git a/src/playlist/PlaylistAny.cxx b/src/playlist/PlaylistAny.cxx index 04efbb569..671802e7d 100644 --- a/src/playlist/PlaylistAny.cxx +++ b/src/playlist/PlaylistAny.cxx @@ -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); } diff --git a/src/playlist/PlaylistAny.hxx b/src/playlist/PlaylistAny.hxx index c472afb31..6ee792993 100644 --- a/src/playlist/PlaylistAny.hxx +++ b/src/playlist/PlaylistAny.hxx @@ -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 diff --git a/src/playlist/PlaylistMapper.cxx b/src/playlist/PlaylistMapper.cxx index ec32abd32..1211e892c 100644 --- a/src/playlist/PlaylistMapper.cxx +++ b/src/playlist/PlaylistMapper.cxx @@ -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; } diff --git a/src/playlist/PlaylistMapper.hxx b/src/playlist/PlaylistMapper.hxx index a460cb124..01c982763 100644 --- a/src/playlist/PlaylistMapper.hxx +++ b/src/playlist/PlaylistMapper.hxx @@ -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 diff --git a/src/playlist/PlaylistQueue.cxx b/src/playlist/PlaylistQueue.cxx index 9adea317f..85bf7ca72 100644 --- a/src/playlist/PlaylistQueue.cxx +++ b/src/playlist/PlaylistQueue.cxx @@ -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; } diff --git a/src/playlist/PlaylistRegistry.cxx b/src/playlist/PlaylistRegistry.cxx index 55064849b..6b594e3c5 100644 --- a/src/playlist/PlaylistRegistry.cxx +++ b/src/playlist/PlaylistRegistry.cxx @@ -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(); diff --git a/src/playlist/PlaylistRegistry.hxx b/src/playlist/PlaylistRegistry.hxx index 0079fa68e..d8a24f4af 100644 --- a/src/playlist/PlaylistRegistry.hxx +++ b/src/playlist/PlaylistRegistry.hxx @@ -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 diff --git a/src/playlist/Print.cxx b/src/playlist/Print.cxx index b5398e67d..5031a4b94 100644 --- a/src/playlist/Print.cxx +++ b/src/playlist/Print.cxx @@ -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; } |