field_iterator.cpp

00001
00002 /***************************************************************************
00003  *  field_iterator.cpp - Iterate over field of an interface or a message
00004  *
00005  *  Created: Fri Jul 17 21:28:58 2009
00006  *  Copyright  2006  Tim Niemueller [www.niemueller.de]
00007  *             2009  Daniel Beck
00008  *
00009  ****************************************************************************/
00010
00011 /*  This program is free software; you can redistribute it and/or modify
00012  *  it under the terms of the GNU General Public License as published by
00013  *  the Free Software Foundation; either version 2 of the License, or
00014  *  (at your option) any later version. A runtime exception applies to
00015  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00016  *
00017  *  This program is distributed in the hope that it will be useful,
00018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  *  GNU Library General Public License for more details.
00021  *
00022  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00023  */
00024
00025 #include <interface/field_iterator.h>
00026
00027 #include <core/exceptions/software.h>
00028 #include <core/exceptions/system.h>
00029
00030 #include <cstdlib>
00031 #include <cstring>
00032 #include <cstdio>
00033
00034 namespace fawkes {
00035 
00036 /** @class InterfaceFieldIterator <interface/interface.h>
00037  * Interface field iterator.
00038  * This iterator is part of the BlackBoard introspection API. It can be used to
00039  * iterate over all available fields and values of an interface without actually
00040  * knowing the specific type of the interface.
00041  * @author Tim Niemueller
00042  */
00043
00044 
00045 /** Constructor.
00046  * Creates an invalid iterator.
00047  */
00048 InterfaceFieldIterator::InterfaceFieldIterator()
00049 {
00050   __infol = NULL;
00051   __value_string = NULL;
00052 }
00053
00054 
00055 /** Constructor.
00056  * This creates an iterator pointing to the given entry of the info list.
00057  * @param info_list pointer to info list entry to start from
00058  */
00059 InterfaceFieldIterator::InterfaceFieldIterator(const interface_fieldinfo_t *info_list)
00060 {
00061   __infol = info_list;
00062   __value_string = NULL;
00063 }
00064
00065 
00066 /** Copy constructor.
00067  * @param fit iterator to copy
00068  */
00069 InterfaceFieldIterator::InterfaceFieldIterator(const InterfaceFieldIterator &fit)
00070 {
00071   __infol = fit.__infol;
00072   if ( fit.__value_string ) {
00073     __value_string = strdup(fit.__value_string);
00074   } else {
00075     __value_string = NULL;
00076   }
00077 }
00078
00079 
00080 /** Destructor. */
00081 InterfaceFieldIterator::~InterfaceFieldIterator()
00082 {
00083   if ( __value_string )  free(__value_string);
00084 }
00085
00086 
00087 /** Prefix increment.
00088  * @return reference to this instance
00089  */
00090 InterfaceFieldIterator &
00091 InterfaceFieldIterator::operator++()
00092 {
00093   if ( __infol != NULL ) {
00094     __infol = __infol->next;
00095     if ( __value_string )  free(__value_string);
00096     __value_string = NULL;
00097   }
00098
00099   return *this;
00100 }
00101
00102 
00103 /** Postfix increment operator.
00104  * @param inc ignored
00105  * @return instance before advancing to the next shared memory segment
00106  */
00107 InterfaceFieldIterator
00108 InterfaceFieldIterator::operator++(int inc)
00109 {
00110   InterfaceFieldIterator rv(*this);
00111   ++(*this);
00112   return rv;
00113 }
00114
00115 
00116 /** Advance by i steps.
00117  * @param i number of (matching) segments to advance.
00118  * @return reference to this after advancing
00119  */
00120 InterfaceFieldIterator &
00121 InterfaceFieldIterator::operator+(unsigned int i)
00122 {
00123   for (unsigned int j = 0; j < i; ++j) {
00124     ++(*this);
00125   }
00126   return *this;
00127 }
00128
00129 
00130 /** Advance by i steps.
00131  * @param i number of (matching) segments to advance.
00132  * @return reference to this after advancing
00133  */
00134 InterfaceFieldIterator &
00135 InterfaceFieldIterator::operator+=(unsigned int i)
00136 {
00137   for (unsigned int j = 0; j < i; ++j) {
00138     ++(*this);
00139   }
00140   return *this;
00141 }
00142
00143 
00144 /** Check iterators for equality.
00145  * @param fi iterator to compare to
00146  * @return true if iterators point to the the same field, false otherwise
00147  */
00148 bool
00149 InterfaceFieldIterator::operator==(const InterfaceFieldIterator & fi) const
00150 {
00151   return (__infol == fi.__infol);
00152 }
00153
00154 
00155 /** Check iterators for inequality.
00156  * @param fi iterator to compare to
00157  * @return true if iteraters point to the different fields, false otherwise
00158  */
00159 bool
00160 InterfaceFieldIterator::operator!=(const InterfaceFieldIterator & fi) const
00161 {
00162   return ! (*this == fi);
00163 }
00164
00165 
00166 /** Get FieldHeader.
00167  * @return shared memory header
00168  */
00169 const void *
00170 InterfaceFieldIterator::operator*() const
00171 {
00172   if ( __infol == NULL ) {
00173     throw NullPointerException("Cannot get value of end element");
00174   } else {
00175     return __infol->value;
00176   }
00177 }
00178
00179 
00180 /** Make this instance point to the same segment as fi.
00181  * @param fi field iterator to compare
00182  * @return reference to this instance
00183  */
00184 InterfaceFieldIterator &
00185 InterfaceFieldIterator::operator=(const InterfaceFieldIterator & fi)
00186 {
00187   __infol = fi.__infol;
00188
00189   return *this;
00190 }
00191
00192 
00193 /** Get type of current field.
00194  * @return field type
00195  */
00196 interface_fieldtype_t
00197 InterfaceFieldIterator::get_type() const
00198 {
00199   if ( __infol == NULL ) {
00200     throw NullPointerException("Cannot get type of end element");
00201   } else {
00202     return __infol->type;
00203   }
00204 }
00205
00206 
00207 /** Get type of current field as string.
00208  * @return field type as string
00209  */
00210 const char *
00211 InterfaceFieldIterator::get_typename() const
00212 {
00213   if ( __infol == NULL ) {
00214     throw NullPointerException("Cannot get type of end element");
00215   } else {
00216     switch (__infol->type) {
00217     case IFT_BOOL:     return "bool";
00218     case IFT_INT:      return "int";
00219     case IFT_UINT:     return "unsigned int";
00220     case IFT_LONGINT:  return "long int";
00221     case IFT_LONGUINT: return "long unsigned int";
00222     case IFT_FLOAT:    return "float";
00223     case IFT_BYTE:     return "byte";
00224     case IFT_STRING:   return "string";
00225     default:           return "unknown";
00226     }
00227   }
00228 }
00229
00230 
00231 /** Get name of current field.
00232  * @return field name
00233  */
00234 const char *
00235 InterfaceFieldIterator::get_name() const
00236 {
00237   if ( __infol == NULL ) {
00238     throw NullPointerException("Cannot get name of end element");
00239   } else {
00240     return __infol->name;
00241   }
00242 }
00243
00244 
00245 /** Get value of current field.
00246  * @return field value
00247  */
00248 const void *
00249 InterfaceFieldIterator::get_value() const
00250 {
00251   if ( __infol == NULL ) {
00252     throw NullPointerException("Cannot get value of end element");
00253   } else {
00254     return __infol->value;
00255   }
00256 }
00257
00258 
00259 /** Get length of current field.
00260  * @return length of field
00261  */
00262 size_t
00263 InterfaceFieldIterator::get_length() const
00264 {
00265   if ( __infol == NULL ) {
00266     throw NullPointerException("Cannot get length of end element");
00267   } else {
00268     return __infol->length;
00269   }
00270 }
00271
00272 
00273 /** Get value of current field as string.
00274  * @return field value as string
00275  */
00276 const char *
00277 InterfaceFieldIterator::get_value_string()
00278 {
00279   if ( __infol == NULL ) {
00280     throw NullPointerException("Cannot get value of end element");
00281   } else {
00282     if ( __value_string == NULL ) {
00283       if ( __infol->length == 0 )  throw OutOfBoundsException("Field length out of bounds",
00284                                                               __infol->length, 1, (unsigned int)0xFFFFFFFF);
00285
00286       char *tmp1 = strdup("");
00287       char *tmp2;
00288
00289       if ( __infol->type != IFT_STRING ) {
00290         for (size_t i = 0; i < __infol->length; ++i) {
00291           int rv = 0;
00292           switch (__infol->type) {
00293           case IFT_BOOL:
00294             rv = asprintf(&tmp2, "%s%s", tmp1, (((bool *)__infol->value)[i]) ? "true" : "false");
00295             break;
00296           case IFT_INT:
00297             rv = asprintf(&tmp2, "%s%i", tmp1, ((int *)__infol->value)[i]);
00298             break;
00299           case IFT_UINT:
00300             rv = asprintf(&tmp2, "%s%u", tmp1, ((unsigned int *)__infol->value)[i]);
00301             break;
00302           case IFT_LONGINT:
00303             rv = asprintf(&tmp2, "%s%li", tmp1, ((long int *)__infol->value)[i]);
00304             break;
00305           case IFT_LONGUINT:
00306             rv = asprintf(&tmp2, "%s%lu", tmp1, ((long unsigned int *)__infol->value)[i]);
00307             break;
00308           case IFT_FLOAT:
00309             rv = asprintf(&tmp2, "%s%f", tmp1, ((float *)__infol->value)[i]);
00310             break;
00311           case IFT_BYTE:
00312             rv = asprintf(&tmp2, "%s%u", tmp1, ((unsigned char *)__infol->value)[i]);
00313             break;
00314           case IFT_STRING:
00315             // cannot happen, caught with surrounding if statement
00316             break;
00317           }
00318
00319           if ( rv == -1 ) {
00320             throw OutOfMemoryException("InterfaceFieldIterator::get_value_string(): asprintf() failed (1)");
00321           }
00322
00323           free(tmp1);
00324           tmp1 = tmp2;
00325           if ( (__infol->length > 1) && (i < __infol->length - 1) ) {
00326             if (asprintf(&tmp2, "%s, ", tmp1) == -1) {
00327               throw OutOfMemoryException("InterfaceFieldIterator::get_value_string(): asprintf() failed (2)");
00328             }
00329             free(tmp1);
00330             tmp1 = tmp2;
00331           }
00332         }
00333
00334         __value_string = tmp1;
00335       } else {
00336         // it's a string, or a small number
00337         if ( __infol->length > 1 ) {
00338           if (asprintf(&__value_string, "%s", (const char *)__infol->value) == -1) {
00339             throw OutOfMemoryException("InterfaceFieldIterator::get_value_string(): asprintf() failed (3)");
00340           }
00341         } else {
00342           if (asprintf(&__value_string, "%c", *((const char *)__infol->value)) == -1) {
00343             throw OutOfMemoryException("InterfaceFieldIterator::get_value_string(): asprintf() failed (4)");
00344           }
00345         }
00346       }
00347     }
00348     return __value_string;
00349   }
00350 }
00351
00352 
00353 /** Get value of current field as bool.
00354  * @return field value
00355  * @param index array index (only use if field is an array)
00356  * @exception NullPointerException invalid iterator, possibly end iterator
00357  * @exception TypeMismatchException thrown if field is not of type bool
00358  * @exception OutOfBoundsException thrown if index is out of bounds
00359  */
00360 bool
00361 InterfaceFieldIterator::get_bool(unsigned int index) const
00362 {
00363   if ( __infol == NULL ) {
00364     throw NullPointerException("Cannot get value of end element");
00365   } else if ( __infol->type != IFT_BOOL ) {
00366     throw TypeMismatchException("Requested value is not of type bool");
00367   } else if (index >= __infol->length) {
00368     throw OutOfBoundsException("Field index out of bounds", index, 0, __infol->length);
00369   } else {
00370     return ((bool *)__infol->value)[index];
00371   }
00372 }
00373
00374 
00375 /** Get value of current field as integer.
00376  * @return field value
00377  * @param index array index (only use if field is an array)
00378  * @exception NullPointerException invalid iterator, possibly end iterator
00379  * @exception TypeMismatchException thrown if field is not of type int
00380  * @exception OutOfBoundsException thrown if index is out of bounds
00381  */
00382 int
00383 InterfaceFieldIterator::get_int(unsigned int index) const
00384 {
00385   if ( __infol == NULL ) {
00386     throw NullPointerException("Cannot get value of end element");
00387   } else if ( __infol->type != IFT_INT ) {
00388     throw TypeMismatchException("Requested value is not of type int");
00389   } else if (index >= __infol->length) {
00390     throw OutOfBoundsException("Field index out of bounds", index, 0, __infol->length);
00391   } else {
00392     return ((int *)__infol->value)[index];
00393   }
00394 }
00395
00396 
00397 /** Get value of current field as unsigned integer.
00398  * @return field value
00399  * @param index array index (only use if field is an array)
00400  * @exception NullPointerException invalid iterator, possibly end iterator
00401  * @exception TypeMismatchException thrown if field is not of type unsigned int
00402  * @exception OutOfBoundsException thrown if index is out of bounds
00403  */
00404 unsigned int
00405 InterfaceFieldIterator::get_uint(unsigned int index) const
00406 {
00407   if ( __infol == NULL ) {
00408     throw NullPointerException("Cannot get value of end element");
00409   } else if ( __infol->type != IFT_UINT ) {
00410     throw TypeMismatchException("Requested value is not of type unsigned int");
00411   } else if (index >= __infol->length) {
00412     throw OutOfBoundsException("Field index out of bounds", index, 0, __infol->length);
00413   } else {
00414     return ((unsigned int *)__infol->value)[index];
00415   }
00416 }
00417
00418 
00419 /** Get value of current field as long integer.
00420  * @return field value
00421  * @param index array index (only use if field is an array)
00422  * @exception NullPointerException invalid iterator, possibly end iterator
00423  * @exception TypeMismatchException thrown if field is not of type long int
00424  * @exception OutOfBoundsException thrown if index is out of bounds
00425  */
00426 long int
00427 InterfaceFieldIterator::get_longint(unsigned int index) const
00428 {
00429   if ( __infol == NULL ) {
00430     throw NullPointerException("Cannot get value of end element");
00431   } else if ( __infol->type != IFT_LONGINT ) {
00432     throw TypeMismatchException("Requested value is not of type long int");
00433   } else if (index >= __infol->length) {
00434     throw OutOfBoundsException("Field index out of bounds", index, 0, __infol->length);
00435   } else {
00436     return ((long int *)__infol->value)[index];
00437   }
00438 }
00439
00440 
00441 /** Get value of current field as unsigned long int.
00442  * @return field value
00443  * @param index array index (only use if field is an array)
00444  * @exception NullPointerException invalid iterator, possibly end iterator
00445  * @exception TypeMismatchException thrown if field is not of type long unsigned int
00446  * @exception OutOfBoundsException thrown if index is out of bounds
00447  */
00448 unsigned long int
00449 InterfaceFieldIterator::get_longuint(unsigned int index) const
00450 {
00451   if ( __infol == NULL ) {
00452     throw NullPointerException("Cannot get value of end element");
00453   } else if ( __infol->type != IFT_LONGUINT ) {
00454     throw TypeMismatchException("Requested value is not of type unsigned long int");
00455   } else if (index >= __infol->length) {
00456     throw OutOfBoundsException("Field index out of bounds", index, 0, __infol->length);
00457   } else {
00458     return ((unsigned long int *)__infol->value)[index];
00459   }
00460 }
00461
00462 
00463 /** Get value of current field as float.
00464  * @return field value
00465  * @param index array index (only use if field is an array)
00466  * @exception NullPointerException invalid iterator, possibly end iterator
00467  * @exception TypeMismatchException thrown if field is not of type float
00468  * @exception OutOfBoundsException thrown if index is out of bounds
00469  */
00470 float
00471 InterfaceFieldIterator::get_float(unsigned int index) const
00472 {
00473   if ( __infol == NULL ) {
00474     throw NullPointerException("Cannot get value of end element");
00475   } else if ( __infol->type != IFT_FLOAT ) {
00476     throw TypeMismatchException("Requested value is not of type float");
00477   } else if (index >= __infol->length) {
00478     throw OutOfBoundsException("Field index out of bounds", index, 0, __infol->length);
00479   } else {
00480     return ((float *)__infol->value)[index];
00481   }
00482 }
00483
00484 
00485 /** Get value of current field as byte.
00486  * @return field value
00487  * @param index array index (only use if field is an array)
00488  * @exception NullPointerException invalid iterator, possibly end iterator
00489  * @exception TypeMismatchException thrown if field is not of type byte
00490  * @exception OutOfBoundsException thrown if index is out of bounds
00491  */
00492 unsigned char
00493 InterfaceFieldIterator::get_byte(unsigned int index) const
00494 {
00495   if ( __infol == NULL ) {
00496     throw NullPointerException("Cannot get value of end element");
00497   } else if ( __infol->type != IFT_BYTE ) {
00498     throw TypeMismatchException("Requested value is not of type float");
00499   } else if (index >= __infol->length) {
00500     throw OutOfBoundsException("Field index out of bounds", index, 0, __infol->length);
00501   } else {
00502     return ((unsigned char *)__infol->value)[index];
00503   }
00504 }
00505
00506 
00507 /** Get value of current field as bool array.
00508  * @return field value
00509  * @exception NullPointerException invalid iterator, possibly end iterator
00510  * @exception TypeMismatchException thrown if field is not of type bool or field
00511  * is not an array (length is 1)
00512  */
00513 bool *
00514 InterfaceFieldIterator::get_bools() const
00515 {
00516   if ( __infol == NULL ) {
00517     throw NullPointerException("Cannot get value of end element");
00518   } else if ( __infol->type != IFT_BOOL ) {
00519     throw TypeMismatchException("Requested value is not of type bool");
00520   } else if (__infol->length == 1) {
00521     throw TypeMismatchException("Field %s is not an array", __infol->name);
00522   } else {
00523     return (bool *)__infol->value;
00524   }
00525 }
00526
00527 
00528 /** Get value of current field as integer array.
00529  * @return field value
00530  * @exception NullPointerException invalid iterator, possibly end iterator
00531  * @exception TypeMismatchException thrown if field is not of type int or field
00532  * is not an array (length is 1)
00533  */
00534 int *
00535 InterfaceFieldIterator::get_ints() const
00536 {
00537   if ( __infol == NULL ) {
00538     throw NullPointerException("Cannot get value of end element");
00539   } else if ( __infol->type != IFT_INT ) {
00540     throw TypeMismatchException("Requested value is not of type int");
00541   } else {
00542     return (int *)__infol->value;
00543   }
00544 }
00545
00546 
00547 /** Get value of current field as unsigned integer array.
00548  * @return field value
00549  * @exception NullPointerException invalid iterator, possibly end iterator
00550  * @exception TypeMismatchException thrown if field is not of type unsigned int
00551  * or field is not an array (length is 1)
00552  */
00553 unsigned int *
00554 InterfaceFieldIterator::get_uints() const
00555 {
00556   if ( __infol == NULL ) {
00557     throw NullPointerException("Cannot get value of end element");
00558   } else if ( __infol->type != IFT_UINT ) {
00559     throw TypeMismatchException("Requested value is not of type unsigned int");
00560   } else {
00561     return (unsigned int *)__infol->value;
00562   }
00563 }
00564
00565 
00566 /** Get value of current field as long integer array.
00567  * @return field value
00568  * @exception NullPointerException invalid iterator, possibly end iterator
00569  * @exception TypeMismatchException thrown if field is not of type long int
00570  * or field is not an array (length is 1)
00571  */
00572 long int *
00573 InterfaceFieldIterator::get_longints() const
00574 {
00575   if ( __infol == NULL ) {
00576     throw NullPointerException("Cannot get value of end element");
00577   } else if ( __infol->type != IFT_LONGINT ) {
00578     throw TypeMismatchException("Requested value is not of type long int");
00579   } else {
00580     return (long int *)__infol->value;
00581   }
00582 }
00583
00584 
00585 /** Get value of current field as unsigned long int array.
00586  * @return field value
00587  * @exception NullPointerException invalid iterator, possibly end iterator
00588  * @exception TypeMismatchException thrown if field is not of type long unsigned
00589  * int or field is not an array (length is 1)
00590  */
00591 unsigned long int *
00592 InterfaceFieldIterator::get_longuints() const
00593 {
00594   if ( __infol == NULL ) {
00595     throw NullPointerException("Cannot get value of end element");
00596   } else if ( __infol->type != IFT_LONGUINT ) {
00597     throw TypeMismatchException("Requested value is not of type unsigned long int");
00598   } else {
00599     return (unsigned long int *)__infol->value;
00600   }
00601 }
00602
00603 
00604 /** Get value of current field as float array.
00605  * @return field value
00606  * @exception NullPointerException invalid iterator, possibly end iterator
00607  * @exception TypeMismatchException thrown if field is not of type float or field
00608  * is not an array (length is 1)
00609  */
00610 float *
00611 InterfaceFieldIterator::get_floats() const
00612 {
00613   if ( __infol == NULL ) {
00614     throw NullPointerException("Cannot get value of end element");
00615   } else if ( __infol->type != IFT_FLOAT ) {
00616     throw TypeMismatchException("Requested value is not of type float");
00617   } else {
00618     return (float *)__infol->value;
00619   }
00620 }
00621
00622 
00623 /** Get value of current field as byte array.
00624  * @return field value
00625  * @exception NullPointerException invalid iterator, possibly end iterator
00626  * @exception TypeMismatchException thrown if field is not of type byte or field
00627  * is not an array (length is 1)
00628  */
00629 unsigned char *
00630 InterfaceFieldIterator::get_bytes() const
00631 {
00632   if ( __infol == NULL ) {
00633     throw NullPointerException("Cannot get value of end element");
00634   } else if ( __infol->type != IFT_BYTE ) {
00635     throw TypeMismatchException("Requested value is not of type float");
00636   } else {
00637     return (unsigned char *)__infol->value;
00638   }
00639 }
00640
00641 
00642 /** Get value of current field as string.
00643  * @return field value
00644  * @exception NullPointerException invalid iterator, possibly end iterator
00645  * @exception TypeMismatchException thrown if field is not of type string
00646  */
00647 const char *
00648 InterfaceFieldIterator::get_string() const
00649 {
00650   if ( __infol == NULL ) {
00651     throw NullPointerException("Cannot get value of end element");
00652   } else if ( __infol->type != IFT_STRING ) {
00653     throw TypeMismatchException("Requested value is not of type string");
00654   } else {
00655     return (const char *)__infol->value;
00656   }
00657 }
00658
00659 
00660 /** Set value of current field as bool.
00661  * @param v the new value
00662  * @param index array index (only use if field is an array)
00663  * @exception NullPointerException invalid iterator, possibly end iterator
00664  * @exception TypeMismatchException thrown if field is not of type bool
00665  * @exception OutOfBoundsException thrown if index is out of bounds
00666  */
00667 void
00668 InterfaceFieldIterator::set_bool(bool v, unsigned int index)
00669 {
00670   if ( __infol == NULL ) {
00671     throw NullPointerException("Cannot set value of end element");
00672   } else if ( __infol->type != IFT_BOOL ) {
00673     throw TypeMismatchException("Field to be written is not of type bool");
00674   } else if (index >= __infol->length) {
00675     throw OutOfBoundsException("Field index out of bounds", index, 0, __infol->length);
00676   } else {
00677     char* dst = (char *) __infol->value + index * sizeof(bool);
00678     memcpy((void *) dst, &v, sizeof(bool));
00679   }
00680 }
00681
00682 
00683 /** Set value of current field as integer.
00684  * @param v the new value
00685  * @param index array index (only use if field is an array)
00686  * @exception NullPointerException invalid iterator, possibly end iterator
00687  * @exception TypeMismatchException thrown if field is not of type int
00688  * @exception OutOfBoundsException thrown if index is out of bounds
00689  */
00690 void
00691 InterfaceFieldIterator::set_int(int v, unsigned int index)
00692 {
00693   if ( __infol == NULL ) {
00694     throw NullPointerException("Cannot set value of end element");
00695   } else if ( __infol->type != IFT_INT ) {
00696     throw TypeMismatchException("Field to be written is not of type int");
00697   } else if (index >= __infol->length) {
00698     throw OutOfBoundsException("Field index out of bounds", index, 0, __infol->length);
00699   } else {
00700     char* dst = (char *) __infol->value + index * sizeof(int);
00701     memcpy((void *) dst, &v, sizeof(int));
00702   }
00703 }
00704
00705 
00706 /** Set value of current field as unsigned integer.
00707  * @param v the new value
00708  * @param index array index (only use if field is an array)
00709  * @exception NullPointerException invalid iterator, possibly end iterator
00710  * @exception TypeMismatchException thrown if field is not of type unsigned int
00711  * @exception OutOfBoundsException thrown if index is out of bounds
00712  */
00713 void
00714 InterfaceFieldIterator::set_uint(unsigned int v, unsigned int index)
00715 {
00716   if ( __infol == NULL ) {
00717     throw NullPointerException("Cannot set value of end element");
00718   } else if ( __infol->type != IFT_UINT ) {
00719     throw TypeMismatchException("Field to be written is not of type unsigned int");
00720   } else if (index >= __infol->length) {
00721     throw OutOfBoundsException("Field index out of bounds", index, 0, __infol->length);
00722   } else {
00723     char* dst = (char *) __infol->value + index * sizeof(unsigned int);
00724     memcpy((void *) dst, &v, sizeof(unsigned int));
00725   }
00726 }
00727
00728 
00729 /** Set value of current field as long integer.
00730  * @param v the new value
00731  * @param index array index (only use if field is an array)
00732  * @exception NullPointerException invalid iterator, possibly end iterator
00733  * @exception TypeMismatchException thrown if field is not of type long int
00734  * @exception OutOfBoundsException thrown if index is out of bounds
00735  */
00736 void
00737 InterfaceFieldIterator::set_longint(long int v, unsigned int index)
00738 {
00739   if ( __infol == NULL ) {
00740     throw NullPointerException("Cannot set value of end element");
00741   } else if ( __infol->type != IFT_LONGINT ) {
00742     throw TypeMismatchException("Field to be written is not of type long int");
00743   } else if (index >= __infol->length) {
00744     throw OutOfBoundsException("Field index out of bounds", index, 0, __infol->length);
00745   } else {
00746     char* dst = (char *) __infol->value + index * sizeof(long int);
00747     memcpy((void *) dst, &v, sizeof(long int));
00748   }
00749 }
00750
00751 
00752 /** Set value of current field as unsigned long integer.
00753  * @param v the new value
00754  * @param index array index (only use if field is an array)
00755  * @exception NullPointerException invalid iterator, possibly end iterator
00756  * @exception TypeMismatchException thrown if field is not of type long unsigned int
00757  * @exception OutOfBoundsException thrown if index is out of bounds
00758  */
00759 void
00760 InterfaceFieldIterator::set_longuint(long unsigned int v, unsigned int index)
00761 {
00762   if ( __infol == NULL ) {
00763     throw NullPointerException("Cannot set value of end element");
00764   } else if ( __infol->type != IFT_LONGUINT ) {
00765     throw TypeMismatchException("Field to be written is not of type long unsigned int");
00766   } else if (index >= __infol->length) {
00767     throw OutOfBoundsException("Field index out of bounds", index, 0, __infol->length);
00768   } else {
00769     char* dst = (char *) __infol->value + index * sizeof(long unsigned int);
00770     memcpy((void *) dst, &v, sizeof(long unsigned int));
00771   }
00772 }
00773
00774 
00775 /** Set value of current field as float.
00776  * @param v the new value
00777  * @param index array index (only use if field is an array)
00778  * @exception NullPointerException invalid iterator, possibly end iterator
00779  * @exception TypeMismatchException thrown if field is not of type float
00780  * @exception OutOfBoundsException thrown if index is out of bounds
00781  */
00782 void
00783 InterfaceFieldIterator::set_float(float v, unsigned int index)
00784 {
00785   if ( __infol == NULL ) {
00786     throw NullPointerException("Cannot set value of end element");
00787   } else if ( __infol->type != IFT_FLOAT ) {
00788     throw TypeMismatchException("Field to be written is not of type float");
00789   } else if (index >= __infol->length) {
00790     throw OutOfBoundsException("Field index out of bounds", index, 0, __infol->length);
00791   } else {
00792     char* dst = (char *) __infol->value + index * sizeof(float);
00793     memcpy((void *) dst, &v, sizeof(float));
00794   }
00795 }
00796
00797 
00798 /** Set value of current field as byte.
00799  * @param v the new value
00800  * @param index array index (only use if field is an array)
00801  * @exception NullPointerException invalid iterator, possibly end iterator
00802  * @exception TypeMismatchException thrown if field is not of type byte
00803  * @exception OutOfBoundsException thrown if index is out of bounds
00804  */
00805 void
00806 InterfaceFieldIterator::set_byte(unsigned char v, unsigned int index)
00807 {
00808   if ( __infol == NULL ) {
00809     throw NullPointerException("Cannot set value of end element");
00810   } else if ( __infol->type != IFT_BYTE ) {
00811     throw TypeMismatchException("Field to be written is not of type byte");
00812   } else if (index >= __infol->length) {
00813     throw OutOfBoundsException("Field index out of bounds", index, 0, __infol->length);
00814   } else {
00815     char* dst = (char *) __infol->value + index * sizeof(unsigned char);
00816     memcpy((void *) dst, &v, sizeof(unsigned char ));
00817   }
00818 }
00819
00820 
00821 /** Set value of current field as bool array.
00822  * @param v an array of bools
00823  * @exception NullPointerException invalid iterator, possibly end iterator
00824  * @exception TypeMismatchException thrown if field is not of type bool or field
00825  * is not an array (length is 1)
00826  */
00827 void
00828 InterfaceFieldIterator::set_bools(bool *v)
00829 {
00830   if ( __infol == NULL ) {
00831     throw NullPointerException("Cannot set value of end element");
00832   } else if ( __infol->type != IFT_BOOL ) {
00833     throw TypeMismatchException("Field to be written is not of type bool");
00834   } else if (__infol->length == 1) {
00835     throw TypeMismatchException("Field %s is not an array", __infol->name);
00836   } else {
00837     memcpy(__infol->value, v, __infol->length * sizeof(bool));
00838   }
00839 }
00840
00841 
00842 /** Set value of current field as integer array.
00843  * @param v an array of ints
00844  * @exception NullPointerException invalid iterator, possibly end iterator
00845  * @exception TypeMismatchException thrown if field is not of type int or field
00846  * is not an array (length is 1)
00847  */
00848 void
00849 InterfaceFieldIterator::set_ints(int *v)
00850 {
00851   if ( __infol == NULL ) {
00852     throw NullPointerException("Cannot set value of end element");
00853   } else if ( __infol->type != IFT_INT ) {
00854     throw TypeMismatchException("Field to be written is not of type int");
00855   } else if (__infol->length == 1) {
00856     throw TypeMismatchException("Field %s is not an array", __infol->name);
00857   } else {
00858     memcpy(__infol->value, v, __infol->length * sizeof(int));
00859   }
00860 }
00861
00862 
00863 /** Set value of current field as unsigned integer array.
00864  * @param v an array of unsigned ints
00865  * @exception NullPointerException invalid iterator, possibly end iterator
00866  * @exception TypeMismatchException thrown if field is not of type unsigned int or field
00867  * is not an array (length is 1)
00868  */
00869 void
00870 InterfaceFieldIterator::set_uints(unsigned int *v)
00871 {
00872   if ( __infol == NULL ) {
00873     throw NullPointerException("Cannot set value of end element");
00874   } else if ( __infol->type != IFT_UINT ) {
00875     throw TypeMismatchException("Field to be written is not of type unsigned int");
00876   } else if (__infol->length == 1) {
00877     throw TypeMismatchException("Field %s is not an array", __infol->name);
00878   } else {
00879     memcpy(__infol->value, v, __infol->length * sizeof(unsigned int));
00880   }
00881 }
00882
00883 
00884 /** Set value of current field as long integer array.
00885  * @param v an array of long ints
00886  * @exception NullPointerException invalid iterator, possibly end iterator
00887  * @exception TypeMismatchException thrown if field is not of type long int or field
00888  * is not an array (length is 1)
00889  */
00890 void
00891 InterfaceFieldIterator::set_longints(long int *v)
00892 {
00893   if ( __infol == NULL ) {
00894     throw NullPointerException("Cannot set value of end element");
00895   } else if ( __infol->type != IFT_LONGINT ) {
00896     throw TypeMismatchException("Field to be written is not of type long int");
00897   } else if (__infol->length == 1) {
00898     throw TypeMismatchException("Field %s is not an array", __infol->name);
00899   } else {
00900     memcpy(__infol->value, v, __infol->length * sizeof(long int));
00901   }
00902 }
00903
00904 
00905 /** Set value of current field as unsigned long integer array.
00906  * @param v an array of unsigned long ints
00907  * @exception NullPointerException invalid iterator, possibly end iterator
00908  * @exception TypeMismatchException thrown if field is not of type long unsigned int or field
00909  * is not an array (length is 1)
00910  */
00911 void
00912 InterfaceFieldIterator::set_longuints(long unsigned int *v)
00913 {
00914   if ( __infol == NULL ) {
00915     throw NullPointerException("Cannot set value of end element");
00916   } else if ( __infol->type != IFT_LONGUINT ) {
00917     throw TypeMismatchException("Field to be written is not of type long unsigned int");
00918   } else if (__infol->length == 1) {
00919     throw TypeMismatchException("Field %s is not an array", __infol->name);
00920   } else {
00921     memcpy(__infol->value, v, __infol->length * sizeof(long unsigned int));
00922   }
00923 }
00924
00925 
00926 /** Set value of current field as float array.
00927  * @param v an array of floats
00928  * @exception NullPointerException invalid iterator, possibly end iterator
00929  * @exception TypeMismatchException thrown if field is not of type float or field
00930  * is not an array (length is 1)
00931  */
00932 void
00933 InterfaceFieldIterator::set_floats(float *v)
00934 {
00935   if ( __infol == NULL ) {
00936     throw NullPointerException("Cannot set value of end element");
00937   } else if ( __infol->type != IFT_FLOAT ) {
00938     throw TypeMismatchException("Field to be written is not of type float");
00939   } else if (__infol->length == 1) {
00940     throw TypeMismatchException("Field %s is not an array", __infol->name);
00941   } else {
00942     memcpy(__infol->value, v, __infol->length * sizeof(float));
00943   }
00944 }
00945
00946 
00947 /** Set value of current field as byte array.
00948  * @param v an array of bytes
00949  * @exception NullPointerException invalid iterator, possibly end iterator
00950  * @exception TypeMismatchException thrown if field is not of type byte or field
00951  * is not an array (length is 1)
00952  */
00953 void
00954 InterfaceFieldIterator::set_bytes(unsigned char *v)
00955 {
00956   if ( __infol == NULL ) {
00957     throw NullPointerException("Cannot set value of end element");
00958   } else if ( __infol->type != IFT_BYTE ) {
00959     throw TypeMismatchException("Field to be written is not of type byte");
00960   } else if (__infol->length == 1) {
00961     throw TypeMismatchException("Field %s is not an array", __infol->name);
00962   } else {
00963     memcpy(__infol->value, v, __infol->length * sizeof(unsigned char));
00964   }
00965 }
00966
00967 
00968 /** Set value of current field as string.
00969  * @param v a string
00970  * @exception NullPointerException invalid iterator, possibly end iterator
00971  * @exception TypeMismatchException thrown if field is not of type string
00972  */
00973 void
00974 InterfaceFieldIterator::set_string(const char *v)
00975 {
00976   if ( __infol == NULL ) {
00977     throw NullPointerException("Cannot set value of end element");
00978   } else if ( __infol->type != IFT_STRING ) {
00979     throw TypeMismatchException("Field to be written is not of type string");
00980   } else {
00981     strncpy((char *) __infol->value, v, __infol->length);
00982   }
00983 }
00984
00985 } // end namespace fawkes