input: wrap InputStream in std::unique_ptr

This commit is contained in:
Max Kellermann
2016-02-21 08:03:32 +01:00
parent 054e9ecaae
commit cadc67ea40
19 changed files with 107 additions and 103 deletions

View File

@@ -22,6 +22,7 @@
#include "check.h"
#include "Offset.hxx"
#include "Ptr.hxx"
#include "thread/Mutex.hxx"
#include "Compiler.h"
@@ -123,18 +124,17 @@ public:
* @return an #InputStream object on success, nullptr on error
*/
gcc_nonnull_all
gcc_malloc
static InputStream *Open(const char *uri, Mutex &mutex, Cond &cond,
Error &error);
static InputStreamPtr Open(const char *uri, Mutex &mutex, Cond &cond,
Error &error);
/**
* Just like Open(), but waits for the stream to become ready.
* It is a wrapper for Open(), WaitReady() and Check().
*/
gcc_malloc gcc_nonnull_all
static InputStream *OpenReady(const char *uri,
Mutex &mutex, Cond &cond,
Error &error);
gcc_nonnull_all
static InputStreamPtr OpenReady(const char *uri,
Mutex &mutex, Cond &cond,
Error &error);
/**
* The absolute URI which was used to open this stream.

View File

@@ -35,19 +35,19 @@
#include <errno.h>
#endif
InputStream *
InputStreamPtr
OpenLocalInputStream(Path path, Mutex &mutex, Cond &cond, Error &error)
{
assert(!error.IsDefined());
InputStream *is = OpenFileInputStream(path, mutex, cond, error);
InputStreamPtr is(OpenFileInputStream(path, mutex, cond, error));
#ifdef ENABLE_ARCHIVE
if (is == nullptr && error.IsDomain(errno_domain) &&
error.GetCode() == ENOTDIR) {
/* ENOTDIR means this may be a path inside an archive
file */
Error error2;
is = OpenArchiveInputStream(path, mutex, cond, error2);
is.reset(OpenArchiveInputStream(path, mutex, cond, error2));
if (is == nullptr && error2.IsDefined())
error = std::move(error2);
}

View File

@@ -21,8 +21,8 @@
#define MPD_INPUT_LOCAL_OPEN_HXX
#include "check.h"
#include "Ptr.hxx"
class InputStream;
class Path;
class Mutex;
class Cond;
@@ -32,7 +32,7 @@ class Error;
* Open a "local" file. This is a wrapper for the input plugins
* "file" and "archive".
*/
InputStream *
InputStreamPtr
OpenLocalInputStream(Path path, Mutex &mutex, Cond &cond, Error &error);
#endif

View File

@@ -30,7 +30,7 @@
#include "util/Error.hxx"
#include "util/Domain.hxx"
InputStream *
InputStreamPtr
InputStream::Open(const char *url,
Mutex &mutex, Cond &cond,
Error &error)
@@ -51,7 +51,7 @@ InputStream::Open(const char *url,
if (is != nullptr) {
is = input_rewind_open(is);
return is;
return InputStreamPtr(is);
} else if (error.IsDefined())
return nullptr;
}
@@ -60,12 +60,12 @@ InputStream::Open(const char *url,
return nullptr;
}
InputStream *
InputStreamPtr
InputStream::OpenReady(const char *uri,
Mutex &mutex, Cond &cond,
Error &error)
{
InputStream *is = Open(uri, mutex, cond, error);
auto is = Open(uri, mutex, cond, error);
if (is == nullptr)
return nullptr;
@@ -74,10 +74,8 @@ InputStream::OpenReady(const char *uri,
bool success = is->Check(error);
mutex.unlock();
if (!success) {
delete is;
is = nullptr;
}
if (!success)
is.reset();
return is;
}

29
src/input/Ptr.hxx Normal file
View File

@@ -0,0 +1,29 @@
/*
* Copyright (C) 2003-2015 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_INPUT_STREAM_PTR_HXX
#define MPD_INPUT_STREAM_PTR_HXX
#include <memory>
class InputStream;
typedef std::unique_ptr<InputStream> InputStreamPtr;
#endif