33 #include "../stuff/color_macros.h" 43 Factory* Factory::factoryInstance = 0;
51 # ifdef G2O_DEBUG_FACTORY 52 cerr <<
"# Factory destroying " << (
void*)
this << endl;
54 for (CreatorMap::iterator it = _creator.begin(); it != _creator.end(); ++it) {
55 delete it->second->creator;
63 if (factoryInstance == 0) {
65 # ifdef G2O_DEBUG_FACTORY 66 cerr <<
"# Factory allocated " << (
void*)factoryInstance << endl;
70 return factoryInstance;
75 CreatorMap::const_iterator foundIt = _creator.find(tag);
76 if (foundIt != _creator.end()) {
77 cerr <<
"FACTORY WARNING: Overwriting Vertex tag " << tag << endl;
80 TagLookup::const_iterator tagIt = _tagLookup.find(c->
name());
81 if (tagIt != _tagLookup.end()) {
82 cerr <<
"FACTORY WARNING: Registering same class for two tags " << c->
name() << endl;
89 #ifdef G2O_DEBUG_FACTORY 90 cerr <<
"# Factory " << (
void*)
this <<
" constructing type " << tag <<
" ";
96 #ifdef G2O_DEBUG_FACTORY 97 cerr <<
"done." << endl;
98 cerr <<
"# Factory " << (
void*)
this <<
" registering " << tag;
99 cerr <<
" " << (
void*) c <<
" ";
100 switch (element->elementType()) {
101 case HyperGraph::HGET_VERTEX:
102 cerr <<
" -> Vertex";
104 case HyperGraph::HGET_EDGE:
107 case HyperGraph::HGET_PARAMETER:
108 cerr <<
" -> Parameter";
110 case HyperGraph::HGET_CACHE:
113 case HyperGraph::HGET_DATA:
117 assert(0 &&
"Unknown element type occured, fix elementTypes");
124 _tagLookup[c->
name()] = tag;
128 void Factory::unregisterType(
const std::string& tag)
131 CreatorMap::iterator tagPosition = _creator.find(tag);
133 if (tagPosition != _creator.end()) {
138 TagLookup::iterator classPosition = _tagLookup.find(c->
name());
139 if (classPosition != _tagLookup.end())
141 _tagLookup.erase(classPosition);
143 _creator.erase(tagPosition);
149 CreatorMap::const_iterator foundIt = _creator.find(tag);
150 if (foundIt != _creator.end()) {
152 return foundIt->second->creator->construct();
159 static std::string emptyStr(
"");
160 TagLookup::const_iterator foundIt = _tagLookup.find(
typeid(*e).name());
161 if (foundIt != _tagLookup.end())
162 return foundIt->second;
166 void Factory::fillKnownTypes(std::vector<std::string>& types)
const 169 for (CreatorMap::const_iterator it = _creator.begin(); it != _creator.end(); ++it)
170 types.push_back(it->first);
173 bool Factory::knowsTag(
const std::string& tag,
int* elementType)
const 175 CreatorMap::const_iterator foundIt = _creator.find(tag);
176 if (foundIt == _creator.end()) {
182 *elementType = foundIt->second->elementTypeBit;
186 void Factory::destroy()
188 delete factoryInstance;
192 void Factory::printRegisteredTypes(std::ostream& os,
bool comment)
const 196 os <<
"types:" << endl;
197 for (CreatorMap::const_iterator it = _creator.begin(); it != _creator.end(); ++it) {
200 cerr <<
"\t" << it->first << endl;
206 if (elemsToConstruct.none()) {
207 return construct(tag);
209 CreatorMap::const_iterator foundIt = _creator.find(tag);
210 if (foundIt != _creator.end() && foundIt->second->elementTypeBit >= 0 && elemsToConstruct.test(foundIt->second->elementTypeBit)) {
211 return foundIt->second->creator->construct();
virtual HyperGraph::HyperGraphElement * construct()=0
std::bitset< HyperGraph::HGET_NUM_ELEMS > GraphElemBitset
create vertices and edges based on TAGs in, for example, a file
Abstract interface for allocating HyperGraphElement.
virtual const std::string & name() const =0