xref: /openbmc/boost-dbus/include/dbus/message.hpp (revision 44d24258)
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_MESSAGE_HPP
7 #define DBUS_MESSAGE_HPP
8 
9 #include <dbus/dbus.h>
10 #include <dbus/element.hpp>
11 #include <dbus/endpoint.hpp>
12 #include <boost/intrusive_ptr.hpp>
13 #include <dbus/impl/message_iterator.hpp>
14 
15 void intrusive_ptr_add_ref(DBusMessage *m)
16 {
17   dbus_message_ref(m);
18 }
19 
20 void intrusive_ptr_release(DBusMessage *m)
21 {
22   dbus_message_unref(m);
23 }
24 
25 namespace dbus {
26 
27 class message
28 {
29   boost::intrusive_ptr<DBusMessage> message_;
30 public:
31 
32   /// Create a method call message
33   static message new_call(
34       const endpoint& destination,
35       const string& method_name);
36 
37   /// Create a method return message
38   static message new_return(
39       message& call);
40 
41   /// Create an error message
42   static message new_error(
43       message& call,
44       const string& error_name,
45       const string& error_message);
46 
47   /// Create a signal message
48   static message new_signal(
49       const endpoint& origin,
50       const string& signal_name);
51 
52   message() {}
53 
54   message(DBusMessage *m)
55     : message_(dbus_message_ref(m))
56   {
57   }
58 
59   operator DBusMessage *()
60   {
61     return message_.get();
62   }
63 
64   operator const DBusMessage *() const
65   {
66     return message_.get();
67   }
68 
69   string get_path() const
70   {
71     return dbus_message_get_path(message_.get());
72   }
73 
74   string get_interface() const
75   {
76     return dbus_message_get_interface(message_.get());
77   }
78 
79   string get_member() const
80   {
81     return dbus_message_get_member(message_.get());
82   }
83 
84   string get_sender() const
85   {
86     return dbus_message_get_sender(message_.get());
87   }
88 
89   string get_destination() const
90   {
91     return dbus_message_get_destination(message_.get());
92   }
93 
94   uint32 get_serial()
95   {
96     return dbus_message_get_serial(message_.get());
97   }
98 
99   message& set_serial(uint32 serial)
100   {
101     dbus_message_set_serial(message_.get(), serial);
102     return *this;
103   }
104 
105   uint32 get_reply_serial()
106   {
107     return dbus_message_get_reply_serial(message_.get());
108   }
109 
110   message& set_reply_serial(uint32 reply_serial)
111   {
112     dbus_message_set_reply_serial(message_.get(), reply_serial);
113     return *this;
114   }
115 
116   struct packer
117   {
118     impl::message_iterator iter_;
119     packer(message& m);
120     template<typename Element> packer& pack(const Element& e)
121     {
122       return *this << e;
123     }
124   };
125   struct unpacker
126   {
127     impl::message_iterator iter_;
128     unpacker(message& m);
129     template<typename Element> unpacker& unpack(Element& e)
130     {
131       return *this >> e;
132     }
133   };
134 
135   template<typename Element> packer pack(const Element& e)
136   {
137     return packer(*this).pack(e);
138   }
139 
140   template<typename Element> unpacker unpack(Element& e)
141   {
142     return unpacker(*this).unpack(e);
143   }
144 
145 };
146 
147 template<typename Element>
148 message::packer operator<<(message m, const Element& e)
149 {
150   return message::packer(m).pack(e);
151 }
152 
153 template<typename Element>
154 message::unpacker operator>>(message m, Element& e)
155 {
156   return message::unpacker(m).unpack(e);
157 }
158 
159 } // namespace dbus
160 
161 #include <dbus/impl/packer.ipp>
162 #include <dbus/impl/unpacker.ipp>
163 #include <dbus/impl/message.ipp>
164 #include <dbus/impl/message_iterator.ipp>
165 
166 #endif // DBUS_MESSAGE_HPP
167