utility.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef DBUSUTILITY_H
00021 #define DBUSUTILITY_H
00022
00023 #include <typeinfo>
00024 #include <string>
00025
00026 #include <sigc++/sigc++.h>
00027
00028 #include <dbus-cxx/dbus-cxx-config.h>
00029 #include <dbus-cxx/enums.h>
00030 #include <dbus-cxx/pointer.h>
00031 #include <dbus-cxx/path.h>
00032 #include <dbus-cxx/signature.h>
00033
00034 #define DBUS_CXX_PARAM_LIMIT 7
00035
00036 #ifdef DBUS_CXX_DEBUG_ENABLED
00037 #include <iostream>
00038
00039 #define DEBUG_OUT(x,y) if (DEBUG) std::cout << x << " " << pthread_self() << ": " << y << std::endl
00040 #define DBUS_CXX_DEBUG(x) std::cout << pthread_self() << ": " << x << std::endl
00041
00042
00043 #else
00044 #define DEBUG_OUT(x,y) ;
00045 #define DBUS_CXX_DEBUG(x) ;
00046 #endif
00047
00048 #define DBUS_CXX_INTROSPECTABLE_INTERFACE "org.freedesktop.DBus.Introspectable"
00049
00069 #define DBUS_CXX_ITERATOR_SUPPORT( CppType, DBusType ) \
00070 inline \
00071 DBus::MessageIterator& operator>>(DBus::MessageIterator& __msgiter, CppType& __cpptype) \
00072 { \
00073 DBusType __dbustype; \
00074 __msgiter >> __dbustype; \
00075 __cpptype = static_cast< CppType >( __dbustype ); \
00076 return __msgiter; \
00077 } \
00078 \
00079 inline \
00080 DBus::MessageAppendIterator& operator<<(DBus::MessageAppendIterator& __msgiter, CppType& __cpptype) \
00081 { \
00082 __msgiter << static_cast< DBusType >( __cpptype ); \
00083 return __msgiter; \
00084 } \
00085 \
00086 namespace DBus { \
00087 template<> inline std::string signature< CppType >() { return signature< DBusType >(); } \
00088 }
00089
00090
00091 namespace DBus
00092 {
00093
00094 typedef sigc::nil nil;
00095
00096 class Connection;
00097
00098 void init(bool threadsafe=true);
00099
00100 bool initialized();
00101
00102 std::string introspect( DBusCxxPointer<Connection> conn, const std::string& destination, const std::string& path );
00103
00104 inline std::string signature( uint8_t ) { return DBUS_TYPE_BYTE_AS_STRING; }
00105 inline std::string signature( bool ) { return DBUS_TYPE_BOOLEAN_AS_STRING; }
00106 inline std::string signature( int16_t ) { return DBUS_TYPE_INT16_AS_STRING; }
00107 inline std::string signature( uint16_t ) { return DBUS_TYPE_UINT16_AS_STRING; }
00108 inline std::string signature( int32_t ) { return DBUS_TYPE_INT32_AS_STRING; }
00109 inline std::string signature( uint32_t ) { return DBUS_TYPE_UINT32_AS_STRING; }
00110 inline std::string signature( int64_t ) { return DBUS_TYPE_INT64_AS_STRING; }
00111 inline std::string signature( uint64_t ) { return DBUS_TYPE_UINT64_AS_STRING; }
00112 inline std::string signature( double ) { return DBUS_TYPE_DOUBLE_AS_STRING; }
00113 inline std::string signature( std::string ) { return DBUS_TYPE_STRING_AS_STRING; }
00114 inline std::string signature( Signature ) { return DBUS_TYPE_SIGNATURE_AS_STRING; }
00115 inline std::string signature( Path ) { return DBUS_TYPE_OBJECT_PATH_AS_STRING; }
00116
00117 inline std::string signature( char ) { return DBUS_TYPE_BYTE_AS_STRING; }
00118 inline std::string signature( int8_t ) { return DBUS_TYPE_BYTE_AS_STRING; }
00119
00120 #if DBUS_CXX_SIZEOF_LONG_INT == 4
00121 inline std::string signature( long int ) { return DBUS_TYPE_INT32_AS_STRING; }
00122 inline std::string signature( long unsigned int ) { return DBUS_TYPE_UINT32_AS_STRING; }
00123 #endif
00124
00125 inline std::string signature( float ) { return DBUS_TYPE_DOUBLE_AS_STRING; }
00126
00127 template <typename T> inline std::string signature() { return 1; }
00128 template<> inline std::string signature<uint8_t>() { return DBUS_TYPE_BYTE_AS_STRING; }
00129 template<> inline std::string signature<bool>() { return DBUS_TYPE_BOOLEAN_AS_STRING; }
00130 template<> inline std::string signature<int16_t>() { return DBUS_TYPE_INT16_AS_STRING; }
00131 template<> inline std::string signature<uint16_t>() { return DBUS_TYPE_UINT16_AS_STRING; }
00132 template<> inline std::string signature<int32_t>() { return DBUS_TYPE_INT32_AS_STRING; }
00133 template<> inline std::string signature<uint32_t>() { return DBUS_TYPE_UINT32_AS_STRING; }
00134 template<> inline std::string signature<int64_t>() { return DBUS_TYPE_INT64_AS_STRING; }
00135 template<> inline std::string signature<uint64_t>() { return DBUS_TYPE_UINT64_AS_STRING; }
00136 template<> inline std::string signature<double>() { return DBUS_TYPE_DOUBLE_AS_STRING; }
00137 template<> inline std::string signature<std::string>() { return DBUS_TYPE_STRING_AS_STRING; }
00138 template<> inline std::string signature<Signature>() { return DBUS_TYPE_SIGNATURE_AS_STRING; }
00139 template<> inline std::string signature<Path>() { return DBUS_TYPE_OBJECT_PATH_AS_STRING; }
00140
00141 template<> inline std::string signature<char>() { return DBUS_TYPE_BYTE_AS_STRING; }
00142 template<> inline std::string signature<int8_t>() { return DBUS_TYPE_BYTE_AS_STRING; }
00143
00144 #if DBUS_CXX_SIZEOF_LONG_INT == 4
00145 template<> inline std::string signature<long int>() { return DBUS_TYPE_INT32_AS_STRING; }
00146 template<> inline std::string signature<long unsigned int>() { return DBUS_TYPE_UINT32_AS_STRING; }
00147 #endif
00148
00149 template<> inline std::string signature<float>() { return DBUS_TYPE_DOUBLE_AS_STRING; }
00150
00151 template<> inline std::string signature<std::vector<uint8_t> >() { return DBUS_TYPE_BYTE_AS_STRING; }
00152 template<> inline std::string signature<std::vector<bool> >() { return DBUS_TYPE_BOOLEAN_AS_STRING; }
00153 template<> inline std::string signature<std::vector<int16_t> >() { return DBUS_TYPE_INT16_AS_STRING; }
00154 template<> inline std::string signature<std::vector<uint16_t> >() { return DBUS_TYPE_UINT16_AS_STRING; }
00155 template<> inline std::string signature<std::vector<int32_t> >() { return DBUS_TYPE_INT32_AS_STRING; }
00156 template<> inline std::string signature<std::vector<uint32_t> >() { return DBUS_TYPE_UINT32_AS_STRING; }
00157 template<> inline std::string signature<std::vector<int64_t> >() { return DBUS_TYPE_INT64_AS_STRING; }
00158 template<> inline std::string signature<std::vector<uint64_t> >() { return DBUS_TYPE_UINT64_AS_STRING; }
00159 template<> inline std::string signature<std::vector<double> >() { return DBUS_TYPE_DOUBLE_AS_STRING; }
00160
00161 template<> inline std::string signature<std::vector<char> >() { return DBUS_TYPE_BYTE_AS_STRING; }
00162 template<> inline std::string signature<std::vector<int8_t> >() { return DBUS_TYPE_BYTE_AS_STRING; }
00163
00164 #if DBUS_CXX_SIZEOF_LONG_INT == 4
00165 template<> inline std::string signature<std::vector<long int> >() { return DBUS_TYPE_INT32_AS_STRING; }
00166 template<> inline std::string signature<std::vector<long unsigned int> >() { return DBUS_TYPE_UINT32_AS_STRING; }
00167 #endif
00168
00169 template<> inline std::string signature<std::vector<float> >() { return DBUS_TYPE_DOUBLE_AS_STRING; }
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 inline Type type( uint8_t ) { return TYPE_BYTE; }
00233 inline Type type( bool ) { return TYPE_BOOLEAN; }
00234 inline Type type( int16_t ) { return TYPE_INT16; }
00235 inline Type type( uint16_t ) { return TYPE_UINT16; }
00236 inline Type type( int32_t ) { return TYPE_INT32; }
00237 inline Type type( uint32_t ) { return TYPE_UINT32; }
00238 inline Type type( int64_t ) { return TYPE_INT64; }
00239 inline Type type( uint64_t ) { return TYPE_UINT64; }
00240 inline Type type( double ) { return TYPE_DOUBLE; }
00241 inline Type type( const std::string& ) { return TYPE_STRING; }
00242 inline Type type( const char* ) { return TYPE_STRING; }
00243 inline Type type( Path ) { return TYPE_OBJECT_PATH; }
00244 inline Type type( Signature ) { return TYPE_SIGNATURE; }
00245
00246
00247 inline Type type( char ) { return TYPE_BYTE; }
00248 inline Type type( int8_t ) { return TYPE_BYTE; }
00249
00250 #if DBUS_CXX_SIZEOF_LONG_INT == 4
00251 inline Type type( long int ) { return TYPE_INT32; }
00252 inline Type type( long unsigned int ) { return TYPE_UINT32; }
00253 #endif
00254
00255 inline Type type( float ) { return TYPE_DOUBLE; }
00256
00257 template <typename T> inline Type type() { return std::string();
00258 }
00259 template<> inline Type type<uint8_t>() { return TYPE_BYTE; }
00260 template<> inline Type type<bool>() { return TYPE_BOOLEAN; }
00261 template<> inline Type type<int16_t>() { return TYPE_INT16; }
00262 template<> inline Type type<uint16_t>() { return TYPE_UINT16; }
00263 template<> inline Type type<int32_t>() { return TYPE_INT32; }
00264 template<> inline Type type<uint32_t>() { return TYPE_UINT32; }
00265 template<> inline Type type<int64_t>() { return TYPE_INT64; }
00266 template<> inline Type type<uint64_t>() { return TYPE_UINT64; }
00267 template<> inline Type type<double>() { return TYPE_DOUBLE; }
00268 template<> inline Type type<const std::string&>() { return TYPE_STRING; }
00269 template<> inline Type type<const char*>() { return TYPE_STRING; }
00270 template<> inline Type type<Path>() { return TYPE_OBJECT_PATH; }
00271 template<> inline Type type<Signature>() { return TYPE_SIGNATURE; }
00272
00273
00274 template<> inline Type type<char>() { return TYPE_BYTE; }
00275 template<> inline Type type<int8_t>() { return TYPE_BYTE; }
00276
00277 #if DBUS_CXX_SIZEOF_LONG_INT == 4
00278 template<> inline Type type<long int>() { return TYPE_INT32; }
00279 template<> inline Type type<long unsigned int>() { return TYPE_UINT32; }
00280 #endif
00281
00282 template<> inline Type type<float>() { return TYPE_DOUBLE; }
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308 inline std::string type_string( uint8_t ) { return "byte"; }
00309 inline std::string type_string( bool ) { return "boolean"; }
00310 inline std::string type_string( int16_t ) { return "int16_t"; }
00311 inline std::string type_string( uint16_t ) { return "uint16_t"; }
00312 inline std::string type_string( int32_t ) { return "int32_t"; }
00313 inline std::string type_string( uint32_t ) { return "uint32_t"; }
00314 inline std::string type_string( int64_t ) { return "int64_t"; }
00315 inline std::string type_string( uint64_t ) { return "uint64_t"; }
00316 inline std::string type_string( double ) { return "double"; }
00317 inline std::string type_string( std::string ) { return "std::string"; }
00318 inline std::string type_string( Path ) { return "Path"; }
00319 inline std::string type_string( Signature ) { return "Signature"; }
00320
00321
00322 template <typename T> inline std::string type_string() { return 1; }
00323 template<> inline std::string type_string<uint8_t>() { return "byte"; }
00324 template<> inline std::string type_string<bool>() { return "boolean"; }
00325 template<> inline std::string type_string<int16_t>() { return "int16_t"; }
00326 template<> inline std::string type_string<uint16_t>() { return "uint16_t"; }
00327 template<> inline std::string type_string<int32_t>() { return "int32_t"; }
00328 template<> inline std::string type_string<uint32_t>() { return "uint32_t"; }
00329 template<> inline std::string type_string<int64_t>() { return "int64_t"; }
00330 template<> inline std::string type_string<uint64_t>() { return "uint64_t"; }
00331 template<> inline std::string type_string<double>() { return "double"; }
00332 template<> inline std::string type_string<std::string>() { return "std::string"; }
00333 template<> inline std::string type_string<Path>() { return "Path"; }
00334 template<> inline std::string type_string<Signature>() { return "Signature"; }
00335
00336
00337 inline std::string type_string_from_code( DBus::Type t )
00338 {
00339 switch ( t )
00340 {
00341 case TYPE_BYTE:
00342 return "byte";
00343 case TYPE_BOOLEAN:
00344 return "boolean";
00345 case TYPE_INT16:
00346 return "int16_t";
00347 case TYPE_UINT16:
00348 return "uint16_t";
00349 case TYPE_INT32:
00350 return "int32_t";
00351 case TYPE_UINT32:
00352 return "uint32_t";
00353 case TYPE_INT64:
00354 return "int64_t";
00355 case TYPE_UINT64:
00356 return "uint64_t";
00357 case TYPE_DOUBLE:
00358 return "double";
00359 case TYPE_STRING:
00360 return "string";
00361 case TYPE_OBJECT_PATH:
00362 return "Path";
00363 case TYPE_SIGNATURE:
00364 return "Signature";
00365 case TYPE_ARRAY:
00366 return "Array";
00367 case TYPE_VARIANT:
00368 return "Variant";
00369 case TYPE_STRUCT:
00370 return "Struct";
00371 case TYPE_DICT_ENTRY:
00372 return "Dict Entry";
00373 default:
00374 break;
00375 }
00376 return std::string("Invalid");
00377 }
00378
00379 Type checked_type_cast( int n );
00380
00381 }
00382
00383 #endif