variantiterator.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_VARIANT_ITERATOR_H
9 #define DBUSCXX_VARIANT_ITERATOR_H
10 
11 #include <string>
12 #include <memory>
13 #include <dbus-cxx/types.h>
14 #include <dbus-cxx/error.h>
16 
17 namespace DBus {
18 
19 class Variant;
20 class Demarshaling;
21 
22 namespace priv {
23 
25 private:
36  const Variant* variant,
37  std::shared_ptr<Demarshaling> demarshal );
38 
39 public:
40 
42 
43  VariantIterator( const Variant* variant );
44 
46 
47  template <typename T>
49  v = static_cast<T>( *this );
50  this->next();
51  return *this;
52  }
53 
54  operator bool();
55  operator uint8_t();
56  operator uint16_t();
57  operator int16_t();
58  operator uint32_t();
59  operator int32_t();
60  operator uint64_t();
61  operator int64_t();
62  operator double();
63  operator std::string();
64  operator DBus::Variant();
65 
66  template <typename T>
67  operator std::vector<T>() {
68  if( !is_array() ) {
69  throw ErrorInvalidTypecast( "VariantIterator: Extracting non array into std::vector" );
70  }
71 
72  std::vector<T> retval;
73  VariantIterator subiter = this->recurse();
74 
75  while( subiter.is_valid() ) {
76  //NOTE: we don't need to do subiter.next() here, because
77  //operator>> does that for us
78  T val;
79  subiter >> val;
80  retval.push_back( val );
81  }
82 
83  return retval;
84  }
85 
86  template <typename Key, typename Data>
87  operator std::map<Key, Data>() {
88  if( !this->is_dict() ) {
89  throw ErrorInvalidTypecast( "VariantIterator: Extracting non dict into std::map" );
90  }
91 
92  std::map<Key, Data> dict;
93  Key val_key;
94  Data val_data;
95 
96  VariantIterator subiter = this->recurse();
97 
98  while( subiter.is_valid() ) {
99  VariantIterator subSubiter = subiter.recurse();
100 
101  while( subSubiter.is_valid() ) {
102  subSubiter >> val_key;
103  subSubiter >> val_data;
104  dict[ val_key ] = val_data;
105  subSubiter.next();
106  }
107 
108  subiter.next();
109  }
110 
111  return dict;
112  }
113 
114  template <typename ...T>
115  operator std::tuple<T...>() {
116  std::tuple<T...> tup;
117 
118  VariantIterator subiter = this->recurse();
119  std::apply( [subiter]( auto&& ...arg ) mutable {
120  ( subiter >> ... >> arg );
121  },
122  tup );
123 
124  return tup;
125  }
126 
127  bool get_bool();
128  uint8_t get_uint8();
129  uint16_t get_uint16();
130  int16_t get_int16();
131  uint32_t get_uint32();
132  int32_t get_int32();
133  uint64_t get_uint64();
134  int64_t get_int64();
135  double get_double();
136  std::string get_string();
139 
140  void invalidate();
141 
143  bool is_valid() const;
144 
146  bool has_next() const;
147 
156  bool next();
157 
159  DataType arg_type() const;
160 
166  DataType element_type() const;
167 
169 
171 
173 
175  bool is_fixed() const;
176 
178  bool is_container() const;
179 
181  bool is_array() const;
182 
184  bool is_dict() const;
185 
186 private:
187  class priv_data;
188 
189  std::shared_ptr<priv_data> m_priv;
190 };
191 
192 } /* namespace priv */
193 
194 } /* namespace DBus */
195 
196 #endif /* DBUS_CXX_VARIANT_ITERATOR_H */
A SignatureIterator allows you to iterate over a given DBus signature, and to extract useful informat...
Definition: signatureiterator.h:33
Represents a DBus signature.
Definition: signature.h:74
A Variant is a type-safe union for DBus operations.
Definition: variant.h:42
Definition: variantiterator.h:24
uint16_t get_uint16()
Definition: variantiterator.cpp:459
VariantIterator recurse()
Definition: variantiterator.cpp:144
bool is_container() const
True if the iterator points to a container.
Definition: variantiterator.cpp:165
VariantIterator & operator++()
Definition: variantiterator.cpp:120
bool is_valid() const
True if this is a valid iterator.
Definition: variantiterator.cpp:74
bool get_bool()
Definition: variantiterator.cpp:435
bool is_dict() const
True if the iterator points to a dictionary.
Definition: variantiterator.cpp:174
uint64_t get_uint64()
Definition: variantiterator.cpp:491
bool has_next() const
True if there are any more fields left to iterate over.
Definition: variantiterator.cpp:91
int64_t get_int64()
Definition: variantiterator.cpp:483
double get_double()
Definition: variantiterator.cpp:499
VariantIterator & operator>>(Variant &v)
Definition: variantiterator.cpp:530
std::string get_string()
Definition: variantiterator.cpp:507
bool is_array() const
True if the iterator points to an array.
Definition: variantiterator.cpp:170
std::shared_ptr< priv_data > m_priv
Definition: variantiterator.h:187
uint32_t get_uint32()
Definition: variantiterator.cpp:475
bool next()
Moves the iterator to the next field and invalidates it if it moves beyond the last field.
Definition: variantiterator.cpp:97
Signature get_signature()
Definition: variantiterator.cpp:526
DataType arg_type() const
Returns the argument type that the iterator points to.
Definition: variantiterator.cpp:132
bool is_fixed() const
True if the element type is a fixed type.
Definition: variantiterator.cpp:160
Variant get_variant()
Definition: variantiterator.cpp:519
uint8_t get_uint8()
Definition: variantiterator.cpp:443
void invalidate()
Definition: variantiterator.cpp:70
VariantIterator()
Definition: variantiterator.cpp:38
DataType element_type() const
Returns the element type of the array that the iterator points to.
Definition: variantiterator.cpp:136
int32_t get_int32()
Definition: variantiterator.cpp:467
int16_t get_int16()
Definition: variantiterator.cpp:451
VariantIterator & operator>>(T &v)
Definition: variantiterator.h:48
Global DBus namespace, where everything happens.
Definition: callmessage.cpp:18
DataType
Definition: enums.h:52