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