dbus-cxx logo

signature.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2009,2010 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 #include <ostream>
00020 #include <string>
00021 #include <dbus-cxx/signatureiterator.h>
00022 #include <dbus-cxx/path.h>
00023 
00024 #ifndef DBUSCXX_SIGNATURE_H
00025 #define DBUSCXX_SIGNATURE_H
00026 
00027 namespace DBus
00028 {
00029 
00033   class Signature
00034   {
00035     public:
00036 
00037       typedef SignatureIterator iterator;
00038 
00039       typedef const SignatureIterator const_iterator;
00040 
00041       typedef std::string::size_type size_type;
00042 
00043       static const size_type npos;
00044 
00045       Signature();
00046 
00047       Signature( const std::string& s, size_type pos = 0, size_type n = npos );
00048 
00049       Signature( const char* );
00050 
00051       Signature( const char* s, size_type n );
00052 
00053       Signature( size_type n, char c );
00054 
00055       template<class InputIterator>
00056       Signature( InputIterator first, InputIterator last ): m_signature( first, last ) { }
00057 
00058       ~Signature();
00059 
00060       operator const std::string&() const;
00061 
00062       const std::string& str() const;
00063 
00064       Signature& operator=(const std::string& s);
00065 
00066       Signature& operator=(const char* s);
00067 
00068       bool operator==(const std::string& s) const { return m_signature == s; }
00069 
00070       iterator begin();
00071 
00072       const_iterator begin() const;
00073 
00074       iterator end();
00075 
00076       const_iterator end() const;
00077 
00079       bool is_valid() const;
00080 
00082       bool is_singleton() const;
00083 
00084     protected:
00085 
00086       std::string m_signature;
00087 
00088   };
00089 
00090   inline std::string signature( uint8_t )     { return DBUS_TYPE_BYTE_AS_STRING; }
00091   inline std::string signature( bool )        { return DBUS_TYPE_BOOLEAN_AS_STRING; }
00092   inline std::string signature( int16_t )     { return DBUS_TYPE_INT16_AS_STRING; }
00093   inline std::string signature( uint16_t )    { return DBUS_TYPE_UINT16_AS_STRING; }
00094   inline std::string signature( int32_t )     { return DBUS_TYPE_INT32_AS_STRING; }
00095   inline std::string signature( uint32_t )    { return DBUS_TYPE_UINT32_AS_STRING; }
00096   inline std::string signature( int64_t )     { return DBUS_TYPE_INT64_AS_STRING; }
00097   inline std::string signature( uint64_t )    { return DBUS_TYPE_UINT64_AS_STRING;      }
00098   inline std::string signature( double )      { return DBUS_TYPE_DOUBLE_AS_STRING;      }
00099   inline std::string signature( std::string ) { return DBUS_TYPE_STRING_AS_STRING;      }
00100   inline std::string signature( Signature )   { return DBUS_TYPE_SIGNATURE_AS_STRING;   }
00101   inline std::string signature( Path )        { return DBUS_TYPE_OBJECT_PATH_AS_STRING; }
00102 
00103   inline std::string signature( char )        { return DBUS_TYPE_BYTE_AS_STRING;        }
00104   inline std::string signature( int8_t )      { return DBUS_TYPE_BYTE_AS_STRING;        }
00105   
00106 #if DBUS_CXX_SIZEOF_LONG_INT == 4
00107   inline std::string signature( long int )          { return DBUS_TYPE_INT32_AS_STRING;       }
00108   inline std::string signature( long unsigned int ) { return DBUS_TYPE_UINT32_AS_STRING;      }
00109 #endif
00110   
00111   inline std::string signature( float )         { return DBUS_TYPE_DOUBLE_AS_STRING; }
00112   
00113   template <typename T> inline std::string signature()   { return 1; /* This is invalid; you must use one of the specializations only */}
00114   template<> inline std::string signature<uint8_t>()     { return DBUS_TYPE_BYTE_AS_STRING;        }
00115   template<> inline std::string signature<bool>()        { return DBUS_TYPE_BOOLEAN_AS_STRING;     }
00116   template<> inline std::string signature<int16_t>()     { return DBUS_TYPE_INT16_AS_STRING;       }
00117   template<> inline std::string signature<uint16_t>()    { return DBUS_TYPE_UINT16_AS_STRING;      }
00118   template<> inline std::string signature<int32_t>()     { return DBUS_TYPE_INT32_AS_STRING;       }
00119   template<> inline std::string signature<uint32_t>()    { return DBUS_TYPE_UINT32_AS_STRING;      }
00120   template<> inline std::string signature<int64_t>()     { return DBUS_TYPE_INT64_AS_STRING;       }
00121   template<> inline std::string signature<uint64_t>()    { return DBUS_TYPE_UINT64_AS_STRING;      }
00122   template<> inline std::string signature<double>()      { return DBUS_TYPE_DOUBLE_AS_STRING;      }
00123   template<> inline std::string signature<std::string>() { return DBUS_TYPE_STRING_AS_STRING;      }
00124   template<> inline std::string signature<Signature>()   { return DBUS_TYPE_SIGNATURE_AS_STRING;   }
00125   template<> inline std::string signature<Path>()        { return DBUS_TYPE_OBJECT_PATH_AS_STRING; }
00126   
00127   template<> inline std::string signature<char>()        { return DBUS_TYPE_BYTE_AS_STRING;        }
00128   template<> inline std::string signature<int8_t>()      { return DBUS_TYPE_BYTE_AS_STRING;        }
00129 
00130 #if DBUS_CXX_SIZEOF_LONG_INT == 4
00131   template<> inline std::string signature<long int>()          { return DBUS_TYPE_INT32_AS_STRING;       }
00132   template<> inline std::string signature<long unsigned int>() { return DBUS_TYPE_UINT32_AS_STRING;       }
00133 #endif
00134   
00135   template<> inline std::string signature<float>()         { return DBUS_TYPE_DOUBLE_AS_STRING; }
00136 
00137   template<> inline std::string signature<std::vector<uint8_t> >()     { return DBUS_TYPE_BYTE_AS_STRING;        }
00138   template<> inline std::string signature<std::vector<bool> >()        { return DBUS_TYPE_BOOLEAN_AS_STRING;     }
00139   template<> inline std::string signature<std::vector<int16_t> >()     { return DBUS_TYPE_INT16_AS_STRING;       }
00140   template<> inline std::string signature<std::vector<uint16_t> >()    { return DBUS_TYPE_UINT16_AS_STRING;      }
00141   template<> inline std::string signature<std::vector<int32_t> >()     { return DBUS_TYPE_INT32_AS_STRING;       }
00142   template<> inline std::string signature<std::vector<uint32_t> >()    { return DBUS_TYPE_UINT32_AS_STRING;      }
00143   template<> inline std::string signature<std::vector<int64_t> >()     { return DBUS_TYPE_INT64_AS_STRING;       }
00144   template<> inline std::string signature<std::vector<uint64_t> >()    { return DBUS_TYPE_UINT64_AS_STRING;      }
00145   template<> inline std::string signature<std::vector<double> >()      { return DBUS_TYPE_DOUBLE_AS_STRING;      }
00146 
00147   template<> inline std::string signature<std::vector<char> >()        { return DBUS_TYPE_BYTE_AS_STRING;        }
00148   template<> inline std::string signature<std::vector<int8_t> >()      { return DBUS_TYPE_BYTE_AS_STRING;        }
00149 
00150 #if DBUS_CXX_SIZEOF_LONG_INT == 4
00151   template<> inline std::string signature<std::vector<long int> >()          { return DBUS_TYPE_INT32_AS_STRING;       }
00152   template<> inline std::string signature<std::vector<long unsigned int> >() { return DBUS_TYPE_UINT32_AS_STRING;       }
00153 #endif
00154   
00155   template<> inline std::string signature<std::vector<float> >()         { return DBUS_TYPE_DOUBLE_AS_STRING; }
00156 
00157   //   inline std::string signature( Variant )     { return DBUS_TYPE_VARIANT_AS_STRING; }
00158 //   template <typename T> inline std::string signature( const std::vector<T>& ) { T t; return DBUS_TYPE_ARRAY_AS_STRING + signature( t ); }
00159 
00160 //   template <typename Key,typename Data> inline std::string signature( const std::vector<std::pair<Key,Data> >& )
00161 //   {
00162 //     Key k; Data d;
00163 //     std::string sig;
00164 //     sig = DBUS_TYPE_ARRAY_AS_STRING;
00165 //     sig += DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING + signature(k) + signature(d) + DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
00166 //     return sig;
00167 //   }
00168 
00169 //   template <typename T1>
00170 //   inline std::string signature( const Struct<T1>& )
00171 //   {
00172 //     T1 t1;
00173 //     return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
00174 //   }
00175 // 
00176 //   template <typename T1, typename T2>
00177 //   inline std::string signature( const Struct<T1,T2>& )
00178 //   {
00179 //     T1 t1; T2 t2;
00180 //     return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + signature( t2 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
00181 //   }
00182 // 
00183 //   template <typename T1, typename T2, typename T3>
00184 //   inline std::string signature( const Struct<T1,T2,T3>& )
00185 //   {
00186 //     T1 t1; T2 t2; T3 t3;
00187 //     return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + signature( t2 ) + signature( t3 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
00188 //   }
00189 // 
00190 //   template <typename T1, typename T2, typename T3, typename T4>
00191 //   inline std::string signature( const Struct<T1,T2,T3,T4>& )
00192 //   {
00193 //     T1 t1; T2 t2; T3 t3; T4 t4;
00194 //     return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + signature( t2 ) + signature( t3 ) + signature( t4 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
00195 //   }
00196 // 
00197 //   template <typename T1, typename T2, typename T3, typename T4, typename T5>
00198 //   inline std::string signature( const Struct<T1,T2,T3,T4,T5>& )
00199 //   {
00200 //     T1 t1; T2 t2; T3 t3; T4 t4; T5 t5;
00201 //     return DBUS_STRUCT_BEGIN_CHAR_AS_STRING + signature( t1 ) + signature( t2 ) + signature( t3 ) + signature( t4 ) + signature( t5 ) + DBUS_STRUCT_END_CHAR_AS_STRING;
00202 //   }
00203 // 
00204 //   template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
00205 //   inline std::string signature( const Struct<T1,T2,T3,T4,T5,T6>& )
00206 //   {
00207 //     T1 t1; T2 t2; T3 t3; T4 t4; T5 t5; T6 t6;
00208 //     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;
00209 //   }
00210 // 
00211 //   template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
00212 //   inline std::string signature( const Struct<T1,T2,T3,T4,T5,T6,T7>& )
00213 //   {
00214 //     T1 t1; T2 t2; T3 t3; T4 t4; T5 t5; T6 t6; T7 t7;
00215 //     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;
00216 //   }
00217 
00218 
00219 }
00220 
00221 inline
00222 std::ostream& operator<<(std::ostream& sout, const DBus::Signature& sig)
00223 {
00224   sout << sig.str();
00225   return sout;
00226 }
00227 
00228 #endif

Generated on Thu Mar 18 09:30:55 2010 for dbus-cxx by doxygen 1.6.1