message.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-or-later OR BSD-3-Clause
2 /***************************************************************************
3  * Copyright (C) 2007,2009,2010 by Rick L. Vinyard, Jr. *
4  * rvinyard@cs.nmsu.edu *
5  * *
6  * This file is part of the dbus-cxx library. *
7  ***************************************************************************/
8 #include <stdint.h>
9 #include <dbus-cxx/error.h>
12 #include <memory>
13 #include <string>
14 #include "enums.h"
15 
16 #include <dbus-cxx/variant.h>
17 
18 #ifndef DBUSCXX_MESSAGE_H
19 #define DBUSCXX_MESSAGE_H
20 
21 #define DBUSCXX_MESSAGE_NO_REPLY_EXPECTED 0x01
22 #define DBUSCXX_MESSAGE_NO_AUTO_START_FLAG 0x02
23 
24 namespace DBus {
25 class ReturnMessage;
26 
43 class Message {
44 protected:
45 
46  Message();
47 
48 public:
49 
50  virtual ~Message();
51 
52  bool operator == ( const Message& other );
53 
54  bool is_valid() const;
55 
56  void invalidate();
57 
58  operator bool() const;
59 
60  uint32_t serial() const;
61 
62  virtual MessageType type() const = 0;
63 
64  void set_auto_start( bool auto_start );
65 
72  bool auto_start();
73 
81  bool set_destination( const std::string& s );
82 
83  std::string destination() const;
84 
85  std::string sender() const;
86 
87  Signature signature() const;
88 
89  template <typename T>
90  MessageIterator operator>>( T& value ) const {
91  MessageIterator iter = this->begin();
92  iter >> value;
93  return iter;
94  }
95 
96  template <typename T>
97  MessageAppendIterator operator<<( const T& value ) {
98  MessageAppendIterator aiter( *this );
99  aiter << value;
100  return aiter;
101  }
102 
103  MessageIterator begin() const;
104 
105  MessageIterator end() const;
106 
108 
119  bool serialize_to_vector( std::vector<uint8_t>* vec, uint32_t serial ) const;
120 
129 
134  uint8_t flags() const;
135 
144 
145  Endianess endianess() const;
146 
147  const std::vector<int>& filedescriptors() const;
148 
149  static std::shared_ptr<Message> create_from_data( uint8_t* data, uint32_t data_len, std::vector<int> fds = std::vector<int>() );
150 
151 protected:
152 
153  void append_signature( std::string toappend );
154 
158  void clear_sig_and_data();
159 
160  void set_flags( uint8_t flags );
161 
162 private:
163  std::vector<uint8_t>* body();
164  const std::vector<uint8_t>* body() const;
165  void add_filedescriptor( int fd );
166  uint32_t filedescriptors_size() const;
167  int filedescriptor_at_location( int location ) const;
168 
169 private:
170  class priv_data;
171 
172  DBUS_CXX_PROPAGATE_CONST( std::unique_ptr<priv_data> ) m_priv;
173 
174  friend class MessageAppendIterator;
175  friend class MessageIterator;
176  friend std::ostream& operator<<( std::ostream& os, const DBus::Message* msg );
177 
178 };
179 
180 
181 template <typename T>
182 inline
183 DBus::MessageIterator operator>>( std::shared_ptr<const DBus::Message> ptr, T& value ) {
184  if( !ptr ) { throw DBus::ErrorInvalidSharedPtr(); }
185 
186  return ( *ptr ) >> value;
187 }
188 
189 template <typename T>
190 inline
191 DBus::MessageAppendIterator operator<<( std::shared_ptr<DBus::Message> ptr, const T& value ) {
192  if( !ptr ) { throw DBus::ErrorInvalidSharedPtr(); }
193 
194  return ( *ptr ) << value;
195 }
196 
197 }
198 
199 #endif
Insertion iterator allow values to be appended to a message.
Definition: messageappenditerator.h:38
Extraction iterator allowing values to be retrieved from a message.
Definition: messageiterator.h:56
This class represents a basic DBus message and also serves as a base class for the specialized messag...
Definition: message.h:43
Variant header_field(MessageHeaderFields field) const
Returns the given header field(if it exists), otherwise returns a default constructed variant.
Definition: message.cpp:378
Signature signature() const
Definition: message.cpp:162
bool set_destination(const std::string &s)
Set the destination of this message.
Definition: message.cpp:123
virtual ~Message()
Definition: message.cpp:52
bool operator==(const Message &other)
Definition: message.cpp:59
MessageIterator operator>>(T &value) const
Definition: message.h:90
virtual MessageType type() const =0
std::string sender() const
Definition: message.cpp:140
int filedescriptor_at_location(int location) const
Definition: message.cpp:439
std::vector< uint8_t > * body()
Definition: message.cpp:416
uint8_t flags() const
The message flags, as the marshaled byte.
Definition: message.cpp:400
std::string destination() const
Definition: message.cpp:130
static std::shared_ptr< Message > create_from_data(uint8_t *data, uint32_t data_len, std::vector< int > fds=std::vector< int >())
Definition: message.cpp:261
void clear_sig_and_data()
Clears the signature and the data, so you can re-append data.
Definition: message.cpp:389
uint32_t serial() const
Definition: message.cpp:101
void set_flags(uint8_t flags)
Definition: message.cpp:404
DBUS_CXX_PROPAGATE_CONST(std::unique_ptr< priv_data >) m_priv
bool serialize_to_vector(std::vector< uint8_t > *vec, uint32_t serial) const
Serialize this message to the given vector.
Definition: message.cpp:172
bool auto_start()
Returns true if the bus is allowed to start an owner for this message's destination if it is not runn...
Definition: message.cpp:119
MessageAppendIterator operator<<(const T &value)
Definition: message.h:97
bool is_valid() const
Definition: message.cpp:87
void add_filedescriptor(int fd)
Definition: message.cpp:424
Endianess endianess() const
Definition: message.cpp:435
MessageIterator end() const
Definition: message.cpp:154
uint32_t filedescriptors_size() const
Definition: message.cpp:431
const std::vector< int > & filedescriptors() const
Definition: message.cpp:448
Message()
Definition: message.cpp:48
void append_signature(std::string toappend)
Definition: message.cpp:365
MessageAppendIterator append()
Definition: message.cpp:158
MessageIterator begin() const
Definition: message.cpp:150
void set_auto_start(bool auto_start)
Definition: message.cpp:111
void invalidate()
Definition: message.cpp:93
Variant set_header_field(MessageHeaderFields field, Variant value)
Set the given header field.
Definition: message.cpp:408
Represents a DBus signature.
Definition: signature.h:74
A Variant is a type-safe union for DBus operations.
Definition: variant.h:42
Global DBus namespace, where everything happens.
Definition: callmessage.cpp:18
MessageHeaderFields
Definition: enums.h:140
std::shared_ptr< DBus::Connection > operator<<(std::shared_ptr< DBus::Connection > ptr, std::shared_ptr< DBus::Message > msg)
Definition: connection.h:431
MessageType
Definition: enums.h:81
DBus::MessageIterator operator>>(std::shared_ptr< const DBus::Message > ptr, T &value)
Definition: message.h:183
Endianess
Definition: enums.h:114