variant.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_H
9 #define DBUSCXX_VARIANT_H
10 
11 #include <dbus-cxx/enums.h>
12 #include <dbus-cxx/path.h>
13 #include <dbus-cxx/signature.h>
17 #include <dbus-cxx/marshaling.h>
18 #include <dbus-cxx/types.h>
19 #include <dbus-cxx/error.h>
20 #include <string>
21 #include <any>
22 #include <stdint.h>
23 #include <ostream>
24 #include <vector>
25 #include <map>
26 #include <tuple>
27 
28 namespace DBus {
29 
30 class MessageIterator;
31 class FileDescriptor;
32 
42 class Variant {
43 public:
44  Variant();
45  Variant( uint8_t byte );
46  Variant( bool b );
47  Variant( int16_t i );
48  Variant( uint16_t i );
49  Variant( int32_t i );
50  Variant( uint32_t i );
51  Variant( int64_t i );
52  Variant( uint64_t i );
53  Variant( double i );
54  Variant( const char* cstr );
55  Variant( std::string str );
56  explicit Variant( Signature sig );
57  explicit Variant( Path path );
58 
59  template<typename T>
60  Variant( const std::vector<T>& vec ) :
62  m_signature( DBus::signature( vec ) ),
63  m_dataAlignment( 4 ) {
64  priv::VariantAppendIterator it( this );
65 
66  it << vec;
67  }
68 
69  template<typename Key, typename Value>
70  Variant( const std::map<Key, Value>& map ) :
72  m_signature( DBus::signature( map ) ),
73  m_dataAlignment( 4 ) {
74  priv::VariantAppendIterator it( this );
75 
76  it << map;
77  }
78 
79  template<typename ...T>
80  Variant( const std::tuple<T...>& tup ) :
82  m_signature( DBus::signature( tup ) ),
83  m_dataAlignment( 8 ) {
84  priv::VariantAppendIterator it( this );
85  it << tup;
86  }
87 
88  Variant( const Variant& other );
89  Variant( Variant&& other );
90  ~Variant();
91 
92  Signature signature() const;
93 
94  DataType type() const;
95 
96  const std::vector<uint8_t>* marshaled() const;
97 
98  int data_alignment() const;
99 
100  bool operator==( const Variant& other ) const;
101 
102  Variant& operator=( const Variant& other );
103 
104  template <typename T>
105  std::vector<T> to_vector() {
106  if( m_currentType != DataType::ARRAY ) {
107  throw ErrorBadVariantCast();
108  }
109  priv::VariantIterator vi( this );
110 
111  std::vector<T> retval = vi;
112 
113  return retval;
114  }
115 
116  template <typename Key, typename Value>
117  std::map<Key, Value> to_map() {
118  if( m_currentType != DataType::ARRAY ) {
119  throw ErrorBadVariantCast();
120  }
121  priv::VariantIterator vi( this );
122 
123  std::map<Key, Value> retval = vi;
124 
125  return retval;
126  }
127 
128  template <typename ...T>
129  std::tuple<T...> to_tuple() {
131  throw ErrorBadVariantCast();
132  }
133  std::tuple<T...> tup;
134  priv::VariantIterator vi( this );
135 
136  tup = vi;
137 
138  return tup;
139  }
140 
141  bool to_bool() const;
142  uint8_t to_uint8() const;
143  uint16_t to_uint16() const;
144  int16_t to_int16() const;
145  uint32_t to_uint32() const;
146  int32_t to_int32() const;
147  uint64_t to_uint64() const;
148  int64_t to_int64() const;
149  double to_double() const;
150  std::string to_string() const;
151  DBus::Path to_path() const;
153 
154  operator bool();
155  operator uint8_t();
156  operator uint16_t();
157  operator int16_t();
158  operator uint32_t();
159  operator int32_t();
160  operator uint64_t();
161  operator int64_t();
162  operator double();
163  operator std::string();
164  operator DBus::Path();
165  operator DBus::Signature();
166 
167  template <typename T>
168  operator std::vector<T>() {
169  return to_vector<T>();
170  }
171 
172  template <typename Key, typename Data>
173  operator std::map<Key, Data>() {
174  return to_map<Key, Data>();
175  }
176 
177  template <typename... T>
178  operator std::tuple<T...>() {
179  return to_tuple<T...>();
180  }
181 
185  [[deprecated]]
187 
188  static Variant createFromDemarshal( Signature sig, std::shared_ptr<Demarshaling> demarshal, const std::vector<int>& filedescriptors, uint32_t depth );
189 
190 private:
191  void recurseArray( SignatureIterator iter, std::shared_ptr<Demarshaling> demarshal, Marshaling* marshal, const std::vector<int>& filedescriptors, uint32_t depth );
192  void recurseDictEntry( SignatureIterator iter, std::shared_ptr<Demarshaling> demarshal, Marshaling* marshal, uint32_t ending_offset, const std::vector<int>& filedescriptors, uint32_t depth );
193  void recurseStruct( SignatureIterator sigit, std::shared_ptr<Demarshaling> demarshal, Marshaling* marshal, const std::vector<int>& filedescriptors, uint32_t depth );
194  void remarshal(DataType dt, SignatureIterator sigit, std::shared_ptr<Demarshaling> demarshal, Marshaling* marshal, const std::vector<int>& filedescriptors, uint32_t depth);
195 
199  [[deprecated]]
200  void recurseArray( MessageIterator iter, Marshaling* marshal );
204  [[deprecated]]
205  void recurseDictEntry( MessageIterator iter, Marshaling* marshal );
209  [[deprecated]]
210  void recurseStruct( MessageIterator iter, Marshaling* marshal );
211 
212 private:
215  std::vector<uint8_t> m_marshaled;
217 
218  friend std::ostream& operator<<( std::ostream& os, const Variant& var );
220  friend class priv::VariantIterator;
221 };
222 
223 } /* namespace DBus */
224 
225 #endif /* DBUSCXX_VARIANT_H */
Definition: error.h:426
Implements the marshaling algorithms on a given vector of data.
Definition: marshaling.h:27
Extraction iterator allowing values to be retrieved from a message.
Definition: messageiterator.h:56
Represents a DBus Path.
Definition: path.h:21
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
std::vector< T > to_vector()
Definition: variant.h:105
int data_alignment() const
Definition: variant.cpp:452
int16_t to_int16() const
Definition: variant.cpp:503
friend std::ostream & operator<<(std::ostream &os, const Variant &var)
Definition: variant.cpp:634
uint8_t to_uint8() const
Definition: variant.cpp:485
Variant(const std::map< Key, Value > &map)
Definition: variant.h:70
std::tuple< T... > to_tuple()
Definition: variant.h:129
bool to_bool() const
Definition: variant.cpp:476
DBus::Signature to_signature() const
Definition: variant.cpp:575
DataType m_currentType
Definition: variant.h:213
static Variant createFromDemarshal(Signature sig, std::shared_ptr< Demarshaling > demarshal, const std::vector< int > &filedescriptors, uint32_t depth)
Definition: variant.cpp:155
static Variant createFromMessage(MessageIterator iter)
Obsolete, do not call.
Definition: variant.cpp:309
int32_t to_int32() const
Definition: variant.cpp:521
int m_dataAlignment
Definition: variant.h:216
double to_double() const
Definition: variant.cpp:548
std::vector< uint8_t > m_marshaled
Definition: variant.h:215
void recurseDictEntry(SignatureIterator iter, std::shared_ptr< Demarshaling > demarshal, Marshaling *marshal, uint32_t ending_offset, const std::vector< int > &filedescriptors, uint32_t depth)
Definition: variant.cpp:281
int64_t to_int64() const
Definition: variant.cpp:539
uint16_t to_uint16() const
Definition: variant.cpp:494
std::string to_string() const
Definition: variant.cpp:557
uint32_t to_uint32() const
Definition: variant.cpp:512
void recurseArray(SignatureIterator iter, std::shared_ptr< Demarshaling > demarshal, Marshaling *marshal, const std::vector< int > &filedescriptors, uint32_t depth)
Definition: variant.cpp:250
const std::vector< uint8_t > * marshaled() const
Definition: variant.cpp:448
void recurseStruct(SignatureIterator sigit, std::shared_ptr< Demarshaling > demarshal, Marshaling *marshal, const std::vector< int > &filedescriptors, uint32_t depth)
Definition: variant.cpp:319
Signature signature() const
Definition: variant.cpp:147
Variant(const std::tuple< T... > &tup)
Definition: variant.h:80
void remarshal(DataType dt, SignatureIterator sigit, std::shared_ptr< Demarshaling > demarshal, Marshaling *marshal, const std::vector< int > &filedescriptors, uint32_t depth)
Definition: variant.cpp:338
Variant(const std::vector< T > &vec)
Definition: variant.h:60
Variant & operator=(const Variant &other)
Definition: variant.cpp:467
DBus::Path to_path() const
Definition: variant.cpp:566
std::map< Key, Value > to_map()
Definition: variant.h:117
DataType type() const
Definition: variant.cpp:151
bool operator==(const Variant &other) const
Definition: variant.cpp:456
uint64_t to_uint64() const
Definition: variant.cpp:530
Signature m_signature
Definition: variant.h:214
Variant()
Definition: variant.cpp:28
~Variant()
Definition: variant.cpp:145
Used to append a single, complete value to a Variant.
Definition: variantappenditerator.h:28
Definition: variantiterator.h:24
Global DBus namespace, where everything happens.
Definition: callmessage.cpp:18
DataType
Definition: enums.h:52