dbus-cxx logo

utility.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007,2008,2009 by Rick L. Vinyard, Jr.                  *
00003  *   rvinyard@cs.nmsu.edu                                                  *
00004  *                                                                         *
00005  *   This file is part of the dbus-cxx library.                            *
00006  *                                                                         *
00007  *   The dbus-cxx library is free software; you can redistribute it and/or *
00008  *   modify it under the terms of the GNU General Public License           *
00009  *   version 3 as published by the Free Software Foundation.               *
00010  *                                                                         *
00011  *   The dbus-cxx library is distributed in the hope that it will be       *
00012  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty   *
00013  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
00014  *   General Public License for more details.                              *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU General Public License     *
00017  *   along with this software. If not see <http://www.gnu.org/licenses/>.  *
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 //   #define DEBUG_OUT(x,y) ;
00042 //   #define DBUS_CXX_DEBUG(x) ;
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; /* This is invalid; you must use one of the specializations only */}
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   //   inline std::string signature( Variant )     { return DBUS_TYPE_VARIANT_AS_STRING; }
00172 //   template <typename T> inline std::string signature( const std::vector<T>& ) { T t; return DBUS_TYPE_ARRAY_AS_STRING + signature( t ); }
00173 
00174 //   template <typename Key,typename Data> inline std::string signature( const std::vector<std::pair<Key,Data> >& )
00175 //   {
00176 //     Key k; Data d;
00177 //     std::string sig;
00178 //     sig = DBUS_TYPE_ARRAY_AS_STRING;
00179 //     sig += DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING + signature(k) + signature(d) + DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
00180 //     return sig;
00181 //   }
00182 
00183 //   template <typename T1>
00184 //   inline std::string signature( const Struct<T1>& )
00185 //   {
00186 //     T1 t1;
00187 //     return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
00188 //   }
00189 // 
00190 //   template <typename T1, typename T2>
00191 //   inline std::string signature( const Struct<T1,T2>& )
00192 //   {
00193 //     T1 t1; T2 t2;
00194 //     return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + signature( t2 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
00195 //   }
00196 // 
00197 //   template <typename T1, typename T2, typename T3>
00198 //   inline std::string signature( const Struct<T1,T2,T3>& )
00199 //   {
00200 //     T1 t1; T2 t2; T3 t3;
00201 //     return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + signature( t2 ) + signature( t3 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
00202 //   }
00203 // 
00204 //   template <typename T1, typename T2, typename T3, typename T4>
00205 //   inline std::string signature( const Struct<T1,T2,T3,T4>& )
00206 //   {
00207 //     T1 t1; T2 t2; T3 t3; T4 t4;
00208 //     return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + signature( t2 ) + signature( t3 ) + signature( t4 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
00209 //   }
00210 // 
00211 //   template <typename T1, typename T2, typename T3, typename T4, typename T5>
00212 //   inline std::string signature( const Struct<T1,T2,T3,T4,T5>& )
00213 //   {
00214 //     T1 t1; T2 t2; T3 t3; T4 t4; T5 t5;
00215 //     return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + signature( t2 ) + signature( t3 ) + signature( t4 ) + signature( t5 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
00216 //   }
00217 // 
00218 //   template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
00219 //   inline std::string signature( const Struct<T1,T2,T3,T4,T5,T6>& )
00220 //   {
00221 //     T1 t1; T2 t2; T3 t3; T4 t4; T5 t5; T6 t6;
00222 //     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;
00223 //   }
00224 // 
00225 //   template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
00226 //   inline std::string signature( const Struct<T1,T2,T3,T4,T5,T6,T7>& )
00227 //   {
00228 //     T1 t1; T2 t2; T3 t3; T4 t4; T5 t5; T6 t6; T7 t7;
00229 //     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;
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 //   inline Type type( Variant )            { return TYPE_VARIANT; }
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(); /* This is 
00258 invalid; you must use one of the specializations only */}
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 //   template<> inline Type type<Variant>()            { return TYPE_VARIANT; }
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 //   template <typename T> inline Type type(const std::vector<T>&) { return TYPE_ARRAY; }
00286 
00287 //   template <typename T0>
00288 //   inline Type type( const Struct<T0>& ) { return TYPE_STRUCT; }
00289 // 
00290 //   template <typename T0, typename T1>
00291 //   inline Type type( const Struct<T0,T1>& ) { return TYPE_STRUCT; }
00292 // 
00293 //   template <typename T0, typename T1, typename T2>
00294 //   inline Type type( const Struct<T0,T1,T2>& ) { return TYPE_STRUCT; }
00295 // 
00296 //   template <typename T0, typename T1, typename T2, typename T3>
00297 //   inline Type type( const Struct<T0,T1,T2,T3>& ) { return TYPE_STRUCT; }
00298 // 
00299 //   template <typename T0, typename T1, typename T2, typename T3, typename T4>
00300 //   inline Type type( const Struct<T0,T1,T2,T3,T4>& ) { return TYPE_STRUCT; }
00301 // 
00302 //   template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
00303 //   inline Type type( const Struct<T0,T1,T2,T3,T4,T5>& ) { return TYPE_STRUCT; }
00304 // 
00305 //   template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
00306 //   inline Type type( const Struct<T0,T1,T2,T3,T4,T5,T6>& ) { return TYPE_STRUCT; }
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 //   inline std::string type_string( Variant )     { return "Variant"; }
00321 
00322   template <typename T> inline std::string type_string()   { return 1; /* This is invalid; you must use one of the specializations only */}
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 //   template<> inline std::string type_string<Variant>()     { return "Variant"; }
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

Generated on Wed Sep 30 11:14:36 2009 for dbus-cxx by doxygen 1.6.1