factory.cpp
Go to the documentation of this file.
00001 // g2o - General Graph Optimization
00002 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
00003 // 
00004 // g2o is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU Lesser General Public License as published
00006 // by the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 // 
00009 // g2o is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 // 
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "factory.h"
00018 
00019 #include "creators.h"
00020 
00021 #include <iostream>
00022 #include <typeinfo>
00023 #include <cassert>
00024 
00025 using namespace std;
00026 
00027 namespace g2o {
00028 
00029 Factory* Factory::factoryInstance = 0;
00030 
00031 Factory::Factory()
00032 {
00033 }
00034 
00035 Factory::~Factory()
00036 {
00037   for (CreatorMap::iterator it = creator.begin(); it != creator.end(); ++it)
00038     delete it->second;
00039 }
00040 
00041 Factory* Factory::instance()
00042 {
00043   if (factoryInstance == 0) {
00044     factoryInstance = new Factory();
00045   }
00046 
00047   return factoryInstance;
00048 }
00049 
00050 void Factory::registerType(const std::string& tag, AbstractHyperGraphElementCreator* c)
00051 {
00052   CreatorMap::const_iterator foundIt = creator.find(tag);
00053   if (foundIt != creator.end()) {
00054     cerr << "FACTORY WARNING: Overwriting Vertex tag " << tag << endl;
00055     assert(0);
00056   }
00057   TagLookup::const_iterator tagIt = tagLookup.find(c->name());
00058   if (tagIt != tagLookup.end()) {
00059     cerr << "FACTORY WARNING: Registering same class for two tags " << c->name() << endl;
00060     assert(0);
00061   }
00062 
00063   creator[tag] = c;
00064   tagLookup[c->name()] = tag;
00065 }
00066 
00067 HyperGraph::HyperGraphElement* Factory::construct(const std::string& tag)
00068 {
00069   CreatorMap::const_iterator foundIt = creator.find(tag);
00070   if (foundIt != creator.end()) {
00071     //cerr << "tag " << tag << " -> " << foundIt->second->name() << endl;
00072     return foundIt->second->construct();
00073   }
00074   return 0;
00075 }
00076 
00077 const std::string& Factory::tag(HyperGraph::HyperGraphElement* e)
00078 {
00079   static std::string emptyStr("");
00080   TagLookup::const_iterator foundIt = instance()->tagLookup.find(typeid(*e).name());
00081   if (foundIt != instance()->tagLookup.end())
00082     return foundIt->second;
00083   return emptyStr;
00084 }
00085 
00086 void Factory::fillKnownTypes(std::vector<std::string>& types)
00087 {
00088   types.clear();
00089   for (CreatorMap::const_iterator it = creator.begin(); it != creator.end(); ++it)
00090     types.push_back(it->first);
00091 }
00092 
00093 bool Factory::knowsTag(const std::string& tag)
00094 {
00095   return (creator.find(tag) != creator.end());
00096 }
00097 
00098 void Factory::destroy()
00099 {
00100   delete factoryInstance;
00101   factoryInstance = 0;
00102 }
00103 
00104 void Factory::printRegisteredTypes(std::ostream& os, bool comment)
00105 {
00106   if (comment)
00107     os << "# ";
00108   os << "types:" << endl;
00109   for (CreatorMap::const_iterator it = creator.begin(); it != creator.end(); ++it) {
00110     if (comment)
00111       os << "#";
00112     cerr << "\t" << it->first << endl;
00113   }
00114 }
00115 
00116 } // end namespace


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:08