xref: /openbmc/boost-dbus/include/dbus/error.hpp (revision 16d80fe9)
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
18   : public boost::system::error_category
19 {
20   DBusError error_;
21 
22 public:
23   error()
24   {
25     dbus_error_init(&error_);
26   }
27 
28   error(DBusError *src)
29   {
30     dbus_error_init(&error_);
31     dbus_move_error(src, &error_);
32   }
33 
34   error(dbus::message& m)
35   {
36     dbus_error_init(&error_);
37     dbus_set_error_from_message(&error_, m);
38   }
39 
40   ~error()
41   {
42     dbus_error_free(&error_);
43   }
44 
45   const char *name() const BOOST_SYSTEM_NOEXCEPT
46   {
47     return error_.name;
48   }
49 
50   string message(int value) const
51   {
52     return error_.message;
53   }
54 
55   bool is_set() const
56   {
57     return dbus_error_is_set(&error_);
58   }
59 
60   operator const DBusError *() const
61   {
62     return &error_;
63   }
64 
65   operator DBusError *()
66   {
67     return &error_;
68   }
69 
70   boost::system::error_code error_code();
71   boost::system::system_error system_error();
72   void throw_if_set();
73 };
74 
75 boost::system::error_code error::error_code()
76 {
77   return boost::system::error_code(
78       is_set(),
79       *this);
80 }
81 
82 boost::system::system_error error::system_error()
83 {
84   return boost::system::system_error(error_code());
85 }
86 
87 void error::throw_if_set()
88 {
89   if(is_set()) throw system_error();
90 }
91 
92 } // namespace dbus
93 
94 #endif // DBUS_ERROR_HPP
95