xref: /openbmc/boost-dbus/include/dbus/error.hpp (revision da3eeb6a)
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 
17 class error : public boost::system::error_category {
18   DBusError error_;
19 
20  public:
21   error() { dbus_error_init(&error_); }
22 
23   error(DBusError *src) {
24     dbus_error_init(&error_);
25     dbus_move_error(src, &error_);
26   }
27 
28   error(dbus::message &m) {
29     dbus_error_init(&error_);
30     dbus_set_error_from_message(&error_, m);
31   }
32 
33   ~error() { dbus_error_free(&error_); }
34 
35   const char *name() const BOOST_SYSTEM_NOEXCEPT { return error_.name; }
36 
37   string message(int value) const { return error_.message; }
38 
39   bool is_set() const { return dbus_error_is_set(&error_); }
40 
41   operator const DBusError *() const { return &error_; }
42 
43   operator DBusError *() { return &error_; }
44 
45   boost::system::error_code error_code() const;
46   boost::system::system_error system_error() const;
47   void throw_if_set() const;
48 };
49 
50 inline boost::system::error_code error::error_code() const {
51   return boost::system::error_code(is_set(), *this);
52 }
53 
54 inline boost::system::system_error error::system_error() const {
55   return boost::system::system_error(error_code());
56 }
57 
58 inline void error::throw_if_set() const {
59   if (is_set()) throw system_error();
60 }
61 
62 }  // namespace dbus
63 
64 #endif  // DBUS_ERROR_HPP
65