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 <string> 00020 #include <vector> 00021 00022 #include <dbus/dbus.h> 00023 00024 #include <dbus-cxx/types.h> 00025 00026 #ifndef DBUSCXX_MESSAGEAPPENDITERATOR_H 00027 #define DBUSCXX_MESSAGEAPPENDITERATOR_H 00028 00029 namespace DBus 00030 { 00031 00032 class Message; 00033 00041 class MessageAppendIterator 00042 { 00043 public: 00044 00045 MessageAppendIterator(); 00046 00047 MessageAppendIterator( Message& message ); 00048 00049 MessageAppendIterator( DBusCxxPointer<Message> message ); 00050 00051 ~MessageAppendIterator(); 00052 00057 const Message* message() const; 00058 00060 DBusMessageIter* cobj(); 00061 00063 bool init( Message& message ); 00064 00066 void invalidate(); 00067 00069 bool is_valid() const; 00070 00072 operator bool() const; 00073 00074 bool append( bool v ); 00075 bool append( uint8_t v ); 00076 bool append( int16_t v ); 00077 bool append( uint16_t v ); 00078 bool append( int32_t v ); 00079 bool append( uint32_t v ); 00080 bool append( int64_t v ); 00081 bool append( uint64_t v ); 00082 bool append( double v ); 00083 bool append( const char* v ); 00084 bool append( const std::string& v ); 00085 bool append( const Signature& v ); 00086 bool append( const Path& v ); 00087 00088 bool append( char v ); 00089 bool append( int8_t v ); 00090 bool append( float v ); 00091 #if DBUS_CXX_SIZEOF_LONG_INT == 4 00092 bool append( long int v ); 00093 bool append( long unsigned int v ); 00094 #endif 00095 00096 template <typename T> 00097 void append( const std::vector<T>& v ) { 00098 this->open_container( CONTAINER_ARRAY, DBus::signature<T>().c_str() ); 00099 00100 for ( size_t i=0; i < v.size(); i++ ) 00101 *m_subiter << v[i]; 00102 00103 this->close_container(); 00104 } 00105 00106 template <typename T> 00107 MessageAppendIterator& operator<<( const T& v ) 00108 { 00109 this->append( v ); 00110 return *this; 00111 } 00112 00113 00114 // template <typename T0, typename T1> 00115 // void append(const Struct<T0,T1>& s) { 00116 // this->open_container( CONTAINER_STRUCT, std::string() ); 00117 // m_subiter->append( boost::get<0>(s) ); 00118 // m_subiter->append( boost::get<1>(s) ); 00119 // this->close_container(); 00120 // } 00121 // 00122 // template <typename T0, typename T1, typename T2> 00123 // void append(const Struct<T0,T1,T2>& s) { 00124 // this->open_container( CONTAINER_STRUCT, std::string() ); 00125 // m_subiter->append( boost::get<0>(s) ); 00126 // m_subiter->append( boost::get<1>(s) ); 00127 // m_subiter->append( boost::get<2>(s) ); 00128 // this->close_container(); 00129 // } 00130 // 00131 // template <typename T0, typename T1, typename T2, typename T3> 00132 // void append(const Struct<T0,T1,T2,T3>& s) { 00133 // this->open_container( CONTAINER_STRUCT, std::string() ); 00134 // m_subiter->append( boost::get<0>(s) ); 00135 // m_subiter->append( boost::get<1>(s) ); 00136 // m_subiter->append( boost::get<2>(s) ); 00137 // m_subiter->append( boost::get<3>(s) ); 00138 // this->close_container(); 00139 // } 00140 // 00141 // template <typename T0, typename T1, typename T2, typename T3, typename T4> 00142 // void append(const Struct<T0,T1,T2,T3,T4>& s) { 00143 // this->open_container( CONTAINER_STRUCT, std::string() ); 00144 // m_subiter->append( boost::get<0>(s) ); 00145 // m_subiter->append( boost::get<1>(s) ); 00146 // m_subiter->append( boost::get<2>(s) ); 00147 // m_subiter->append( boost::get<3>(s) ); 00148 // m_subiter->append( boost::get<4>(s) ); 00149 // this->close_container(); 00150 // } 00151 // 00152 // template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5> 00153 // void append(const Struct<T0,T1,T2,T3,T4,T5>& s) { 00154 // this->open_container( CONTAINER_STRUCT, std::string() ); 00155 // m_subiter->append( boost::get<0>(s) ); 00156 // m_subiter->append( boost::get<1>(s) ); 00157 // m_subiter->append( boost::get<2>(s) ); 00158 // m_subiter->append( boost::get<3>(s) ); 00159 // m_subiter->append( boost::get<4>(s) ); 00160 // m_subiter->append( boost::get<5>(s) ); 00161 // this->close_container(); 00162 // } 00163 // 00164 // template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6> 00165 // void append(const Struct<T0,T1,T2,T3,T4,T5,T6>& s) { 00166 // this->open_container( CONTAINER_STRUCT, std::string() ); 00167 // m_subiter->append( boost::get<0>(s) ); 00168 // m_subiter->append( boost::get<1>(s) ); 00169 // m_subiter->append( boost::get<2>(s) ); 00170 // m_subiter->append( boost::get<3>(s) ); 00171 // m_subiter->append( boost::get<4>(s) ); 00172 // m_subiter->append( boost::get<5>(s) ); 00173 // m_subiter->append( boost::get<6>(s) ); 00174 // this->close_container(); 00175 // } 00176 00177 // template <typename Key, typename Data> 00178 // void append( const std::vector<std::pair<Key,Data> >& dictionary ) { 00179 // std::string sig = signature( dictionary ); 00180 // sig.erase( 0 ); 00181 // this->open_container( CONTAINER_ARRAY, sig ); 00182 // for ( int i = 0; i != dictionary.size(); i++ ) { 00183 // m_subiter->open_container( CONTAINER_DICT_ENTRY, std::string() ); 00184 // m_subiter->m_subiter->append( dictionary[i].first ); 00185 // m_subiter->m_subiter->append( dictionary[i].second ); 00186 // m_subiter->close_container(); 00187 // } 00188 // this->close_container(); 00189 // } 00190 00191 void open_container( ContainerType t, const std::string& contained_signature ); 00192 00193 void close_container( ); 00194 00195 MessageAppendIterator* sub_iterator(); 00196 00197 // bool append_array( char type, const void* ptr, size_t length ); 00198 00199 // iterator new_array( const char* sig ); 00200 00201 // iterator new_variant( const char* sig ); 00202 00203 // iterator new_struct(); 00204 00205 // iterator new_dict_entry(); 00206 00207 // void close_container( iterator& container ); 00208 00209 protected: 00210 Message* m_message; 00211 DBusMessageIter m_cobj; 00212 MessageAppendIterator* m_subiter; 00213 00214 template <typename T> bool protected_append( const T& v ); 00215 bool protected_append( const bool& v ); 00216 bool protected_append( const std::string& v ); 00217 bool protected_append( const Signature& v ); 00218 bool protected_append( const Path& v ); 00219 00220 }; 00221 00222 } 00223 00224 #endif 00225