S11nVector.h

Go to the documentation of this file.
00001 #ifndef __VALUEVECTOR_H__
00002 #define __VALUEVECTOR_H__
00003 
00004 /*
00005 
00006 $Header$
00007 
00008 */
00009 
00010 #include <vector>
00011 
00012 #include "Error.h"
00013 #include "Parser.h"
00014 #include "S11n.h"
00015 
00016 static const char *const VALUE_VECTOR_DELIMITERS = "|";
00017 
00018 static const char *const XML_TAG_ITEM = "item";
00019 
00020 using namespace std;
00021 
00022 template<class T>
00023 class S11n<vector<T> >
00024 {
00025 
00026 public:
00027 
00028   static int toString(const vector<T> &value, String &str);
00029   static int fromString(const String &str, vector<T> &value);
00030 
00031   static int toXml(const vector<T> &value, XmlElement *element);
00032   static int fromXml(const XmlElement *element, vector<T> &value);
00033 
00034 };
00035 
00036 // Implementation.
00037 
00038 template<class T>
00039 int S11n<vector<T> >::toString(const vector<T> &value, String &str)
00040 {
00041   unsigned int i, size = value.size();
00042   String s;
00043   int res;
00044 
00045   str.clear();
00046 
00047   for (i = 0; i < size; ++i) {
00048     if (FAILED(res = S11n<T>::toString(value[i], s)))
00049       return ERROR_BACKTRACE(res);
00050     str += s;
00051     if (i < size - 1)
00052       str += VALUE_VECTOR_DELIMITERS;
00053   }
00054 
00055   return OK;
00056 }
00057 
00058 template<class T>
00059 int S11n<vector<T> >::fromString(const String &str, vector<T> &value)
00060 {
00061   StringList list;
00062   T v;
00063   int res;
00064 
00065   value.clear();
00066 
00067   if (FAILED(res = Parser::split(str, list, 0, VALUE_VECTOR_DELIMITERS)))
00068     return ERROR_BACKTRACE(res);
00069 
00070   unsigned int i, size = list.size();
00071 
00072   for (i = 0; i < size; ++i) {
00073     if (FAILED(res = S11n<T>::fromString(list[i], v)))
00074       return ERROR_BACKTRACE(res);
00075     value.push_back(v);
00076   }
00077   return OK;
00078 }
00079 
00080 template<class T>
00081 int S11n<vector<T> >::toXml(const vector<T> &value, XmlElement *element)
00082 {
00083   CHECK_POINTER(element);
00084 
00085   int res;
00086 
00087   if (FAILED(res = element->removeChildren()))
00088     return ERROR_BACKTRACE(res);
00089 
00090   XmlElement *child;
00091   unsigned int i, size = value.size();
00092 
00093   for (i = 0; i < size; ++i) {
00094     child = new XmlElement(XML_TAG_ITEM);
00095     CHECK_CREATION(child, "XmlElement");
00096     if (FAILED(res = element->appendChild(child)))
00097       return ERROR_BACKTRACE(res);
00098     if (FAILED(res = S11n<T>::toXml(value[i], child)))
00099       return ERROR_BACKTRACE(res);
00100   }
00101   return OK;
00102 }
00103 
00104 template<class T>
00105 int S11n<vector<T> >::fromXml(const XmlElement *element, vector<T> &value)
00106 {
00107   CHECK_POINTER(element);
00108 
00109   XmlElementCollection *children = element->children();
00110   unsigned int i, size = children->size();
00111   T v;
00112   int res;
00113 
00114   value.clear();
00115   for (i = 0; i < size; ++i) {
00116     if (FAILED(res = S11n<T>::fromXml(children->at(i), v)))
00117       return ERROR_BACKTRACE(res);
00118     value.push_back(v);
00119   }
00120   return OK;
00121 }
00122 
00123 #endif

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