EntityRepository.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 EntityRepository.cpp
00024  * @Description EntityRepository implementation
00025  * @Status Work in Progress
00026  * @Version $Id: EntityRepository.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer mafb
00028  */
00029 
00030 
00031 #include "config.h"
00032 
00033 #include "EntityRepository.hpp"
00034 #include "EntityDescription.hpp"
00035 #include "EntityDescriptionRepository.hpp"
00036 #include "ServiceInterfaceDescription.hpp"
00037 #include "ServiceTypeDescription.hpp"
00038 #include "Service.hpp"
00039 
00040 #include <Exception.hpp>
00041 
00042 #include <sstream>
00043 
00044 using namespace mermaid::support::service;
00045 using mermaid::support::errorhandling::Exception;
00046 
00047 
00048 void EntityRepository::addEntity (shared_ptr<Entity> entity)
00049 {
00050   std::string entityName = entity->getEntityDescription()->getEntityName();
00051   
00052   if (entityMap_.find (entityName) != entityMap_.end()) {
00053     std::stringstream ss;
00054     ss << "EntityRepository::addEntity : repository already has an entity registered with entityName=\"" << entityName << "\"";
00055     throw Exception (ss.str());
00056   }
00057   
00058   entityMap_[entityName] = entity;
00059 }; // addEntity()
00060 
00061 shared_ptr<Entity> EntityRepository::getEntity (std::string entityName)
00062 {
00063   std::map<std::string, shared_ptr<Entity> >::iterator it = entityMap_.find (entityName);
00064   
00065   if (it == entityMap_.end()) {
00066     std::stringstream ss;
00067     ss << "EntityRepository::getEntity : repository does not have an entity registered with entityName=\"" << entityName << "\"";
00068     throw Exception (ss.str());
00069   }
00070   
00071   return (*it).second;
00072 }; // getEntity()
00073 
00074 shared_ptr<Entity> EntityRepository::getOrCreateEntity (std::string entityName)
00075 {
00076 
00077   std::map<std::string, shared_ptr<Entity> >::iterator it = entityMap_.find (entityName);
00078   
00079   if (it == entityMap_.end()) {
00080     shared_ptr<EntityDescription> entityDescription = EntityDescriptionRepository::getInstance()->getEntityDescription (entityName);
00081     shared_ptr<Entity> entity = shared_ptr<Entity> (new Entity (entityDescription));
00082     this->addEntity (entity);
00083     return entity;
00084   }
00085   
00086   return (*it).second;
00087 }; // getOrCreateEntity()
00088 
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3