DataStructureDescription.cpp

Go to the documentation of this file.
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 DataStructureDescription.cpp
00024  * @Description DataStructureDescription class implementation.
00025  * @Status Implementing
00026  * @Version $Id: DataStructureDescription.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer Marco Barbosa
00028  */
00029 
00030 #include "config.h"
00031 
00032 #include "DataStructureDescription.hpp"
00033 
00034 #include <Exception.hpp>
00035 #include <XmlAttribute.hpp>
00036 #include <XmlAttributeVector.hpp>
00037 #include <XmlElementVector.hpp>
00038 #include <iostream>
00039 
00040 using namespace mermaid::support::data;
00041 
00042 using mermaid::support::errorhandling::Exception;
00043 using mermaid::support::xml::XmlAttribute;
00044 using mermaid::support::xml::XmlAttributeVector;
00045 using mermaid::support::xml::XmlElementVector;
00046 
00047 #include <sstream>
00048 
00049 DataStructureDescription::DataStructureDescription (shared_ptr<XmlElement> structureDescriptionElement)
00050 {
00051   if (structureDescriptionElement->getName().compare ("data-structure") != 0) {
00052     throw Exception ("DataStructureDescription::DataStructureDescription : expected \"data-structure\" element");
00053   }
00054   
00055   XmlElementVector children = structureDescriptionElement->getChildrenElements();
00056   
00057   shared_ptr<XmlElement> dataNameElement = children.getFirstElementWithName ("data-name");
00058   shared_ptr<XmlElement> dataCompositionElement = children.getFirstElementWithName ("data-composition");
00059   
00060   name_ = * (dataNameElement->getFirstNonBlankCharData());
00061   composition_ = XmlElement (*dataCompositionElement);
00062   
00063   
00064   XmlElementVector dataItemVector = dataCompositionElement->getChildrenElements();
00065   
00066   XmlElementVector::iterator it;
00067   
00068   for (it = dataItemVector.begin(); it != dataItemVector.end(); it++) {
00069     shared_ptr<XmlElement> dataItem = *it;
00070     XmlElementVector dataItemChildren = dataItem->getChildrenElements();
00071     
00072     shared_ptr<XmlElement> itemNameElement = dataItemChildren.getFirstElementWithName ("name");
00073     shared_ptr<XmlElement> itemTypeElement = dataItemChildren.getFirstElementWithName ("type");
00074     
00075     std::string itemName = * (itemNameElement->getFirstNonBlankCharData());
00076     std::string itemType = * (itemTypeElement->getFirstNonBlankCharData());
00077     
00078     XmlAttributeVector itemTypeAttributes = itemTypeElement->getAttributes();
00079     shared_ptr<XmlAttribute> isArrayAttribute = itemTypeAttributes.getFirstAttributeWithName ("isArray");
00080     shared_ptr<XmlAttribute> arraySizeAttribute = itemTypeAttributes.getFirstAttributeWithName ("arraySize");
00081     
00082     bool isArray = false;
00083     bool isDynamic = false;
00084     
00085     if (isArrayAttribute == false) {
00086       isArray = false;
00087     }
00088     else {
00089       isArray = isArrayAttribute->getValue().compare ("true") == 0 ? true : false;
00090     }
00091     
00092     if (isArray == true) {
00093       if (arraySizeAttribute == false) {
00094         isDynamic = true;
00095       }
00096       else {
00097         isDynamic = arraySizeAttribute->getValue().compare ("dynamic") == 0 ? true : false;
00098       }
00099     }
00100     
00101     
00102     //verify if item name collides with an item type name
00103     
00104     if ( (itemName.compare ("integer") == 0) | (itemName.compare ("double") == 0) | (itemName.compare ("string") == 0)) {
00105       throw Exception ("DataBox::DataBox(DataStructureDescription) : itemName cannot be any of: integer, double or string");
00106     }
00107     
00108     //std::cerr << "Item name:\t" << itemName << "\ttype:\t" << itemType << "\tisArray:\t" << isArray << "\tisDynamic:\t" << isDynamic << std::endl;
00109     
00110     if (isArray == false) {
00111       shared_ptr<DataItemDescription> did (new DataItemDescription (itemName, itemType));
00112       itemDescriptionVector_.push_back (did);
00113       
00114     }
00115     else {
00116       if (isDynamic == true) {
00117         shared_ptr<DataItemDescription> did (new DataItemDescription (itemName, itemType, true));
00118         itemDescriptionVector_.push_back (did);
00119       }
00120       else {
00121         std::stringstream ss;
00122         ss << arraySizeAttribute->getValue();
00123         int size;
00124         ss >> size;
00125         
00126         shared_ptr<DataItemDescription> did (new DataItemDescription (itemName, itemType, true, true, size));
00127         itemDescriptionVector_.push_back (did);
00128       }
00129     }
00130   }
00131   
00132   
00133   
00134 }; // DataDataStructureDescription()
00135 
00136 std::string DataStructureDescription::getName()
00137 {
00138   return name_;
00139 }; // getName()
00140 
00141 std::vector<shared_ptr<DataItemDescription> > DataStructureDescription::getItemDescriptionVector()
00142 {
00143   return itemDescriptionVector_;
00144 }; // getItemDescriptionVector()
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3