signature.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/path.h>
12 #include <any>
13 #include <map>
14 #include <memory>
15 #include <ostream>
16 #include <string>
17 #include <vector>
18 #include <stack>
19 #include "enums.h"
20 
21 #ifndef DBUSCXX_SIGNATURE_H
22 #define DBUSCXX_SIGNATURE_H
23 
24 #define DBUSCXX_TYPE_INVALID_AS_STRING "\0"
25 #define DBUSCXX_TYPE_BYTE_AS_STRING "y"
26 #define DBUSCXX_TYPE_BOOLEAN_AS_STRING "b"
27 #define DBUSCXX_TYPE_INT16_AS_STRING "n"
28 #define DBUSCXX_TYPE_UINT16_AS_STRING "q"
29 #define DBUSCXX_TYPE_INT32_AS_STRING "i"
30 #define DBUSCXX_TYPE_UINT32_AS_STRING "u"
31 #define DBUSCXX_TYPE_INT64_AS_STRING "x"
32 #define DBUSCXX_TYPE_UINT64_AS_STRING "t"
33 #define DBUSCXX_TYPE_DOUBLE_AS_STRING "d"
34 #define DBUSCXX_TYPE_STRING_AS_STRING "s"
35 #define DBUSCXX_TYPE_SIGNATURE_AS_STRING "g"
36 #define DBUSCXX_TYPE_OBJECT_PATH_AS_STRING "o"
37 #define DBUSCXX_TYPE_VARIANT_AS_STRING "v"
38 #define DBUSCXX_TYPE_UNIX_FD_AS_STRING "h"
39 #define DBUSCXX_TYPE_ARRAY_AS_STRING "a"
40 #define DBUSCXX_DICT_ENTRY_BEGIN_CHAR_AS_STRING "{"
41 #define DBUSCXX_DICT_ENTRY_END_CHAR_AS_STRING "}"
42 #define DBUSCXX_STRUCT_BEGIN_CHAR_AS_STRING "("
43 #define DBUSCXX_STRUCT_END_CHAR_AS_STRING ")"
44 
45 namespace DBus {
46 
47 namespace priv {
52 public:
54  m_dataType( d ),
55  m_next( nullptr ),
56  m_sub( nullptr ) {}
57 
59  std::shared_ptr<priv::SignatureNode> m_next;
60  std::shared_ptr<priv::SignatureNode> m_sub;
61 };
62 }
63 
64 class FileDescriptor;
65 class Variant;
66 template<typename... T> class MultipleReturn;
67 
74 class Signature {
75 public:
76 
78 
80 
81  typedef std::string::size_type size_type;
82 
84 
85  Signature();
86 
87  Signature( const std::string& s, size_type pos = 0, size_type n = npos );
88 
89  Signature( const char* );
90 
91  Signature( const char* s, size_type n );
92 
93  Signature( size_type n, char c );
94 
95  // template<class InputIterator>
96  // Signature( InputIterator first, InputIterator last ): m_signature( first, last ) { }
97 
98  ~Signature();
99 
100  operator const std::string& () const;
101 
102  const std::string& str() const;
103 
104  Signature& operator=( const std::string& s );
105 
106  Signature& operator=( const char* s );
107 
108  bool operator==( const std::string& s ) const { return str() == s; }
109 
110  iterator begin();
111 
112  const_iterator begin() const;
113 
114  iterator end();
115 
116  const_iterator end() const;
117 
119  bool is_valid() const;
120 
122  bool is_singleton() const;
123 
129  void print_tree( std::ostream* stream ) const;
130 
131 private:
132  void initialize();
133 
134  std::shared_ptr<priv::SignatureNode> create_signature_tree( std::string::const_iterator* it,
135  std::stack<ContainerType>* container_stack,
136  bool* ok );
137 
138  void print_node( std::ostream* stream, priv::SignatureNode* node, int spaces ) const;
139 
140 private:
141  class priv_data;
142 
143  std::shared_ptr<priv_data> m_priv;
144 };
145 
146 template <typename... T>
147 inline std::string signature( const std::tuple<T...>& );
148 
149 inline std::string signature( uint8_t ) { return DBUSCXX_TYPE_BYTE_AS_STRING; }
150 inline std::string signature( bool ) { return DBUSCXX_TYPE_BOOLEAN_AS_STRING; }
151 inline std::string signature( int16_t ) { return DBUSCXX_TYPE_INT16_AS_STRING; }
152 inline std::string signature( uint16_t ) { return DBUSCXX_TYPE_UINT16_AS_STRING; }
153 inline std::string signature( int32_t ) { return DBUSCXX_TYPE_INT32_AS_STRING; }
154 inline std::string signature( uint32_t ) { return DBUSCXX_TYPE_UINT32_AS_STRING; }
155 inline std::string signature( int64_t ) { return DBUSCXX_TYPE_INT64_AS_STRING; }
156 inline std::string signature( uint64_t ) { return DBUSCXX_TYPE_UINT64_AS_STRING; }
157 inline std::string signature( double ) { return DBUSCXX_TYPE_DOUBLE_AS_STRING; }
158 inline std::string signature( std::string ) { return DBUSCXX_TYPE_STRING_AS_STRING; }
159 inline std::string signature( Signature ) { return DBUSCXX_TYPE_SIGNATURE_AS_STRING; }
160 inline std::string signature( Path ) { return DBUSCXX_TYPE_OBJECT_PATH_AS_STRING; }
161 inline std::string signature( const DBus::Variant& ) { return DBUSCXX_TYPE_VARIANT_AS_STRING; }
162 inline std::string signature( const std::shared_ptr<FileDescriptor> ) { return DBUSCXX_TYPE_UNIX_FD_AS_STRING; }
163 template<typename... T>
165 
166 
167 template <typename T> inline std::string signature( const std::vector<T>& ) { T t; return DBUSCXX_TYPE_ARRAY_AS_STRING + signature( t ); }
168 
169 template <typename Key, typename Data> inline std::string signature( const std::map<Key, Data>& ) {
170  Key k; Data d;
171  std::string sig;
174  signature( k ) + signature( d ) +
176  return sig;
177 }
178 
179 //Note: we need to have two different signature() methods for dictionaries; this is because
180 //when introspecting, we need to use the normal signature() so that it comes up properly.
181 //However, when we are sending out data, that signature would give us an extra array signature,
182 //which is not good. Hence, this method is only used when we need to send out a dict
183 template <typename Key, typename Data> inline std::string signature_dict_data( const std::map<Key, Data>& ) {
184  Key k; Data d;
185  std::string sig;
187  signature( k ) + signature( d ) +
189  return sig;
190 }
191 
192 template<typename... T_arg>
194 
195 namespace priv {
196 /*
197  * dbus_signature class - signature of a given type
198  */
199 template<typename... argn>
201 
202 template<> class dbus_signature<> {
203 public:
204  std::string dbus_sig() const {
205  return "";
206  }
207 };
208 
209 template<typename arg1, typename... argn>
210 class dbus_signature<arg1, argn...> : public dbus_signature<argn...> {
211 public:
212  std::string dbus_sig() const {
213  arg1 arg;
215  }
216 };
217 
218 } /* namespace priv */
219 
220 template<typename... T_arg>
221 inline std::string signature( const std::tuple<T_arg...>& ) {
222  priv::dbus_signature<T_arg...> sig;
223 
225  sig.dbus_sig() +
227 }
228 
229 template<typename... T_arg>
231  priv::dbus_signature<T_arg...> sig;
232 
233  return sig.dbus_sig();
234 }
235 
236 inline
237 std::ostream& operator<<( std::ostream& sout, const DBus::Signature& sig ) {
238  sout << "DBus::Signature[" << sig.str() << "]";
239  return sout;
240 }
241 
242 }
243 
244 #endif
A FileDescriptor holds a UNIX file descriptor that can be passed between processes.
Definition: filedescriptor.h:21
Definition: multiplereturn.h:29
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
SignatureIterator iterator
Definition: signature.h:77
void print_tree(std::ostream *stream) const
Print the signature tree to the given ostream.
Definition: signature.cpp:249
const std::string & str() const
Definition: signature.cpp:66
iterator end()
Definition: signature.cpp:92
std::shared_ptr< priv_data > m_priv
Definition: signature.h:141
bool is_singleton() const
True if the signature is a valid DBus signature and contains exactly one complete type.
Definition: signature.cpp:104
void print_node(std::ostream *stream, priv::SignatureNode *node, int spaces) const
Definition: signature.cpp:264
~Signature()
Definition: signature.cpp:59
void initialize()
Definition: signature.cpp:277
const SignatureIterator const_iterator
Definition: signature.h:79
std::shared_ptr< priv::SignatureNode > create_signature_tree(std::string::const_iterator *it, std::stack< ContainerType > *container_stack, bool *ok)
Definition: signature.cpp:111
bool is_valid() const
True if the signature is a valid DBus signature.
Definition: signature.cpp:100
Signature & operator=(const std::string &s)
Definition: signature.cpp:70
bool operator==(const std::string &s) const
Definition: signature.h:108
Signature()
Definition: signature.cpp:31
std::string::size_type size_type
Definition: signature.h:81
iterator begin()
Definition: signature.cpp:80
static const size_type npos
Definition: signature.h:83
A Variant is a type-safe union for DBus operations.
Definition: variant.h:42
Represents a single entry in our graph of the signature.
Definition: signature.h:51
DataType m_dataType
Definition: signature.h:58
std::shared_ptr< priv::SignatureNode > m_next
Definition: signature.h:59
std::shared_ptr< priv::SignatureNode > m_sub
Definition: signature.h:60
SignatureNode(DataType d)
Definition: signature.h:53
std::string dbus_sig() const
Definition: signature.h:212
std::string dbus_sig() const
Definition: signature.h:204
Definition: signature.h:200
Global DBus namespace, where everything happens.
Definition: callmessage.cpp:18
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
std::shared_ptr< DBus::Connection > operator<<(std::shared_ptr< DBus::Connection > ptr, std::shared_ptr< DBus::Message > msg)
Definition: connection.h:431
const Signature::size_type npos
Definition: signature.cpp:18
DataType
Definition: enums.h:52
std::string signature(const std::tuple< T... > &)
#define DBUSCXX_TYPE_DOUBLE_AS_STRING
Definition: signature.h:33
#define DBUSCXX_STRUCT_END_CHAR_AS_STRING
Definition: signature.h:43
#define DBUSCXX_STRUCT_BEGIN_CHAR_AS_STRING
Definition: signature.h:42
#define DBUSCXX_TYPE_INVALID_AS_STRING
Definition: signature.h:24
#define DBUSCXX_TYPE_INT16_AS_STRING
Definition: signature.h:27
#define DBUSCXX_TYPE_BYTE_AS_STRING
Definition: signature.h:25
#define DBUSCXX_TYPE_INT32_AS_STRING
Definition: signature.h:29
#define DBUSCXX_TYPE_ARRAY_AS_STRING
Definition: signature.h:39
#define DBUSCXX_TYPE_SIGNATURE_AS_STRING
Definition: signature.h:35
#define DBUSCXX_TYPE_VARIANT_AS_STRING
Definition: signature.h:37
#define DBUSCXX_TYPE_BOOLEAN_AS_STRING
Definition: signature.h:26
#define DBUSCXX_TYPE_STRING_AS_STRING
Definition: signature.h:34
#define DBUSCXX_DICT_ENTRY_END_CHAR_AS_STRING
Definition: signature.h:41
#define DBUSCXX_TYPE_UINT64_AS_STRING
Definition: signature.h:32
#define DBUSCXX_TYPE_UINT16_AS_STRING
Definition: signature.h:28
#define DBUSCXX_TYPE_OBJECT_PATH_AS_STRING
Definition: signature.h:36
#define DBUSCXX_DICT_ENTRY_BEGIN_CHAR_AS_STRING
Definition: signature.h:40
#define DBUSCXX_TYPE_UNIX_FD_AS_STRING
Definition: signature.h:38
#define DBUSCXX_TYPE_INT64_AS_STRING
Definition: signature.h:31
#define DBUSCXX_TYPE_UINT32_AS_STRING
Definition: signature.h:30