variantappenditerator.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) 2020 by Robert Middleton *
4  * robert.middleton@rm5248.com *
5  * *
6  * This file is part of the dbus-cxx library. *
7  ***************************************************************************/
8 #ifndef DBUSCXX_VARIANTAPPENDITERATOR_H
9 #define DBUSCXX_VARIANTAPPENDITERATOR_H
10 
11 #include <vector>
12 #include <string>
13 #include <dbus-cxx/signature.h>
14 #include <dbus-cxx/path.h>
16 
17 namespace DBus {
18 
19 class Variant;
20 
21 namespace priv {
22 
29 private:
31 public:
32  VariantAppendIterator( Variant* variant );
34 
35  VariantAppendIterator& operator<<( const bool& v );
36  VariantAppendIterator& operator<<( const uint8_t& v );
37  VariantAppendIterator& operator<<( const int16_t& v );
38  VariantAppendIterator& operator<<( const uint16_t& v );
39  VariantAppendIterator& operator<<( const int32_t& v );
40  VariantAppendIterator& operator<<( const uint32_t& v );
41  VariantAppendIterator& operator<<( const int64_t& v );
42  VariantAppendIterator& operator<<( const uint64_t& v );
43  VariantAppendIterator& operator<<( const double& v );
44  VariantAppendIterator& operator<<( const char* v );
45  VariantAppendIterator& operator<<( const std::string& v );
49 
50  template <typename T>
51  VariantAppendIterator& operator<<( const std::vector<T>& v ) {
54 
55  for( T t : v ) {
56  ( *sub ) << t;
57  }
58 
60 
61  return *this;
62  }
63 
64  template <typename Key, typename Data>
65  VariantAppendIterator& operator<<( const std::map<Key, Data>& dictionary ) {
66  std::string sig = signature_dict_data( dictionary );
67  typename std::map<Key, Data>::const_iterator it;
69 
70  for( it = dictionary.begin(); it != dictionary.end(); it++ ) {
72  *( sub_iterator()->sub_iterator() ) << it->first;
73  *( sub_iterator()->sub_iterator() ) << it->second;
75  }
76 
77  this->close_container();
78 
79  return *this;
80  }
81 
82  template <typename... T>
83  VariantAppendIterator& operator<<( const std::tuple<T...>& tup ) {
84  bool success;
85  std::string signature = DBus::signature( tup );
86  success = this->open_container( ContainerType::STRUCT, signature.c_str() );
88  std::apply( [subiter]( auto&& ...arg ) mutable {
89  ( *subiter << ... << arg );
90  },
91  tup );
92  this->close_container();
93 
94  return *this;
95  }
96 
97 private:
98  bool open_container( ContainerType t, const std::string& contained_signature );
99 
100  bool close_container( );
101 
103 
104 private:
105  class priv_data;
106 
107  std::shared_ptr<priv_data> m_priv;
108 };
109 
110 } /* namespace priv */
111 
112 } /* namespace DBus */
113 
114 #endif /* DBUSCXX_VARIANTAPPENDITERATOR_H */
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
Used to append a single, complete value to a Variant.
Definition: variantappenditerator.h:28
bool open_container(ContainerType t, const std::string &contained_signature)
Definition: variantappenditerator.cpp:171
VariantAppendIterator(Variant *variant, ContainerType t)
Definition: variantappenditerator.cpp:40
bool close_container()
Definition: variantappenditerator.cpp:190
~VariantAppendIterator()
Definition: variantappenditerator.cpp:46
std::shared_ptr< priv_data > m_priv
Definition: variantappenditerator.h:105
VariantAppendIterator & operator<<(const std::map< Key, Data > &dictionary)
Definition: variantappenditerator.h:65
VariantAppendIterator & operator<<(const std::tuple< T... > &tup)
Definition: variantappenditerator.h:83
VariantAppendIterator & operator<<(const std::vector< T > &v)
Definition: variantappenditerator.h:51
VariantAppendIterator & operator<<(const bool &v)
Definition: variantappenditerator.cpp:50
VariantAppendIterator * sub_iterator()
Definition: variantappenditerator.cpp:226
Global DBus namespace, where everything happens.
Definition: callmessage.cpp:18
ContainerType
Definition: enums.h:73
std::string signature_dict_data(const std::map< Key, Data > &)
Definition: signature.h:183
std::string signature(const std::tuple< T... > &)