diff options
author | Max Kellermann <max@duempel.org> | 2013-08-10 18:02:44 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2013-09-04 18:14:22 +0200 |
commit | 29030b54c98b0aee65fbc10ebf7ba36bed98c02c (patch) | |
tree | 79766830b55ebca38ddbce84d8d548227eedb69e /src/system | |
parent | c9fcc7f14860777458153eb2d13c773ccfa1daa2 (diff) |
util/Error: new error passing library
Replaces GLib's GError.
Diffstat (limited to 'src/system')
-rw-r--r-- | src/system/FatalError.cxx | 8 | ||||
-rw-r--r-- | src/system/FatalError.hxx | 7 | ||||
-rw-r--r-- | src/system/Resolver.cxx | 19 | ||||
-rw-r--r-- | src/system/Resolver.hxx | 14 | ||||
-rw-r--r-- | src/system/SocketError.cxx | 24 | ||||
-rw-r--r-- | src/system/SocketError.hxx | 36 | ||||
-rw-r--r-- | src/system/SocketUtil.cxx | 16 | ||||
-rw-r--r-- | src/system/SocketUtil.hxx | 5 |
8 files changed, 80 insertions, 49 deletions
diff --git a/src/system/FatalError.cxx b/src/system/FatalError.cxx index 30ddc50ea..65cb9aa26 100644 --- a/src/system/FatalError.cxx +++ b/src/system/FatalError.cxx @@ -17,7 +17,9 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include "config.h" #include "FatalError.hxx" +#include "util/Error.hxx" #include <glib.h> @@ -53,6 +55,12 @@ FormatFatalError(const char *fmt, ...) } void +FatalError(const Error &error) +{ + FatalError(error.GetMessage()); +} + +void FatalError(GError *error) { FatalError(error->message); diff --git a/src/system/FatalError.hxx b/src/system/FatalError.hxx index 3918e5fc1..be67aabbd 100644 --- a/src/system/FatalError.hxx +++ b/src/system/FatalError.hxx @@ -20,9 +20,12 @@ #ifndef MPD_FATAL_ERROR_HXX #define MPD_FATAL_ERROR_HXX +#include "check.h" #include "gerror.h" #include "gcc.h" +class Error; + /** * Log the specified message and abort the process. */ @@ -36,6 +39,10 @@ FormatFatalError(const char *fmt, ...); gcc_noreturn void +FatalError(const Error &error); + +gcc_noreturn +void FatalError(GError *error); gcc_noreturn diff --git a/src/system/Resolver.cxx b/src/system/Resolver.cxx index a574e82c1..b7fa80c38 100644 --- a/src/system/Resolver.cxx +++ b/src/system/Resolver.cxx @@ -19,6 +19,10 @@ #include "config.h" #include "Resolver.hxx" +#include "util/Error.hxx" +#include "util/Domain.hxx" + +#include <glib.h> #ifndef G_OS_WIN32 #include <sys/socket.h> @@ -30,8 +34,10 @@ #include <string.h> +const Domain resolver_domain("resolver"); + char * -sockaddr_to_string(const struct sockaddr *sa, size_t length, GError **error) +sockaddr_to_string(const struct sockaddr *sa, size_t length, Error &error) { #if defined(HAVE_IPV6) && defined(IN6_IS_ADDR_V4MAPPED) const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *)sa; @@ -59,8 +65,7 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length, GError **error) ret = getnameinfo(sa, length, host, sizeof(host), serv, sizeof(serv), NI_NUMERICHOST|NI_NUMERICSERV); if (ret != 0) { - g_set_error(error, g_quark_from_static_string("netdb"), ret, - "%s", gai_strerror(ret)); + error.Set(resolver_domain, ret, gai_strerror(ret)); return NULL; } @@ -82,7 +87,7 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length, GError **error) struct addrinfo * resolve_host_port(const char *host_port, unsigned default_port, int flags, int socktype, - GError **error_r) + Error &error) { char *p = g_strdup(host_port); const char *host = p, *port = NULL; @@ -130,9 +135,9 @@ resolve_host_port(const char *host_port, unsigned default_port, int ret = getaddrinfo(host, port, &hints, &ai); g_free(p); if (ret != 0) { - g_set_error(error_r, resolver_quark(), ret, - "Failed to look up '%s': %s", - host_port, gai_strerror(ret)); + error.Format(resolver_domain, ret, + "Failed to look up '%s': %s", + host_port, gai_strerror(ret)); return NULL; } diff --git a/src/system/Resolver.hxx b/src/system/Resolver.hxx index a1cd00329..38803dcd1 100644 --- a/src/system/Resolver.hxx +++ b/src/system/Resolver.hxx @@ -22,17 +22,13 @@ #include "gcc.h" -#include <glib.h> +#include <stddef.h> struct sockaddr; struct addrinfo; +class Error; -gcc_const -static inline GQuark -resolver_quark(void) -{ - return g_quark_from_static_string("resolver"); -} +extern const class Domain resolver_domain; /** * Converts the specified socket address into a string in the form @@ -46,7 +42,7 @@ resolver_quark(void) */ gcc_malloc char * -sockaddr_to_string(const struct sockaddr *sa, size_t length, GError **error); +sockaddr_to_string(const struct sockaddr *sa, size_t length, Error &error); /** * Resolve a specification in the form "host", "host:port", @@ -61,6 +57,6 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length, GError **error); struct addrinfo * resolve_host_port(const char *host_port, unsigned default_port, int flags, int socktype, - GError **error_r); + Error &error); #endif diff --git a/src/system/SocketError.cxx b/src/system/SocketError.cxx new file mode 100644 index 000000000..d3569c6f1 --- /dev/null +++ b/src/system/SocketError.cxx @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2003-2013 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 "SocketError.hxx" +#include "util/Domain.hxx" + +const Domain socket_domain("socket"); diff --git a/src/system/SocketError.hxx b/src/system/SocketError.hxx index 53a0c1d8f..c42e267dd 100644 --- a/src/system/SocketError.hxx +++ b/src/system/SocketError.hxx @@ -21,6 +21,7 @@ #define MPD_SOCKET_ERROR_HXX #include "gcc.h" +#include "util/Error.hxx" #include <glib.h> @@ -33,15 +34,10 @@ typedef int socket_error_t; #endif /** - * A GQuark for GError for socket I/O errors. The code is an errno + * A #Domain for #Error for socket I/O errors. The code is an errno * value (or WSAGetLastError() on Windows). */ -gcc_const -static inline GQuark -SocketErrorQuark(void) -{ - return g_quark_from_static_string("socket"); -} +extern const class Domain socket_domain; gcc_pure static inline socket_error_t @@ -121,33 +117,29 @@ public: }; static inline void -SetSocketError(GError **error_r, socket_error_t code) +SetSocketError(Error &error, socket_error_t code) { -#ifdef WIN32 - if (error_r == NULL) - return; -#endif - const SocketErrorMessage msg(code); - g_set_error_literal(error_r, SocketErrorQuark(), code, msg); + error.Set(socket_domain, code, msg); } static inline void -SetSocketError(GError **error_r) +SetSocketError(Error &error) { - SetSocketError(error_r, GetSocketError()); + SetSocketError(error, GetSocketError()); } -gcc_malloc -static inline GError * +gcc_const +static inline Error NewSocketError(socket_error_t code) { - const SocketErrorMessage msg(code); - return g_error_new_literal(SocketErrorQuark(), code, msg); + Error error; + SetSocketError(error, code); + return error; } -gcc_malloc -static inline GError * +gcc_pure +static inline Error NewSocketError() { return NewSocketError(GetSocketError()); diff --git a/src/system/SocketUtil.cxx b/src/system/SocketUtil.cxx index dd7eb7dd2..abaedb8e7 100644 --- a/src/system/SocketUtil.cxx +++ b/src/system/SocketUtil.cxx @@ -41,38 +41,38 @@ int socket_bind_listen(int domain, int type, int protocol, const struct sockaddr *address, size_t address_length, int backlog, - GError **error_r) + Error &error) { int fd, ret; const int reuse = 1; fd = socket_cloexec_nonblock(domain, type, protocol); if (fd < 0) { - SetSocketError(error_r); - g_prefix_error(error_r, "Failed to create socket: "); + SetSocketError(error); + error.AddPrefix("Failed to create socket: "); return -1; } ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *) &reuse, sizeof(reuse)); if (ret < 0) { - SetSocketError(error_r); - g_prefix_error(error_r, "setsockopt() failed: "); + SetSocketError(error); + error.AddPrefix("setsockopt() failed: "); close_socket(fd); return -1; } ret = bind(fd, address, address_length); if (ret < 0) { - SetSocketError(error_r); + SetSocketError(error); close_socket(fd); return -1; } ret = listen(fd, backlog); if (ret < 0) { - SetSocketError(error_r); - g_prefix_error(error_r, "listen() failed: "); + SetSocketError(error); + error.AddPrefix("listen() failed: "); close_socket(fd); return -1; } diff --git a/src/system/SocketUtil.hxx b/src/system/SocketUtil.hxx index 3d0be78c4..5e582ec0d 100644 --- a/src/system/SocketUtil.hxx +++ b/src/system/SocketUtil.hxx @@ -26,11 +26,10 @@ #ifndef MPD_SOCKET_UTIL_HXX #define MPD_SOCKET_UTIL_HXX -#include "gerror.h" - #include <stddef.h> struct sockaddr; +class Error; /** * Creates a socket listening on the specified address. This is a @@ -50,7 +49,7 @@ int socket_bind_listen(int domain, int type, int protocol, const struct sockaddr *address, size_t address_length, int backlog, - GError **error); + Error &error); int socket_keepalive(int fd); |