g2o
Loading...
Searching...
No Matches
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>

Classes

class  CreatorInformation
 

Public Member Functions

 Factory (Factory const &)=delete
 
Factoryoperator= (Factory const &)=delete
 
void registerType (const std::string &tag, const std::shared_ptr< 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
 
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
 
static void destroy ()
 free the instance
 

Protected Types

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

Protected Member Functions

 Factory ()=default
 

Protected Attributes

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

Static Private Attributes

static std::unique_ptr< FactoryfactoryInstance
 

Detailed Description

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

Definition at line 48 of file factory.h.

Member Typedef Documentation

◆ CreatorMap

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

Definition at line 109 of file factory.h.

◆ TagLookup

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

Definition at line 110 of file factory.h.

Constructor & Destructor Documentation

◆ Factory() [1/2]

g2o::Factory::Factory ( Factory const &  )
delete

◆ Factory() [2/2]

g2o::Factory::Factory ( )
protecteddefault

Member Function Documentation

◆ construct() [1/2]

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

construct a graph element based on its tag

Definition at line 129 of file factory.cpp.

130 {
131 CreatorMap::const_iterator foundIt = _creator.find(tag);
132 if (foundIt != _creator.end()) {
133 return foundIt->second->creator->construct();
134 }
135 return nullptr;
136}
CreatorMap _creator
look-up map for the existing creators
Definition factory.h:113
const std::string & tag(const HyperGraph::HyperGraphElement *v) const
return the TAG given a vertex
Definition factory.cpp:138

References _creator, and tag().

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

◆ construct() [2/2]

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 177 of file factory.cpp.

179 {
180 if (elemsToConstruct.none()) {
181 return construct(tag);
182 }
183 CreatorMap::const_iterator foundIt = _creator.find(tag);
184 if (foundIt != _creator.end() && foundIt->second->elementTypeBit >= 0 &&
185 elemsToConstruct.test(foundIt->second->elementTypeBit)) {
186 return foundIt->second->creator->construct();
187 }
188 return nullptr;
189}
HyperGraph::HyperGraphElement * construct(const std::string &tag) const
Definition factory.cpp:129

References _creator, construct(), and tag().

◆ destroy()

void g2o::Factory::destroy ( )
static

free the instance

Definition at line 162 of file factory.cpp.

162 {
163 std::unique_ptr<Factory> aux;
164 factoryInstance.swap(aux);
165}
static std::unique_ptr< Factory > factoryInstance
Definition factory.h:117

References factoryInstance.

◆ fillKnownTypes()

void g2o::Factory::fillKnownTypes ( std::vector< std::string > &  types) const

get a list of all known types

Definition at line 145 of file factory.cpp.

145 {
146 types.clear();
147 for (CreatorMap::const_iterator it = _creator.begin(); it != _creator.end();
148 ++it)
149 types.push_back(it->first);
150}

References _creator.

◆ instance()

Factory * g2o::Factory::instance ( )
static

◆ knowsTag()

bool g2o::Factory::knowsTag ( const std::string &  tag,
int *  elementType = 0 
) const

return whether the factory knows this tag or not

Definition at line 152 of file factory.cpp.

152 {
153 CreatorMap::const_iterator foundIt = _creator.find(tag);
154 if (foundIt == _creator.end()) {
155 if (elementType) *elementType = -1;
156 return false;
157 }
158 if (elementType) *elementType = foundIt->second->elementTypeBit;
159 return true;
160}

References _creator, and tag().

Referenced by g2o::OptimizableGraph::setRenamedTypesFromString().

◆ operator=()

Factory & g2o::Factory::operator= ( Factory const &  )
delete

◆ printRegisteredTypes()

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 167 of file factory.cpp.

167 {
168 if (comment) os << "# ";
169 os << "types:" << endl;
170 for (CreatorMap::const_iterator it = _creator.begin(); it != _creator.end();
171 ++it) {
172 if (comment) os << "#";
173 os << "\t" << it->first << endl;
174 }
175}

References _creator.

Referenced by main().

◆ registerType()

void g2o::Factory::registerType ( const std::string &  tag,
const std::shared_ptr< AbstractHyperGraphElementCreator > &  c 
)

register a tag for a specific creator

Definition at line 58 of file factory.cpp.

60 {
61 CreatorMap::const_iterator foundIt = _creator.find(tag);
62 if (foundIt != _creator.end()) {
63 G2O_WARN("Factory: Overwriting Vertex tag {}", tag);
64 assert(0);
65 }
66 TagLookup::const_iterator tagIt = _tagLookup.find(c->name());
67 if (tagIt != _tagLookup.end()) {
68 G2O_WARN("factory: Registering same class for two tags {}", c->name());
69 assert(0);
70 }
71
72 CreatorInformation* ci = new CreatorInformation();
73 ci->creator = c;
74
75#ifdef G2O_DEBUG_FACTORY
76 G2O_DEBUG("Factory {} constructing type {}", static_cast<void*>(this), tag);
77#endif
78 // construct an element once to figure out its type
79 HyperGraph::HyperGraphElement* element = c->construct();
80 ci->elementTypeBit = element->elementType();
81
82#ifdef G2O_DEBUG_FACTORY
83 G2O_DEBUG("done.");
84 G2O_DEBUG("Factory {} registering {}", static_cast<void*>(this), tag);
85 G2O_DEBUG("{}", static_cast<void*>(c));
86 switch (element->elementType()) {
87 case HyperGraph::HGET_VERTEX:
88 G2O_DEBUG(" -> Vertex");
89 break;
90 case HyperGraph::HGET_EDGE:
91 G2O_DEBUG(" -> Edge");
92 break;
93 case HyperGraph::HGET_PARAMETER:
94 G2O_DEBUG(" -> Parameter");
95 break;
96 case HyperGraph::HGET_CACHE:
97 G2O_DEBUG(" -> Cache");
98 break;
99 case HyperGraph::HGET_DATA:
100 G2O_DEBUG(" -> Data");
101 break;
102 default:
103 assert(0 && "Unknown element type occurred, fix elementTypes");
104 break;
105 }
106#endif
107
108 _creator[tag] = std::unique_ptr<CreatorInformation>(ci);
109 _tagLookup[c->name()] = tag;
110 delete element;
111}
TagLookup _tagLookup
reverse look-up, class name to tag
Definition factory.h:114
#define G2O_WARN(...)
Definition logger.h:88

References _creator, _tagLookup, g2o::Factory::CreatorInformation::creator, g2o::HyperGraph::HyperGraphElement::elementType(), g2o::Factory::CreatorInformation::elementTypeBit, G2O_DEBUG, G2O_WARN, and tag().

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

◆ tag()

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

return the TAG given a vertex

Definition at line 138 of file factory.cpp.

138 {
139 static std::string emptyStr("");
140 TagLookup::const_iterator foundIt = _tagLookup.find(typeid(*e).name());
141 if (foundIt != _tagLookup.end()) return foundIt->second;
142 return emptyStr;
143}

References _tagLookup.

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

◆ unregisterType()

void g2o::Factory::unregisterType ( const std::string &  tag)

unregister a tag for a specific creator

Definition at line 113 of file factory.cpp.

113 {
114 // Look for the tag
115 CreatorMap::iterator tagPosition = _creator.find(tag);
116
117 if (tagPosition != _creator.end()) {
118 const auto& c = tagPosition->second->creator;
119
120 // If we found it, remove the creator from the tag lookup map
121 TagLookup::iterator classPosition = _tagLookup.find(c->name());
122 if (classPosition != _tagLookup.end()) {
123 _tagLookup.erase(classPosition);
124 }
125 _creator.erase(tagPosition);
126 }
127}

References _creator, _tagLookup, and tag().

Member Data Documentation

◆ _creator

CreatorMap g2o::Factory::_creator
protected

look-up map for the existing creators

Definition at line 113 of file factory.h.

Referenced by construct(), construct(), fillKnownTypes(), knowsTag(), printRegisteredTypes(), registerType(), and unregisterType().

◆ _tagLookup

TagLookup g2o::Factory::_tagLookup
protected

reverse look-up, class name to tag

Definition at line 114 of file factory.h.

Referenced by registerType(), tag(), and unregisterType().

◆ factoryInstance

std::unique_ptr< Factory > g2o::Factory::factoryInstance
staticprivate

Definition at line 117 of file factory.h.

Referenced by destroy(), and instance().


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