blob: 63563dad34fee09c89fbd8e56fbf3044d8d71dfc [file] [log] [blame]
Ed Tanousc9b55212017-06-12 13:25:51 -07001// 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
15namespace dbus {
16
Ed Tanous3dac7492017-08-02 13:46:20 -070017namespace detail {
18
19class 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
32inline const boost::system::error_category &get_dbus_category() {
33 static detail::error_category instance;
34 return instance;
35}
36
37class error {
Ed Tanousc9b55212017-06-12 13:25:51 -070038 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 Tanousc9b55212017-06-12 13:25:51 -070055 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
66inline boost::system::error_code error::error_code() const {
Ed Tanous3dac7492017-08-02 13:46:20 -070067 return boost::system::error_code(is_set(), get_dbus_category());
Ed Tanousc9b55212017-06-12 13:25:51 -070068}
69
70inline boost::system::system_error error::system_error() const {
Ed Tanous3dac7492017-08-02 13:46:20 -070071 return boost::system::system_error(
72 error_code(), string(error_.name) + ":" + error_.message);
Ed Tanousc9b55212017-06-12 13:25:51 -070073}
74
75inline void error::throw_if_set() const {
76 if (is_set()) throw system_error();
77}
78
79} // namespace dbus
80
Ed Tanous3dac7492017-08-02 13:46:20 -070081#endif // DBUS_ERROR_HPP