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