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 
00152 //   inline std::string signature( Variant )     { return DBUS_TYPE_VARIANT_AS_STRING; }
00153 //   template <typename T> inline std::string signature( const std::vector<T>& ) { T t; return DBUS_TYPE_ARRAY_AS_STRING + signature( t ); }
00154 
00155 //   template <typename Key,typename Data> inline std::string signature( const std::vector<std::pair<Key,Data> >& )
00156 //   {
00157 //     Key k; Data d;
00158 //     std::string sig;
00159 //     sig = DBUS_TYPE_ARRAY_AS_STRING;
00160 //     sig += DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING + signature(k) + signature(d) + DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
00161 //     return sig;
00162 //   }
00163 
00164 //   template <typename T1>
00165 //   inline std::string signature( const Struct<T1>& )
00166 //   {
00167 //     T1 t1;
00168 //     return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
00169 //   }
00170 // 
00171 //   template <typename T1, typename T2>
00172 //   inline std::string signature( const Struct<T1,T2>& )
00173 //   {
00174 //     T1 t1; T2 t2;
00175 //     return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + signature( t2 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
00176 //   }
00177 // 
00178 //   template <typename T1, typename T2, typename T3>
00179 //   inline std::string signature( const Struct<T1,T2,T3>& )
00180 //   {
00181 //     T1 t1; T2 t2; T3 t3;
00182 //     return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + signature( t2 ) + signature( t3 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
00183 //   }
00184 // 
00185 //   template <typename T1, typename T2, typename T3, typename T4>
00186 //   inline std::string signature( const Struct<T1,T2,T3,T4>& )
00187 //   {
00188 //     T1 t1; T2 t2; T3 t3; T4 t4;
00189 //     return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + signature( t2 ) + signature( t3 ) + signature( t4 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
00190 //   }
00191 // 
00192 //   template <typename T1, typename T2, typename T3, typename T4, typename T5>
00193 //   inline std::string signature( const Struct<T1,T2,T3,T4,T5>& )
00194 //   {
00195 //     T1 t1; T2 t2; T3 t3; T4 t4; T5 t5;
00196 //     return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + signature( t2 ) + signature( t3 ) + signature( t4 ) + signature( t5 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
00197 //   }
00198 // 
00199 //   template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
00200 //   inline std::string signature( const Struct<T1,T2,T3,T4,T5,T6>& )
00201 //   {
00202 //     T1 t1; T2 t2; T3 t3; T4 t4; T5 t5; T6 t6;
00203 //     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;
00204 //   }
00205 // 
00206 //   template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
00207 //   inline std::string signature( const Struct<T1,T2,T3,T4,T5,T6,T7>& )
00208 //   {
00209 //     T1 t1; T2 t2; T3 t3; T4 t4; T5 t5; T6 t6; T7 t7;
00210 //     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;
00211 //   }
00212 
00213   inline Type type( uint8_t )            { return TYPE_BYTE; }
00214   inline Type type( bool )               { return TYPE_BOOLEAN; }
00215   inline Type type( int16_t )            { return TYPE_INT16; }
00216   inline Type type( uint16_t )           { return TYPE_UINT16; }
00217   inline Type type( int32_t )            { return TYPE_INT32; }
00218   inline Type type( uint32_t )           { return TYPE_UINT32; }
00219   inline Type type( int64_t )            { return TYPE_INT64; }
00220   inline Type type( uint64_t )           { return TYPE_UINT64; }
00221   inline Type type( double )             { return TYPE_DOUBLE; }
00222   inline Type type( const std::string& ) { return TYPE_STRING; }
00223   inline Type type( const char* )        { return TYPE_STRING; }
00224   inline Type type( Path )               { return TYPE_OBJECT_PATH; }
00225   inline Type type( Signature )          { return TYPE_SIGNATURE; }
00226 //   inline Type type( Variant )            { return TYPE_VARIANT; }
00227   
00228   inline Type type( char )               { return TYPE_BYTE; }
00229   inline Type type( int8_t )             { return TYPE_BYTE; }
00230 
00231 #if DBUS_CXX_SIZEOF_LONG_INT == 4
00232   inline Type type( long int )            { return TYPE_INT32; }
00233   inline Type type( long unsigned int )   { return TYPE_UINT32; }
00234 #endif
00235   
00236   inline Type type( float )               { return TYPE_DOUBLE; }
00237 
00238 
00239 //   template <typename T> inline Type type(const std::vector<T>&) { return TYPE_ARRAY; }
00240 
00241 //   template <typename T0>
00242 //   inline Type type( const Struct<T0>& ) { return TYPE_STRUCT; }
00243 // 
00244 //   template <typename T0, typename T1>
00245 //   inline Type type( const Struct<T0,T1>& ) { return TYPE_STRUCT; }
00246 // 
00247 //   template <typename T0, typename T1, typename T2>
00248 //   inline Type type( const Struct<T0,T1,T2>& ) { return TYPE_STRUCT; }
00249 // 
00250 //   template <typename T0, typename T1, typename T2, typename T3>
00251 //   inline Type type( const Struct<T0,T1,T2,T3>& ) { return TYPE_STRUCT; }
00252 // 
00253 //   template <typename T0, typename T1, typename T2, typename T3, typename T4>
00254 //   inline Type type( const Struct<T0,T1,T2,T3,T4>& ) { return TYPE_STRUCT; }
00255 // 
00256 //   template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
00257 //   inline Type type( const Struct<T0,T1,T2,T3,T4,T5>& ) { return TYPE_STRUCT; }
00258 // 
00259 //   template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
00260 //   inline Type type( const Struct<T0,T1,T2,T3,T4,T5,T6>& ) { return TYPE_STRUCT; }
00261 
00262   inline std::string type_string( uint8_t )     { return "byte"; }
00263   inline std::string type_string( bool )        { return "boolean"; }
00264   inline std::string type_string( int16_t )     { return "int16_t"; }
00265   inline std::string type_string( uint16_t )    { return "uint16_t"; }
00266   inline std::string type_string( int32_t )     { return "int32_t"; }
00267   inline std::string type_string( uint32_t )    { return "uint32_t"; }
00268   inline std::string type_string( int64_t )     { return "int64_t"; }
00269   inline std::string type_string( uint64_t )    { return "uint64_t"; }
00270   inline std::string type_string( double )      { return "double"; }
00271   inline std::string type_string( std::string ) { return "std::string"; }
00272   inline std::string type_string( Path )        { return "Path"; }
00273   inline std::string type_string( Signature )   { return "Signature"; }
00274 //   inline std::string type_string( Variant )     { return "Variant"; }
00275 
00276   inline std::string type_string_from_code( DBus::Type t )
00277   {
00278     switch ( t )
00279     {
00280       case TYPE_BYTE:
00281         return "byte";
00282       case TYPE_BOOLEAN:
00283         return "boolean";
00284       case TYPE_INT16:
00285         return "int16_t";
00286       case TYPE_UINT16:
00287         return "uint16_t";
00288       case TYPE_INT32:
00289         return "int32_t";
00290       case TYPE_UINT32:
00291         return "uint32_t";
00292       case TYPE_INT64:
00293         return "int64_t";
00294       case TYPE_UINT64:
00295         return "uint64_t";
00296       case TYPE_DOUBLE:
00297         return "double";
00298       case TYPE_STRING:
00299         return "string";
00300       case TYPE_OBJECT_PATH:
00301         return "Path";
00302       case TYPE_SIGNATURE:
00303         return "Signature";
00304       case TYPE_ARRAY:
00305         return "Array";
00306       case TYPE_VARIANT:
00307         return "Variant";
00308       case TYPE_STRUCT:
00309         return "Struct";
00310       case TYPE_DICT_ENTRY:
00311         return "Dict Entry";
00312       default:
00313         break;
00314     }
00315     return std::string("Invalid");
00316   }
00317 
00318   Type checked_type_cast( int n );
00319 
00320 }
00321 
00322 #endif

Generated on Fri Jun 26 15:50:38 2009 for dbus-cxx by doxygen 1.5.8