messageiterator.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef DBUSCXXMESSAGEITERATOR_H
00020 #define DBUSCXXMESSAGEITERATOR_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 #include <dbus-cxx/signature.h>
00031
00032 namespace DBus
00033 {
00034
00035 class Message;
00036
00044 class MessageIterator
00045 {
00046 public:
00047
00048 MessageIterator();
00049
00050 MessageIterator( const Message& message );
00051
00052 MessageIterator( DBusCxxPointer<Message> message );
00053
00058 const Message* message() const;
00059
00061 DBusMessageIter* cobj();
00062
00064 bool init( const Message& message );
00065
00067 void invalidate();
00068
00070 bool is_valid() const;
00071
00073 bool has_next() const;
00074
00080 bool next();
00081
00082 MessageIterator& operator ++();
00083
00084 MessageIterator operator ++( int );
00085
00086 bool operator==( const MessageIterator& other );
00087
00089 Type arg_type() const;
00090
00096 Type element_type() const;
00097
00099 bool is_fixed() const;
00100
00102 bool is_container() const;
00103
00105 bool is_array() const;
00106
00108 bool is_dict() const;
00109
00115 MessageIterator recurse();
00116
00118 std::string signature() const;
00119
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 operator bool();
00230 operator uint8_t();
00231 operator uint16_t();
00232 operator int16_t();
00233 operator uint32_t();
00234 operator int32_t();
00235 operator uint64_t();
00236 operator int64_t();
00237 operator double();
00238 operator const char*();
00239
00240 operator char();
00241 operator int8_t();
00242 operator float();
00243 #if DBUS_CXX_SIZEOF_LONG_INT == 4
00244 operator long int();
00245 operator unsigned long int();
00246 #endif
00247
00248 template <typename T>
00249 operator std::vector<T>() {
00250 return get_array<T>();
00251 }
00252
00253 bool get_bool();
00254 uint8_t get_uint8();
00255 uint16_t get_uint16();
00256 int16_t get_int16();
00257 uint32_t get_uint32();
00258 int32_t get_int32();
00259 uint64_t get_uint64();
00260 int64_t get_int64();
00261 double get_double();
00262 const char* get_string();
00263
00264 template <typename T>
00265 std::vector<T> get_array() {
00266
00267 if ( not this->is_array() )
00268 throw ErrorInvalidTypecast( "MessageIterator: Extracting non array into std::vector" );
00269
00270 if ( not this->is_fixed() )
00271 throw ErrorInvalidTypecast( "MessageIterator: Extracting non fixed array into std::vector" );
00272
00273 if ( this->element_type() != DBus::type<T>() ) {
00274 std::string s = "MessageIterator: Extracting DBus array type ";
00275 s += type_string<T>();
00276 s += " into C++ vector with RTTI type ";
00277 s += typeid( T ).name();
00278 throw ErrorInvalidTypecast( s.c_str() );
00279 }
00280
00281 std::vector<T> array;
00282 int elements;
00283 T* values;
00284
00285 MessageIterator subiter = this->recurse();
00286
00287
00288 dbus_message_iter_get_fixed_array( subiter.cobj(), &values, &elements );
00289
00290
00291 for ( int i=0; i < elements; i++ )
00292 array.push_back( values[i] );
00293
00294 return array;
00295 }
00296
00297 template <typename T>
00298 void get_array(std::vector<T>& array) {
00299
00300 if ( not this->is_array() )
00301 throw ErrorInvalidTypecast( "MessageIterator: Extracting non array into std::vector" );
00302
00303 if ( not this->is_fixed() )
00304 throw ErrorInvalidTypecast( "MessageIterator: Extracting non fixed array into std::vector" );
00305
00306 if ( this->element_type() != DBus::type<T>() ) {
00307 std::string s = "MessageIterator: Extracting DBus array type ";
00308 s += type_string<T>();
00309 s += " into C++ vector with RTTI type ";
00310 s += typeid( T ).name();
00311 throw ErrorInvalidTypecast( s.c_str() );
00312 }
00313
00314 int elements;
00315 T* values;
00316
00317 MessageIterator subiter = this->recurse();
00318
00319
00320 dbus_message_iter_get_fixed_array( subiter.cobj(), &values, &elements );
00321
00322
00323 array.clear();
00324 for ( int i=0; i < elements; i++ )
00325 array.push_back( values[i] );
00326 }
00327
00328 template <typename T>
00329 MessageIterator& operator>>( std::vector<T>& v )
00330 {
00331 this->get_array<T>(v);
00332 return *this;
00333 }
00334
00335 template <typename T>
00336 MessageIterator& operator>>( T& v )
00337 {
00338 v = (T)(*this);
00339 this->next();
00340 return *this;
00341 }
00342
00343 template <typename T>
00344 void value( T& temp ) {
00345 if ( this->arg_type() != DBus::type( temp ) ) {
00346 std::string s = "MessageIterator: Extracting DBus type ";
00347 s += type_string( temp );
00348 s += " into C++ RTTI type ";
00349 s += typeid( T ).name();
00350 throw ErrorInvalidTypecast( s.c_str() );
00351 }
00352 dbus_message_iter_get_basic( &m_cobj, &temp );
00353 }
00354
00355 protected:
00356 const Message* m_message;
00357 DBusMessageIter m_cobj;
00358
00359 };
00360
00361 }
00362
00363 #endif