lib/pulse/Error: use std::system_error

This commit is contained in:
Max Kellermann 2022-11-29 09:51:26 +01:00
parent e455b93fac
commit cf3f3a7750
3 changed files with 57 additions and 26 deletions

View File

@ -23,9 +23,20 @@
#include <pulse/context.h>
#include <pulse/error.h>
std::runtime_error
MakePulseError(pa_context *context, const char *prefix) noexcept
namespace Pulse {
std::string
ErrorCategory::message(int condition) const
{
const int e = pa_context_errno(context);
return FormatRuntimeError("%s: %s", prefix, pa_strerror(e));
return pa_strerror(condition);
}
ErrorCategory error_category;
std::system_error
MakeError(pa_context *context, const char *msg) noexcept
{
return MakeError(pa_context_errno(context), msg);
}
} // namespace Pulse

View File

@ -17,14 +17,34 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_PULSE_ERROR_HXX
#define MPD_PULSE_ERROR_HXX
#pragma once
#include <stdexcept>
#include <system_error>
struct pa_context;
std::runtime_error
MakePulseError(pa_context *context, const char *prefix) noexcept;
namespace Pulse {
#endif
class ErrorCategory final : public std::error_category {
public:
const char *name() const noexcept override {
return "pulse";
}
std::string message(int condition) const override;
};
extern ErrorCategory error_category;
[[nodiscard]] [[gnu::pure]]
inline std::system_error
MakeError(int error, const char *msg) noexcept
{
return std::system_error(error, error_category, msg);
}
[[nodiscard]] [[gnu::pure]]
std::system_error
MakeError(pa_context *context, const char *msg) noexcept;
} // namespace Pulse

View File

@ -368,7 +368,7 @@ PulseOutput::Connect()
if (pa_context_connect(context, server,
(pa_context_flags_t)0, nullptr) < 0)
throw MakePulseError(context,
throw Pulse::MakeError(context,
"pa_context_connect() has failed");
}
@ -501,7 +501,7 @@ PulseOutput::WaitConnection()
case PA_CONTEXT_FAILED:
/* failure */
{
auto e = MakePulseError(context,
auto e = Pulse::MakeError(context,
"failed to connect");
DeleteContext();
throw e;
@ -603,7 +603,7 @@ PulseOutput::SetupStream(const pa_sample_spec &ss)
PA_CHANNEL_MAP_WAVEEX);
stream = pa_stream_new(context, name, &ss, &chan_map);
if (stream == nullptr)
throw MakePulseError(context,
throw Pulse::MakeError(context,
"pa_stream_new() has failed");
pa_stream_set_suspended_callback(stream,
@ -682,7 +682,7 @@ PulseOutput::Open(AudioFormat &audio_format)
nullptr, nullptr) < 0) {
DeleteStream();
throw MakePulseError(context,
throw Pulse::MakeError(context,
"pa_stream_connect_playback() has failed");
}
@ -729,7 +729,7 @@ PulseOutput::WaitStream()
case PA_STREAM_FAILED:
case PA_STREAM_TERMINATED:
case PA_STREAM_UNCONNECTED:
throw MakePulseError(context,
throw Pulse::MakeError(context,
"failed to connect the stream");
case PA_STREAM_CREATING:
@ -752,11 +752,11 @@ PulseOutput::StreamPause(bool _pause)
pa_operation *o = pa_stream_cork(stream, _pause,
pulse_output_stream_success_cb, this);
if (o == nullptr)
throw MakePulseError(context,
throw Pulse::MakeError(context,
"pa_stream_cork() has failed");
if (!pulse_wait_for_operation(mainloop, o))
throw MakePulseError(context,
throw Pulse::MakeError(context,
"pa_stream_cork() has failed");
}
@ -819,7 +819,7 @@ PulseOutput::Play(std::span<const std::byte> src)
int result = pa_stream_write(stream, src.data(), src.size(), nullptr,
0, PA_SEEK_RELATIVE);
if (result < 0)
throw MakePulseError(context, "pa_stream_write() failed");
throw Pulse::MakeError(context, "pa_stream_write() failed");
return src.size();
}
@ -838,7 +838,7 @@ PulseOutput::Drain()
pa_stream_drain(stream,
pulse_output_stream_success_cb, this);
if (o == nullptr)
throw MakePulseError(context, "pa_stream_drain() failed");
throw Pulse::MakeError(context, "pa_stream_drain() failed");
pulse_wait_for_operation(mainloop, o);
}