Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
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 }