g2o
Classes | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Static Private Attributes | List of all members
g2o::Factory Class Reference

create vertices and edges based on TAGs in, for example, a file More...

#include <factory.h>

Collaboration diagram for g2o::Factory:
Collaboration graph
[legend]

Classes

class  CreatorInformation
 

Public Member Functions

void registerType (const std::string &tag, AbstractHyperGraphElementCreator *c)
 
void unregisterType (const std::string &tag)
 
HyperGraph::HyperGraphElementconstruct (const std::string &tag) const
 
HyperGraph::HyperGraphElementconstruct (const std::string &tag, const HyperGraph::GraphElemBitset &elemsToConstruct) const
 
bool knowsTag (const std::string &tag, int *elementType=0) const
 
const std::string & tag (const HyperGraph::HyperGraphElement *v) const
 return the TAG given a vertex More...
 
void fillKnownTypes (std::vector< std::string > &types) const
 
void printRegisteredTypes (std::ostream &os, bool comment=false) const
 

Static Public Member Functions

static Factoryinstance ()
 return the instance More...
 
static void destroy ()
 free the instance More...
 

Protected Types

typedef std::map< std::string, CreatorInformation * > CreatorMap
 
typedef std::map< std::string, std::string > TagLookup
 

Protected Member Functions

 Factory ()
 
 ~Factory ()
 

Protected Attributes

CreatorMap _creator
 look-up map for the existing creators More...
 
TagLookup _tagLookup
 reverse look-up, class name to tag More...
 

Static Private Attributes

static FactoryfactoryInstance = 0
 

Detailed Description

create vertices and edges based on TAGs in, for example, a file

Definition at line 49 of file factory.h.

Member Typedef Documentation

typedef std::map<std::string, CreatorInformation*> g2o::Factory::CreatorMap
protected

Definition at line 118 of file factory.h.

typedef std::map<std::string, std::string> g2o::Factory::TagLookup
protected

Definition at line 119 of file factory.h.

Constructor & Destructor Documentation

g2o::Factory::Factory ( )
protected

Definition at line 45 of file factory.cpp.

46 {
47 }
g2o::Factory::~Factory ( )
protected

Definition at line 49 of file factory.cpp.

50 {
51 # ifdef G2O_DEBUG_FACTORY
52  cerr << "# Factory destroying " << (void*)this << endl;
53 # endif
54  for (CreatorMap::iterator it = _creator.begin(); it != _creator.end(); ++it) {
55  delete it->second->creator;
56  }
57  _creator.clear();
58  _tagLookup.clear();
59 }
TagLookup _tagLookup
reverse look-up, class name to tag
Definition: factory.h:124
CreatorMap _creator
look-up map for the existing creators
Definition: factory.h:123

Member Function Documentation

HyperGraph::HyperGraphElement * g2o::Factory::construct ( const std::string &  tag) const

construct a graph element based on its tag

Definition at line 147 of file factory.cpp.

Referenced by g2o::CacheContainer::createCache(), g2o::EdgeCreator::createEdge(), g2o::ParameterContainer::read(), and g2o::OptimizableGraph::setFixed().

148 {
149  CreatorMap::const_iterator foundIt = _creator.find(tag);
150  if (foundIt != _creator.end()) {
151  //cerr << "tag " << tag << " -> " << (void*) foundIt->second->creator << " " << foundIt->second->creator->name() << endl;
152  return foundIt->second->creator->construct();
153  }
154  return 0;
155 }
const std::string & tag(const HyperGraph::HyperGraphElement *v) const
return the TAG given a vertex
Definition: factory.cpp:157
CreatorMap _creator
look-up map for the existing creators
Definition: factory.h:123
HyperGraph::HyperGraphElement * g2o::Factory::construct ( const std::string &  tag,
const HyperGraph::GraphElemBitset elemsToConstruct 
) const

construct a graph element based on its tag, but only if it's type (a bitmask) matches. A bitmask without any bit set will construct any item. Otherwise a bit has to be set to allow construction of a graph element.

Definition at line 204 of file factory.cpp.

205 {
206  if (elemsToConstruct.none()) {
207  return construct(tag);
208  }
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();
212  }
213  return 0;
214 }
HyperGraph::HyperGraphElement * construct(const std::string &tag) const
Definition: factory.cpp:147
const std::string & tag(const HyperGraph::HyperGraphElement *v) const
return the TAG given a vertex
Definition: factory.cpp:157
CreatorMap _creator
look-up map for the existing creators
Definition: factory.h:123
void g2o::Factory::destroy ( )
static

free the instance

Definition at line 186 of file factory.cpp.

187 {
188  delete factoryInstance;
189  factoryInstance = 0;
190 }
static Factory * factoryInstance
Definition: factory.h:127
void g2o::Factory::fillKnownTypes ( std::vector< std::string > &  types) const

get a list of all known types

Definition at line 166 of file factory.cpp.

167 {
168  types.clear();
169  for (CreatorMap::const_iterator it = _creator.begin(); it != _creator.end(); ++it)
170  types.push_back(it->first);
171 }
stuff to open files and other unsorted components ProjectiveCamera types
CreatorMap _creator
look-up map for the existing creators
Definition: factory.h:123
Factory * g2o::Factory::instance ( )
static
bool g2o::Factory::knowsTag ( const std::string &  tag,
int *  elementType = 0 
) const

return whether the factory knows this tag or not

Definition at line 173 of file factory.cpp.

Referenced by g2o::OptimizableGraph::setFixed(), and g2o::OptimizableGraph::setRenamedTypesFromString().

174 {
175  CreatorMap::const_iterator foundIt = _creator.find(tag);
176  if (foundIt == _creator.end()) {
177  if (elementType)
178  *elementType = -1;
179  return false;
180  }
181  if (elementType)
182  *elementType = foundIt->second->elementTypeBit;
183  return true;
184 }
const std::string & tag(const HyperGraph::HyperGraphElement *v) const
return the TAG given a vertex
Definition: factory.cpp:157
CreatorMap _creator
look-up map for the existing creators
Definition: factory.h:123
void g2o::Factory::printRegisteredTypes ( std::ostream &  os,
bool  comment = false 
) const

print a list of the known registered types to the given stream

Definition at line 192 of file factory.cpp.

193 {
194  if (comment)
195  os << "# ";
196  os << "types:" << endl;
197  for (CreatorMap::const_iterator it = _creator.begin(); it != _creator.end(); ++it) {
198  if (comment)
199  os << "#";
200  cerr << "\t" << it->first << endl;
201  }
202 }
CreatorMap _creator
look-up map for the existing creators
Definition: factory.h:123
void g2o::Factory::registerType ( const std::string &  tag,
AbstractHyperGraphElementCreator c 
)

register a tag for a specific creator

Definition at line 73 of file factory.cpp.

References g2o::AbstractHyperGraphElementCreator::construct(), g2o::Factory::CreatorInformation::creator, g2o::Factory::CreatorInformation::elementTypeBit, HGET_CACHE, HGET_DATA, HGET_EDGE, HGET_PARAMETER, HGET_VERTEX, and g2o::AbstractHyperGraphElementCreator::name().

Referenced by g2o::G2O_ATTRIBUTE_CONSTRUCTOR(), and g2o::RegisterTypeProxy< T >::RegisterTypeProxy().

74 {
75  CreatorMap::const_iterator foundIt = _creator.find(tag);
76  if (foundIt != _creator.end()) {
77  cerr << "FACTORY WARNING: Overwriting Vertex tag " << tag << endl;
78  assert(0);
79  }
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;
83  assert(0);
84  }
85 
86  CreatorInformation* ci = new CreatorInformation();
87  ci->creator = c;
88 
89 #ifdef G2O_DEBUG_FACTORY
90  cerr << "# Factory " << (void*)this << " constructing type " << tag << " ";
91 #endif
92  // construct an element once to figure out its type
93  HyperGraph::HyperGraphElement* element = c->construct();
94  ci->elementTypeBit = element->elementType();
95 
96 #ifdef G2O_DEBUG_FACTORY
97  cerr << "done." << endl;
98  cerr << "# Factory " << (void*)this << " registering " << tag;
99  cerr << " " << (void*) c << " ";
100  switch (element->elementType()) {
102  cerr << " -> Vertex";
103  break;
105  cerr << " -> Edge";
106  break;
108  cerr << " -> Parameter";
109  break;
111  cerr << " -> Cache";
112  break;
114  cerr << " -> Data";
115  break;
116  default:
117  assert(0 && "Unknown element type occured, fix elementTypes");
118  break;
119  }
120  cerr << endl;
121 #endif
122 
123  _creator[tag] = ci;
124  _tagLookup[c->name()] = tag;
125  delete element;
126 }
TagLookup _tagLookup
reverse look-up, class name to tag
Definition: factory.h:124
HGET_EDGE
Definition: hyper_graph.h:63
const std::string & tag(const HyperGraph::HyperGraphElement *v) const
return the TAG given a vertex
Definition: factory.cpp:157
HGET_DATA
Definition: hyper_graph.h:63
CreatorMap _creator
look-up map for the existing creators
Definition: factory.h:123
HGET_PARAMETER
Definition: hyper_graph.h:63
HGET_CACHE
Definition: hyper_graph.h:63
HGET_VERTEX
Definition: hyper_graph.h:63
const std::string & g2o::Factory::tag ( const HyperGraph::HyperGraphElement v) const

return the TAG given a vertex

Definition at line 157 of file factory.cpp.

Referenced by g2o::EdgeCreator::createEdge(), g2o::Cache::key(), g2o::EdgeTypesCostFunction::operator()(), g2o::BackBoneTreeAction::perform(), g2o::OptimizableGraph::saveEdge(), g2o::OptimizableGraph::saveParameter(), g2o::OptimizableGraph::saveUserData(), g2o::OptimizableGraph::saveVertex(), g2o::ParameterContainer::write(), and g2o::Gm2dlIO::writeGm2dl().

158 {
159  static std::string emptyStr("");
160  TagLookup::const_iterator foundIt = _tagLookup.find(typeid(*e).name());
161  if (foundIt != _tagLookup.end())
162  return foundIt->second;
163  return emptyStr;
164 }
TagLookup _tagLookup
reverse look-up, class name to tag
Definition: factory.h:124
void g2o::Factory::unregisterType ( const std::string &  tag)

unregister a tag for a specific creator

Definition at line 128 of file factory.cpp.

References g2o::AbstractHyperGraphElementCreator::name().

Referenced by g2o::RegisterTypeProxy< T >::~RegisterTypeProxy().

129  {
130  // Look for the tag
131  CreatorMap::iterator tagPosition = _creator.find(tag);
132 
133  if (tagPosition != _creator.end()) {
134 
135  AbstractHyperGraphElementCreator* c = tagPosition->second->creator;
136 
137  // If we found it, remove the creator from the tag lookup map
138  TagLookup::iterator classPosition = _tagLookup.find(c->name());
139  if (classPosition != _tagLookup.end())
140  {
141  _tagLookup.erase(classPosition);
142  }
143  _creator.erase(tagPosition);
144  }
145  }
TagLookup _tagLookup
reverse look-up, class name to tag
Definition: factory.h:124
const std::string & tag(const HyperGraph::HyperGraphElement *v) const
return the TAG given a vertex
Definition: factory.cpp:157
CreatorMap _creator
look-up map for the existing creators
Definition: factory.h:123

Member Data Documentation

CreatorMap g2o::Factory::_creator
protected

look-up map for the existing creators

Definition at line 123 of file factory.h.

TagLookup g2o::Factory::_tagLookup
protected

reverse look-up, class name to tag

Definition at line 124 of file factory.h.

Factory * g2o::Factory::factoryInstance = 0
staticprivate

Definition at line 127 of file factory.h.


The documentation for this class was generated from the following files: