xref: /openbmc/boost-dbus/include/dbus/error.hpp (revision 85860989)
1 #include <string>
2 #include <dbus/dbus.h>
3 #include <boost/system/error_code.hpp>
4 #include <boost/system/system_error.hpp>
5 
6 namespace dbus {
7 
8 using std::string;
9 
10 class error
11   : public boost::system::error_category
12 {
13   DBusError error_;
14 
15 public:
16   error()
17   {
18     dbus_error_init(&error_);
19   }
20 
21   error(DBusError *src_)
22   {
23     dbus_error_init(&error_);
24     dbus_move_error(src_, &error_);
25   }
26 
27   ~error()
28   {
29     dbus_error_free(&error_);
30   }
31 
32   const char *name() const BOOST_SYSTEM_NOEXCEPT
33   {
34     return error_.name;
35   }
36 
37   string message(int value) const
38   {
39     return error_.message;
40   }
41 
42   bool is_set() const
43   {
44     return dbus_error_is_set(&error_);
45   }
46 
47   operator bool() const
48   {
49     return is_set();
50   }
51 
52   operator const DBusError *() const
53   {
54     return &error_;
55   }
56 
57   operator DBusError *()
58   {
59     return &error_;
60   }
61 
62   boost::system::error_code error_code();
63   boost::system::system_error system_error();
64   void throw_if_set();
65 };
66 
67 boost::system::error_code error::error_code()
68 {
69   return boost::system::error_code(
70       is_set(),
71       *this);
72 }
73 
74 boost::system::system_error error::system_error()
75 {
76   return boost::system::system_error(error_code());
77 }
78 
79 void error::throw_if_set()
80 {
81   if(is_set()) throw system_error();
82 }
83 
84 } // namespace dbus
85