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