ValueList.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 $Header$
00004 
00005 */
00006 
00007 #include <typeinfo>
00008 
00009 #include "Error.h"
00010 #include "Value.h"
00011 
00012 #include "ValueList.h"
00013 
00014 ValueList::ValueList() : vector<ValueBase *>()
00015 {
00016 }
00017 
00018 void ValueList::clean()
00019 {
00020   unsigned int i, s = size();
00021   ValueBase *p;
00022 
00023   for (i = 0; i < s; ++i)
00024     if ((p = at(i)))
00025       delete p;
00026   clear();
00027 }
00028 
00029 int ValueList::printf(const char *format, ...)
00030 {
00031   va_list ap;
00032   unsigned int i;
00033   unsigned int formatLen = strlen(format);
00034   ValueBase *value;
00035   int res;
00036 
00037   if (formatLen == 0)
00038     return ERROR(MSG_VALUE_LIST_WRONG_FORMAT, format);
00039 
00040   va_start(ap, format);
00041   for (i = 0; i < formatLen; ++i) {
00042     switch (ValueType(tolower(format[i]))) {
00043       case VALUE_TYPE_INT:
00044         value = new ValueInt(va_arg(ap, int));
00045         break;
00046       case VALUE_TYPE_UINT:
00047         value = new ValueUint(va_arg(ap, unsigned int));
00048         break;
00049       case VALUE_TYPE_CHAR:
00050         value = new ValueChar(va_arg(ap, int));
00051         break;
00052       case VALUE_TYPE_STRING:
00053         value = new ValueString(va_arg(ap, char *));
00054         break;
00055       case VALUE_TYPE_DOUBLE:
00056         value = new ValueDouble(va_arg(ap, double));
00057         break;
00058       case VALUE_TYPE_POINTER:
00059         value = new ValuePointer(va_arg(ap, void *));
00060         break;
00061       case VALUE_TYPE_OBJECT:
00062         value = new ValueObject(va_arg(ap, Object *));
00063         break;
00064       default:
00065         clean();
00066         va_end(ap);
00067         return ERROR(MSG_VALUE_LIST_WRONG_FORMAT, format);
00068         break;
00069     }
00070     if (!value) {
00071       clean();
00072       va_end(ap);
00073       return ERROR(MSG_OBJECT_CANNOT_CREATE, "Value<>");
00074     }
00075     if (FAILED(res = value->failureCode())) {
00076       delete value;
00077       clean();
00078       va_end(ap);
00079 
00080       return ERROR_BACKTRACE(res);
00081     }
00082     push_back(value);
00083   }
00084   va_end(ap);
00085   return OK;
00086 }
00087 
00088 int ValueList::scanf(const char *format, ...)
00089 {
00090   va_list ap;
00091   unsigned int i;
00092   unsigned int sz = size();
00093   ValueBase *value;
00094 
00095   if (sz == 0 || sz != strlen(format))
00096     return ERROR(MSG_VALUE_LIST_WRONG_FORMAT, format);
00097 
00098   va_start(ap, format);
00099   for (i = 0; i < sz; ++i) {
00100     value = at(i);
00101     switch (format[i]) {
00102       case VALUE_TYPE_INT:
00103       {
00104         ValueInt *v;
00105         if (!(v = queryInterface<ValueInt>(value))) {
00106           va_end(ap);
00107           return ERROR(MSG_VALUE_LIST_UNEXPECTED, "integer", typeid(value).name());
00108         }
00109         int *p = va_arg(ap, int *);
00110         *p = v->get();
00111         break;
00112       }
00113       case VALUE_TYPE_CHAR:
00114       {
00115         ValueChar *v;
00116         if (!(v = queryInterface<ValueChar>(value))) {
00117           va_end(ap);
00118           return ERROR(MSG_VALUE_LIST_UNEXPECTED, "char", typeid(value).name());
00119         }
00120         char *p = (char *)va_arg(ap, int *);
00121         *p = v->get();
00122         break;
00123       }
00124       case VALUE_TYPE_STRING:
00125       {
00126         ValueString *v;
00127         if (!(v = queryInterface<ValueString>(value))) {
00128           va_end(ap);
00129           return ERROR(MSG_VALUE_LIST_UNEXPECTED, "String", typeid(value).name());
00130         }
00131         String *p = va_arg(ap, String *);
00132         *p = v->get();
00133         break;
00134       }
00135       case VALUE_TYPE_DOUBLE:
00136       {
00137         ValueDouble *v;
00138         if (!(v = queryInterface<ValueDouble>(value))) {
00139           va_end(ap);
00140           return ERROR(MSG_VALUE_LIST_UNEXPECTED, "double", typeid(value).name());
00141         }
00142         double *p = va_arg(ap, double *);
00143         *p = v->get();
00144         break;
00145       }
00146       case VALUE_TYPE_POINTER:
00147       {
00148         ValuePointer *v;
00149         if (!(v = queryInterface<ValuePointer>(value))) {
00150           va_end(ap);
00151           return ERROR(MSG_VALUE_LIST_UNEXPECTED, "pointer", typeid(value).name());
00152         }
00153         void **p = va_arg(ap, void **);
00154         *p = v->get();
00155         break;
00156       }
00157       default:
00158         va_end(ap);
00159         return ERROR(MSG_VALUE_LIST_WRONG_FORMAT, format);
00160         break;
00161     }
00162   }
00163   va_end(ap);
00164   return OK;
00165 }

Generated on Thu Sep 6 20:11:25 2007 for Pylon Application Platform by  doxygen 1.5.1