messageappenditerator.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) 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/enums.h>
10 #include <dbus-cxx/signature.h>
11 #include <dbus-cxx/marshaling.h>
13 #include <stddef.h>
14 #include <map>
15 #include <memory>
16 #include <string>
17 #include <tuple>
18 #include <vector>
19 #include "error.h"
20 #include "path.h"
21 #include "variant.h"
22 
23 #ifndef DBUSCXX_MESSAGEAPPENDITERATOR_H
24 #define DBUSCXX_MESSAGEAPPENDITERATOR_H
25 
26 namespace DBus {
27 
28 class FileDescriptor;
29 class Message;
30 
39 public:
40 
42 
44 
45  MessageAppendIterator( std::shared_ptr<Message> message, ContainerType container = ContainerType::None );
46 
48 
53  const Message* message() const;
54 
56  void invalidate();
57 
59  bool is_valid() const;
60 
62  operator bool() const;
63 
64  MessageAppendIterator& operator<<( const bool& v );
65  MessageAppendIterator& operator<<( const uint8_t& v );
66  MessageAppendIterator& operator<<( const int16_t& v );
67  MessageAppendIterator& operator<<( const uint16_t& v );
68  MessageAppendIterator& operator<<( const int32_t& v );
69  MessageAppendIterator& operator<<( const uint32_t& v );
70  MessageAppendIterator& operator<<( const int64_t& v );
71  MessageAppendIterator& operator<<( const uint64_t& v );
72  MessageAppendIterator& operator<<( const double& v );
73  MessageAppendIterator& operator<<( const char* v );
74  MessageAppendIterator& operator<<( const std::string& v );
77  MessageAppendIterator& operator<<( const std::shared_ptr<FileDescriptor> v );
79 
80  template<typename ... T>
83  std::apply( [this]( auto&& ...arg ) mutable {
84  ( *this << ... << arg );
85  },
86  v.m_data );
87 
88  return *this;
89  }
90 
91  template <typename T>
92  MessageAppendIterator& operator<<( const std::vector<T>& v ) {
93  bool success;
94  T type;
95 
96  success = this->open_container( ContainerType::ARRAY, DBus::signature( type ) );
97 
98  if( !success ) {
99  throw ErrorNoMemory();
100  }
101 
102  for( size_t i = 0; i < v.size(); i++ ) {
103  *sub_iterator() << v[i];
104  }
105 
106  success = this->close_container();
107 
108  if( !success ) {
109  throw ErrorNoMemory();
110  }
111 
112  return *this;
113  }
114 
115  template <typename Key, typename Data>
116  MessageAppendIterator& operator<<( const std::map<Key, Data>& dictionary ) {
117  std::string sig = signature_dict_data( dictionary );
118  typename std::map<Key, Data>::const_iterator it;
119  this->open_container( ContainerType::ARRAY, sig );
120 
121  for( it = dictionary.begin(); it != dictionary.end(); it++ ) {
123  *( sub_iterator()->sub_iterator() ) << it->first;
124  *( sub_iterator()->sub_iterator() ) << it->second;
126  }
127 
128  this->close_container();
129 
130  return *this;
131  }
132 
133  template <typename... T>
134  MessageAppendIterator& operator<<( const std::tuple<T...>& tup ) {
135  bool success;
136  std::string signature = DBus::priv::dbus_signature<T...>().dbus_sig();
137  success = this->open_container( ContainerType::STRUCT, signature.c_str() );
138  MessageAppendIterator* subiter = sub_iterator();
139  std::apply( [subiter]( auto&& ...arg ) mutable {
140  ( *subiter << ... << arg );
141  },
142  tup );
143  this->close_container();
144 
145  return *this;
146  }
147 
148 private:
149  bool open_container( ContainerType t, const std::string& contained_signature );
150 
151  bool close_container( );
152 
154 
155 private:
156  class priv_data;
157 
158  std::shared_ptr<priv_data> m_priv;
159 };
160 
161 }
162 
163 #endif
164 
Insertion iterator allow values to be appended to a message.
Definition: messageappenditerator.h:38
MessageAppendIterator(ContainerType container=ContainerType::None)
Definition: messageappenditerator.cpp:44
MessageAppendIterator & operator<<(const std::map< Key, Data > &dictionary)
Definition: messageappenditerator.h:116
MessageAppendIterator & operator<<(const MultipleReturn< T... > &v)
Definition: messageappenditerator.h:81
void invalidate()
Invalidates the iterator.
Definition: messageappenditerator.cpp:78
std::shared_ptr< priv_data > m_priv
Definition: messageappenditerator.h:156
~MessageAppendIterator()
Definition: messageappenditerator.cpp:74
MessageAppendIterator & operator<<(const bool &v)
Definition: messageappenditerator.cpp:94
MessageAppendIterator & operator<<(const std::tuple< T... > &tup)
Definition: messageappenditerator.h:134
const Message * message() const
Returns a pointer to the message associated with this iterator or NULL if no message is associated.
MessageAppendIterator & operator<<(const std::vector< T > &v)
Definition: messageappenditerator.h:92
bool is_valid() const
True if this is a valid iterator.
Definition: messageappenditerator.cpp:83
bool close_container()
Definition: messageappenditerator.cpp:394
MessageAppendIterator * sub_iterator()
Definition: messageappenditerator.cpp:431
bool open_container(ContainerType t, const std::string &contained_signature)
Definition: messageappenditerator.cpp:344
This class represents a basic DBus message and also serves as a base class for the specialized messag...
Definition: message.h:43
Definition: multiplereturn.h:29
std::tuple< Ts... > m_data
Definition: multiplereturn.h:31
Represents a DBus Path.
Definition: path.h:21
Represents a DBus signature.
Definition: signature.h:74
A Variant is a type-safe union for DBus operations.
Definition: variant.h:42
Definition: signature.h:200
Global DBus namespace, where everything happens.
Definition: callmessage.cpp:18
ContainerType
Definition: enums.h:73
std::string signature_multiple_return_data(const DBus::MultipleReturn< T_arg... > &)
Definition: signature.h:230
std::string signature_dict_data(const std::map< Key, Data > &)
Definition: signature.h:183
DataType type(const uint8_t &)
Definition: types.h:137
std::string signature(const std::tuple< T... > &)