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