DataFactory.cpp
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
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "config.h"
00031
00032 #include <sstream>
00033 #include <boost/pointer_cast.hpp>
00034
00035 #include "DataFactory.hpp"
00036
00037 #include <XmlAttribute.hpp>
00038 #include <XmlAttributeVector.hpp>
00039 #include <XmlElement.hpp>
00040 #include <XmlElementVector.hpp>
00041 #include <XmlDocument.hpp>
00042
00043 #include "DataArray.hpp"
00044 #include "DataType.hpp"
00045
00046 #include <Exception.hpp>
00047
00048 #include <iostream>
00049 #include <sstream>
00050
00051 using namespace mermaid::support::data;
00052
00053 using mermaid::support::errorhandling::Exception;
00054 using boost::shared_ptr;
00055 using boost::static_pointer_cast;
00056
00057 using mermaid::support::xml::XmlAttribute;
00058 using mermaid::support::xml::XmlAttributeVector;
00059 using mermaid::support::xml::XmlDocument;
00060 using mermaid::support::xml::XmlElement;
00061 using mermaid::support::xml::XmlElementVector;
00062
00063 using yarp::os::Value;
00064
00065 std::map<std::string, shared_ptr<DataStructureDescription> > DataFactory::dataStructureDescriptionMap = std::map<std::string, shared_ptr<DataStructureDescription> >();
00066
00067 shared_ptr<Integer> DataFactory::buildInteger (int value)
00068 {
00069 return shared_ptr<Integer> (new Integer (value));
00070 };
00071
00072 shared_ptr<IntegerArray> DataFactory::buildDynamicIntegerArray()
00073 {
00074 return shared_ptr<IntegerArray> (new IntegerArray());
00075 };
00076
00077 shared_ptr<IntegerArray> DataFactory::buildStaticIntegerArray (int size)
00078 {
00079 return shared_ptr<IntegerArray> (new IntegerArray (size));
00080 };
00081
00082 shared_ptr<Double> DataFactory::buildDouble (double value)
00083 {
00084 return shared_ptr<Double> (new Double (value));
00085 };
00086
00087 shared_ptr<DoubleArray> DataFactory::buildDynamicDoubleArray()
00088 {
00089 return shared_ptr<DoubleArray> (new DoubleArray());
00090 };
00091
00092 shared_ptr<DoubleArray> DataFactory::buildStaticDoubleArray (int size)
00093 {
00094 return shared_ptr<DoubleArray> (new DoubleArray (size));
00095 };
00096
00097 shared_ptr<String> DataFactory::buildString (std::string value)
00098 {
00099 return shared_ptr<String> (new String (value));
00100 };
00101
00102 shared_ptr<StringArray> DataFactory::buildDynamicStringArray()
00103 {
00104 return shared_ptr<StringArray> (new StringArray());
00105 };
00106
00107 shared_ptr<StringArray> DataFactory::buildStaticStringArray (int size)
00108 {
00109 return shared_ptr<StringArray> (new StringArray (size));
00110 };
00111
00112
00113 shared_ptr<DataValueVector> DataFactory::buildDataValueVector()
00114 {
00115
00116 DataValueVector * v = new DataValueVector();
00117 return shared_ptr<DataValueVector> (v);
00118 };
00119
00120 shared_ptr<DataValueVector> DataFactory::buildDataValueVector (Bottle& b)
00121 {
00122 shared_ptr<DataValueVector> v (new DataValueVector());
00123
00124 for (int i = 0; i < (int) b.size(); i++) {
00125
00126
00127
00128 Value& element = b.get (i);
00129
00130 if (element.isInt()) {
00131 shared_ptr<DataValue> i = DataFactory::buildInteger (element.asInt());
00132 v->pushBack (i);
00133
00134 }
00135 else if (element.isDouble()) {
00136 shared_ptr<DataValue> f = DataFactory::buildDouble (element.asDouble());
00137 v->pushBack (f);
00138
00139 }
00140 else if (element.isString()) {
00141 shared_ptr<DataValue> s = DataFactory::buildString (std::string (element.asString().c_str()));
00142 v->pushBack (s);
00143
00144 }
00145 else if (element.isList()) {
00146 throw Exception ("DataFactory::buildDataValueVector : unable to read list");
00147
00148
00149
00150 }
00151 else {
00152 throw Exception ("DataFactory::buildDataValueVector : unrecognized type");
00153 }
00154 }
00155
00156 return v;
00157 };
00158
00159
00160 void DataFactory::loadDataDescription (shared_ptr<XmlDocument> document)
00161 {
00162 std::cerr << "DataFactory::loadDataDescription" << std::endl;
00163
00164 shared_ptr<XmlElement> rootElement = document->getRootElement();
00165 XmlElementVector dataElements = rootElement->getChildrenElements();
00166
00167 XmlElementVector::iterator it;
00168
00169 for (it = dataElements.begin(); it != dataElements.end(); it++) {
00170 shared_ptr<XmlElement> e = *it;
00171
00172 shared_ptr<DataStructureDescription> d (new DataStructureDescription (e));
00173 std::string dataStructureName = d->getName();
00174 dataStructureDescriptionMap[dataStructureName] = d;
00175 std::cerr << "DataFactory::loadDataDescription : loaded: " << dataStructureName << std::endl;
00176 };
00177 };
00178
00179 shared_ptr<DataBox> DataFactory::buildDataBox (std::string typeName)
00180 {
00181
00182
00183
00184
00185
00186
00187 shared_ptr<DataStructureDescription> dsd = dataStructureDescriptionMap[typeName];
00188
00189 if (dsd == false) {
00190 throw Exception ("DataFactory::buildDataBoxFromStructure : invalid type name: " + typeName);
00191 }
00192
00193 shared_ptr<DataBox> dbox (new DataBox (dsd));
00194
00195 return dbox;
00196 };
00197
00198 shared_ptr<DataBox> DataFactory::buildDataBoxFromStructureAndBottle (std::string structureTypeName, Bottle &bottle)
00199 {
00200
00201 shared_ptr<DataValueVector> valueVector = buildDataValueVector (bottle);
00202 return buildDataBoxFromStructureAndValueVector (structureTypeName, valueVector);
00203
00204 };
00205
00206
00207
00208 shared_ptr<DataBox> DataFactory::buildDataBoxFromStructureAndValueVector (std::string structureTypeName, shared_ptr<DataValueVector> vv)
00209 {
00210
00211
00212 shared_ptr<DataBox> dbox = buildDataBox (structureTypeName);
00213 dbox->setDataFromValueVector (vv);
00214 return dbox;
00215
00216 };
00217
00218 shared_ptr<DataBoxArray> DataFactory::buildDynamicDataBoxArray (std::string typeName)
00219 {
00220 return shared_ptr<DataBoxArray> (new DataBoxArray (typeName));
00221 };
00222
00223 shared_ptr<DataBoxArray> DataFactory::buildStaticDataBoxArray (std::string typeName, int size)
00224 {
00225 return shared_ptr<DataBoxArray> (new DataBoxArray (typeName, size));
00226 };
00227
00228
00229 void DataFactory::writeDataBoxToBottle (shared_ptr<DataBox> box, Bottle &bottle)
00230 {
00231
00232
00233 shared_ptr<DataValueVector> vv = box->getDataValueVector();
00234
00235 int vectorSize = vv->getSize();
00236
00237 for (int i = 0; i < vectorSize; i++) {
00238 shared_ptr<DataValue> d = vv->getValue (i);
00239 DataType t = d->getType();
00240
00241 if (t == INTEGER) {
00242 Integer * i = (Integer*) d.get();
00243 bottle.addInt (i->getValue());
00244 }
00245 else if (t == DOUBLE) {
00246 Double * f = (Double*) d.get();
00247 bottle.addDouble (f->getValue());
00248 }
00249 else if (t == STRING) {
00250 String * s = (String*) d.get();
00251 bottle.addString (s->getValue().c_str());
00252 }
00253 else if (t == INTEGERARRAY) {
00254 IntegerArray * a = (IntegerArray*) d.get();
00255 int size = a->getSize();
00256
00257 if (a->isDynamic() == true) {
00258 bottle.addInt (size);
00259 }
00260
00261 for (int i = 0; i < size; i++) {
00262 int integer = (*a) [i]->getValue();
00263 bottle.addInt (integer);
00264 }
00265 }
00266 else if (t == DOUBLEARRAY) {
00267 DoubleArray * a = (DoubleArray*) d.get();
00268 int size = a->getSize();
00269
00270 if (a->isDynamic() == true) {
00271 bottle.addInt (size);
00272 }
00273
00274 for (int i = 0; i < size; i++) {
00275 double d = (*a) [i]->getValue();
00276 bottle.addDouble (d);
00277 }
00278 }
00279 else if (t == STRINGARRAY) {
00280 StringArray * a = (StringArray*) d.get();
00281 int size = a->getSize();
00282
00283 if (a->isDynamic() == true) {
00284 bottle.addInt (size);
00285 }
00286
00287 for (int i = 0; i < size; i++) {
00288 std::string s = (*a) [i]->getValue();
00289 bottle.addString (s.c_str());
00290 }
00291 }
00292 else if (t == DATABOX) {
00293 shared_ptr<DataBox> box = static_pointer_cast<DataBox> (d);
00294 writeDataBoxToBottle (box, bottle);
00295 }
00296 else if (t == DATABOXARRAY) {
00297 DataBoxArray * a = (DataBoxArray*) d.get();
00298 int size = a->getSize();
00299
00300 if (a->isDynamic() == true) {
00301 bottle.addInt (size);
00302 }
00303
00304 for (int i = 0; i < size; i++) {
00305 shared_ptr<DataBox> b = (*a) [i];
00306 writeDataBoxToBottle (b, bottle);
00307 }
00308 }
00309 else {
00310 throw Exception ("DataFactory::writeDataBoxToBottle: unknown type conversion");
00311 }
00312 };
00313 };
00314
00315 shared_ptr<DataStructureDescription> DataFactory::getDataStructureDescription (std::string dataStructureName)
00316 {
00317 return dataStructureDescriptionMap[dataStructureName];
00318 }