00001 /* 00002 Copyright 2007, 2008, 2009, 2010, 2011 Instituto de Sistemas e Robotica, Instituto Superior Tecnico 00003 00004 This file is part of MeRMaID. 00005 00006 MeRMaID is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU Lesser General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 MeRMaID is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public License 00017 along with MeRMaID. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 00021 00022 /** 00023 * @Filename DataValueVector.cpp 00024 * @Description DataValueVector class implementation. 00025 * @Status Implementing 00026 * @Version $Id: DataValueVector.cpp 1 2011-03-04 18:13:18Z jreis $ 00027 * @Maintainer 00028 */ 00029 00030 #include "config.h" 00031 00032 #include "DataValueVector.hpp" 00033 00034 #include <DataFactory.hpp> 00035 00036 #include <Exception.hpp> 00037 00038 #include <sstream> 00039 //#include <iostream> 00040 00041 using namespace mermaid::support::data; 00042 00043 using mermaid::support::data::DataFactory; 00044 using mermaid::support::errorhandling::Exception; 00045 00046 00047 DataValueVector * DataValueVector::clone() 00048 { 00049 return new DataValueVector (*this); 00050 }; // clone() 00051 00052 DataValueVector::DataValueVector() 00053 { 00054 setValue (vector<shared_ptr<DataValue> >()); 00055 }; 00056 00057 DataValueVector::DataValueVector (vector<shared_ptr<DataValue> > value) 00058 { 00059 setValue (value); 00060 }; 00061 00062 void DataValueVector::setValue (vector<shared_ptr<DataValue> > value) 00063 { 00064 valueVector_ = value; 00065 }; 00066 00067 void DataValueVector::pushBack (shared_ptr<DataValue> value) 00068 { 00069 valueVector_.push_back (value); 00070 }; // pushBack() 00071 00072 shared_ptr<DataValue> DataValueVector::getValue (unsigned int pos) 00073 { 00074 if (pos >= (unsigned int) valueVector_.size()) { 00075 throw Exception ("DataValueVector::getValue : accessing an invalid member"); 00076 } 00077 return valueVector_[pos]; 00078 }; // getValue() 00079 00080 void DataValueVector::setValue (unsigned int pos, shared_ptr<DataValue> value) 00081 { 00082 if (pos >= (unsigned int) valueVector_.size()) { 00083 throw Exception ("DataValueVector::setValue : accessing an invalid member"); 00084 } 00085 valueVector_[pos] = value; 00086 }; // setValue() 00087 00088 int DataValueVector::getSize() 00089 { 00090 return valueVector_.size(); 00091 }; // getSize() 00092 00093 DataValueVector::operator std::string() const 00094 { 00095 std::stringstream ss; 00096 00097 ss << "<DataValueVector>"; 00098 00099 vector<shared_ptr<DataValue> >::const_iterator it; 00100 00101 for (it = valueVector_.begin(); it != valueVector_.end(); it++) { 00102 shared_ptr<DataValue> d = *it; 00103 if (d == false) { 00104 throw Exception ("DataValueVector::operator std :: string() : invalid DataValue pointer inside the container"); 00105 } 00106 ss << (std::string) *d; 00107 if ( (it + 1) != valueVector_.end()) { 00108 ss << "|"; 00109 } 00110 } 00111 00112 ss << "</DataValueVector>"; 00113 00114 return ss.str(); 00115 }; 00116 00117 //conversion methods 00118 /*DataValueVector& DataValueVector::operator=(Bottle &b) 00119 { 00120 //std::cerr << "DataValueVector::operator=(Bottle &b)" << std::endl; 00121 DataValueVector::DataValueVector(); 00122 00123 buildDataValueVector(*this, b); 00124 00125 return *this; 00126 };*/ 00127 00128 00129 /*void DataValueVector::buildDataValueVector(DataValueVector &v, Bottle &b) 00130 { 00131 00132 for (int i=0; i<b.size(); i++) { 00133 00134 //std::cerr << "DataValueVector::buildDataValueVector : [" << i << "]: "; 00135 00136 Value& element = b.get(i); 00137 switch (element.getCode()) 00138 { 00139 case BOTTLE_TAG_INT: 00140 { 00141 shared_ptr<DataValue> i = DataFactory::buildInteger(element.asInt()); 00142 v.pushBack(i); 00143 //std::cerr << "int " << element.asInt() << std::endl; 00144 } 00145 break; 00146 case BOTTLE_TAG_DOUBLE: 00147 { 00148 shared_ptr<DataValue> f = DataFactory::buildFloat(element.asDouble()); 00149 v.pushBack(f); 00150 //std::cerr << "float " << element.asDouble() << std::endl; 00151 } 00152 break; 00153 case BOTTLE_TAG_STRING: 00154 { 00155 shared_ptr<DataValue> s = DataFactory::buildString(std::string(element.asString().c_str())); 00156 v.pushBack(s); 00157 //std::cerr << "string \"" << element.asString() << "\"" << std::endl; 00158 } 00159 break; 00160 case BOTTLE_TAG_BLOB: 00161 { 00162 throw Exception("DataValueVector::buildDataValueVector : unsupported type: BLOB"); 00163 //std::cerr << "binary blob of length " << element.asBlobLength() << std::endl; 00164 } 00165 break; 00166 case BOTTLE_TAG_VOCAB: 00167 { 00168 throw Exception("DataValueVector::buildDataValueVector : unsupported type: VOCAB"); 00169 //std::cerr << "vocab [" << Vocab::decode(element.asVocab()) << "]" << std::endl; 00170 } 00171 break; 00172 default: 00173 { 00174 if (element.isList()) { 00175 Bottle *lst = element.asList(); 00176 //std::cerr << "list of " << lst->size() << "elements" << std::endl; 00177 shared_ptr<DataValue> newVector(new DataValueVector()); 00178 buildDataValueVector(*((DataValueVector*)newVector.get()), *lst); 00179 v.pushBack(newVector); 00180 } else { 00181 throw Exception("DataValueVector::buildDataValueVector : unrecognized type"); 00182 } 00183 } 00184 break; 00185 } 00186 } 00187 }; // buildDataValueVector*/ 00188 00189 /*DataValueVector::operator Bottle () 00190 { 00191 //std::cerr << "DataValueVector::operator Bottle " << std::endl; 00192 Bottle b; 00193 00194 vector<shared_ptr<DataValue> >::const_iterator it; 00195 00196 for(it = valueVector_.begin(); it!=valueVector_.end(); it++) 00197 { 00198 shared_ptr<DataValue> d = *it; 00199 DataType t = d->getType(); 00200 00201 if(t==INTEGER) 00202 { 00203 Integer * i = (Integer*)d.get(); 00204 b.addInt(i->getValue()); 00205 } 00206 else if(t == FLOAT) 00207 { 00208 Float * f = (Float*)d.get(); 00209 b.addDouble(f->getValue()); 00210 } 00211 else if(t == STRING) 00212 { 00213 String * s = (String*)d.get(); 00214 b.addString(s->getValue().c_str()); 00215 } 00216 else 00217 { 00218 throw Exception("DataValueVector::operator Bottle : NOT IMPLEMENTED type conversion");// d->getType() << std::endl; 00219 } 00220 } 00221 00222 return b; 00223 };*/ 00224