Ed Tanous | c9b5521 | 2017-06-12 13:25:51 -0700 | [diff] [blame] | 1 | // Copyright (c) Benjamin Kietzman (github.com/bkietz) |
| 2 | // |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | #ifndef DBUS_ERROR_HPP |
| 7 | #define DBUS_ERROR_HPP |
| 8 | |
| 9 | #include <dbus/dbus.h> |
| 10 | #include <dbus/element.hpp> |
| 11 | #include <dbus/message.hpp> |
| 12 | #include <boost/system/error_code.hpp> |
| 13 | #include <boost/system/system_error.hpp> |
| 14 | |
| 15 | namespace dbus { |
| 16 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 17 | namespace detail { |
| 18 | |
| 19 | class error_category : public boost::system::error_category { |
| 20 | const char *name() const BOOST_SYSTEM_NOEXCEPT { return "dbus.error"; } |
| 21 | |
| 22 | string message(int value) const { |
| 23 | if (value) |
| 24 | return "DBus error"; |
| 25 | else |
| 26 | return "no error"; |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | } // namespace detail |
| 31 | |
| 32 | inline const boost::system::error_category &get_dbus_category() { |
| 33 | static detail::error_category instance; |
| 34 | return instance; |
| 35 | } |
| 36 | |
| 37 | class error { |
Ed Tanous | c9b5521 | 2017-06-12 13:25:51 -0700 | [diff] [blame] | 38 | DBusError error_; |
| 39 | |
| 40 | public: |
| 41 | error() { dbus_error_init(&error_); } |
| 42 | |
| 43 | error(DBusError *src) { |
| 44 | dbus_error_init(&error_); |
| 45 | dbus_move_error(src, &error_); |
| 46 | } |
| 47 | |
| 48 | error(dbus::message &m) { |
| 49 | dbus_error_init(&error_); |
| 50 | dbus_set_error_from_message(&error_, m); |
| 51 | } |
| 52 | |
| 53 | ~error() { dbus_error_free(&error_); } |
| 54 | |
Ed Tanous | c9b5521 | 2017-06-12 13:25:51 -0700 | [diff] [blame] | 55 | bool is_set() const { return dbus_error_is_set(&error_); } |
| 56 | |
| 57 | operator const DBusError *() const { return &error_; } |
| 58 | |
| 59 | operator DBusError *() { return &error_; } |
| 60 | |
| 61 | boost::system::error_code error_code() const; |
| 62 | boost::system::system_error system_error() const; |
| 63 | void throw_if_set() const; |
| 64 | }; |
| 65 | |
| 66 | inline boost::system::error_code error::error_code() const { |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 67 | return boost::system::error_code(is_set(), get_dbus_category()); |
Ed Tanous | c9b5521 | 2017-06-12 13:25:51 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | inline boost::system::system_error error::system_error() const { |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 71 | return boost::system::system_error( |
| 72 | error_code(), string(error_.name) + ":" + error_.message); |
Ed Tanous | c9b5521 | 2017-06-12 13:25:51 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | inline void error::throw_if_set() const { |
| 76 | if (is_set()) throw system_error(); |
| 77 | } |
| 78 | |
| 79 | } // namespace dbus |
| 80 | |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 81 | #endif // DBUS_ERROR_HPP |