dbus-cxx logo
signature.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2009,2010 by Rick L. Vinyard, Jr. *
3  * rvinyard@cs.nmsu.edu *
4  * *
5  * This file is part of the dbus-cxx library. *
6  * *
7  * The dbus-cxx library is free software; you can redistribute it and/or *
8  * modify it under the terms of the GNU General Public License *
9  * version 3 as published by the Free Software Foundation. *
10  * *
11  * The dbus-cxx library is distributed in the hope that it will be *
12  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty *
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this software. If not see <http://www.gnu.org/licenses/>. *
18  ***************************************************************************/
19 #include <cstdint>
20 #include <ostream>
21 #include <string>
22 #include <map>
24 #include <dbus-cxx/path.h>
25 #include <dbus-cxx/variant.h>
27 
28 #ifndef DBUSCXX_SIGNATURE_H
29 #define DBUSCXX_SIGNATURE_H
30 
31 namespace DBus
32 {
33 
37  class Signature
38  {
39  public:
40 
42 
44 
45  typedef std::string::size_type size_type;
46 
47  static const size_type npos = std::string::npos;
48 
49  Signature();
50 
51  Signature( const std::string& s, size_type pos = 0, size_type n = npos );
52 
53  Signature( const char* );
54 
55  Signature( const char* s, size_type n );
56 
57  Signature( size_type n, char c );
58 
59  template<class InputIterator>
60  Signature( InputIterator first, InputIterator last ): m_signature( first, last ) { }
61 
62  ~Signature();
63 
64  operator const std::string&() const;
65 
66  const std::string& str() const;
67 
68  Signature& operator=(const std::string& s);
69 
70  Signature& operator=(const char* s);
71 
72  bool operator==(const std::string& s) const { return m_signature == s; }
73 
74  iterator begin();
75 
76  const_iterator begin() const;
77 
78  iterator end();
79 
80  const_iterator end() const;
81 
83  bool is_valid() const;
84 
86  bool is_singleton() const;
87 
88  protected:
89 
90  std::string m_signature;
91 
92  };
93 
94  inline std::string signature( uint8_t& ) { return DBUS_TYPE_BYTE_AS_STRING; }
95  inline std::string signature( bool& ) { return DBUS_TYPE_BOOLEAN_AS_STRING; }
96  inline std::string signature( int16_t& ) { return DBUS_TYPE_INT16_AS_STRING; }
97  inline std::string signature( uint16_t& ) { return DBUS_TYPE_UINT16_AS_STRING; }
98  inline std::string signature( int32_t& ) { return DBUS_TYPE_INT32_AS_STRING; }
99  inline std::string signature( uint32_t& ) { return DBUS_TYPE_UINT32_AS_STRING; }
100  inline std::string signature( int64_t& ) { return DBUS_TYPE_INT64_AS_STRING; }
101  inline std::string signature( uint64_t& ) { return DBUS_TYPE_UINT64_AS_STRING; }
102  inline std::string signature( double& ) { return DBUS_TYPE_DOUBLE_AS_STRING; }
103  inline std::string signature( std::string& ) { return DBUS_TYPE_STRING_AS_STRING; }
104  inline std::string signature( Signature& ) { return DBUS_TYPE_SIGNATURE_AS_STRING; }
105  inline std::string signature( Path& ) { return DBUS_TYPE_OBJECT_PATH_AS_STRING; }
106 template <class T>
107  inline std::string signature( const Variant<T>& ) { return DBUS_TYPE_VARIANT_AS_STRING; }
108  inline std::string signature( const FileDescriptor& ) { return DBUS_TYPE_UNIX_FD_AS_STRING; }
109 
110  inline std::string signature( char& ) { return DBUS_TYPE_BYTE_AS_STRING; }
111  inline std::string signature( int8_t& ) { return DBUS_TYPE_BYTE_AS_STRING; }
112 
113 #if DBUS_CXX_SIZEOF_LONG_INT == 4
114  inline std::string signature( long int& ) { return DBUS_TYPE_INT32_AS_STRING; }
115  inline std::string signature( long unsigned int& ) { return DBUS_TYPE_UINT32_AS_STRING; }
116 #endif
117 
118  inline std::string signature( float& ) { return DBUS_TYPE_DOUBLE_AS_STRING; }
119 
120  template <typename T> inline std::string signature( const std::vector<T>& ) { T t; return DBUS_TYPE_ARRAY_AS_STRING + signature( t ); }
121 
122  template <typename Key,typename Data> inline std::string signature( const std::map<Key,Data>& )
123  {
124  Key k; Data d;
125  std::string sig;
126  sig = DBUS_TYPE_ARRAY_AS_STRING;
127  sig += DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING +
128  signature(k) + signature(d) +
129  DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
130  return sig;
131  }
132 
133  //Note: we need to have two different signature() methods for dictionaries; this is because
134  //when introspecting, we need to use the normal signature() so that it comes up properly.
135  //However, when we are sending out data, that signature would give us an extra array signature,
136  //which is not good. Hence, this method is only used when we need to send out a dict
137  template <typename Key,typename Data> inline std::string signature_dict_data( const std::map<Key,Data>& )
138  {
139  Key k; Data d;
140  std::string sig;
141  sig = DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING +
142  signature(k) + signature(d) +
143  DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
144  return sig;
145  }
146 
147 
148 // template <typename T1>
149 // inline std::string signature( const Struct<T1>& )
150 // {
151 // T1 t1;
152 // return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
153 // }
154 //
155 // template <typename T1, typename T2>
156 // inline std::string signature( const Struct<T1,T2>& )
157 // {
158 // T1 t1; T2 t2;
159 // return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + signature( t2 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
160 // }
161 //
162 // template <typename T1, typename T2, typename T3>
163 // inline std::string signature( const Struct<T1,T2,T3>& )
164 // {
165 // T1 t1; T2 t2; T3 t3;
166 // return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + signature( t2 ) + signature( t3 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
167 // }
168 //
169 // template <typename T1, typename T2, typename T3, typename T4>
170 // inline std::string signature( const Struct<T1,T2,T3,T4>& )
171 // {
172 // T1 t1; T2 t2; T3 t3; T4 t4;
173 // return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + signature( t2 ) + signature( t3 ) + signature( t4 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
174 // }
175 //
176 // template <typename T1, typename T2, typename T3, typename T4, typename T5>
177 // inline std::string signature( const Struct<T1,T2,T3,T4,T5>& )
178 // {
179 // T1 t1; T2 t2; T3 t3; T4 t4; T5 t5;
180 // return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + signature( t2 ) + signature( t3 ) + signature( t4 ) + signature( t5 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
181 // }
182 //
183 // template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
184 // inline std::string signature( const Struct<T1,T2,T3,T4,T5,T6>& )
185 // {
186 // T1 t1; T2 t2; T3 t3; T4 t4; T5 t5; T6 t6;
187 // return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + signature( t2 ) + signature( t3 ) + signature( t4 ) + signature( t5 ) + signature( t6 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
188 // }
189 //
190 // template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
191 // inline std::string signature( const Struct<T1,T2,T3,T4,T5,T6,T7>& )
192 // {
193 // T1 t1; T2 t2; T3 t3; T4 t4; T5 t5; T6 t6; T7 t7;
194 // return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + signature( t2 ) + signature( t3 ) + signature( t4 ) + signature( t5 ) + signature( t6 ) + signature( t7 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
195 // }
196 
197 
198 }
199 
200 inline
201 std::ostream& operator<<(std::ostream& sout, const DBus::Signature& sig)
202 {
203  sout << sig.str();
204  return sout;
205 }
206 
207 #endif
SignatureIterator iterator
Definition: signature.h:41
const SignatureIterator const_iterator
Definition: signature.h:43
Definition: accumulators.h:24
std::string::size_type size_type
Definition: signature.h:45
std::ostream & operator<<(std::ostream &sout, const DBus::Signature &sig)
Definition: signature.h:201
~Signature()
Definition: signature.cpp:50
A Variant is a type of DBus object that can be one of several different types of normal DBus object...
Definition: variant.h:33
iterator end()
Definition: signature.cpp:86
Signature & operator=(const std::string &s)
Definition: signature.cpp:64
Signature()
Definition: signature.cpp:26
const Signature::size_type npos
Definition: signature.cpp:24
std::string m_signature
Definition: signature.h:90
iterator begin()
Definition: signature.cpp:76
const std::string & str() const
Definition: signature.cpp:59
A FileDescriptor holds a UNIX file descriptor that can be passed between processes.
Definition: filedescriptor.h:30
Signature iterator allowing type signatures to be parsed.
Definition: signatureiterator.h:41
Definition: path.h:31
Definition: signature.h:37
std::string signature_dict_data(const std::map< Key, Data > &)
Definition: signature.h:137
std::string signature(uint8_t &)
Definition: signature.h:94
Signature(InputIterator first, InputIterator last)
Definition: signature.h:60
bool operator==(const std::string &s) const
Definition: signature.h:72
static const size_type npos
Definition: signature.h:47
bool is_valid() const
True if the signature is a valid DBus signature.
Definition: signature.cpp:96
bool is_singleton() const
True if the signature is a valid DBus signature and contains exactly one complete type...
Definition: signature.cpp:102

Generated on Sun Mar 19 2017 14:54:27 for dbus-cxx by doxygen 1.8.8