factory.cpp
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #include "factory.h"
28 
29 #include "creators.h"
30 #include "parameter.h"
31 #include "cache.h"
32 #include "optimizable_graph.h"
33 #include "../stuff/color_macros.h"
34 
35 #include <iostream>
36 #include <typeinfo>
37 #include <cassert>
38 
39 using namespace std;
40 
41 namespace g2o {
42 
43 Factory* Factory::factoryInstance = 0;
44 
45 Factory::Factory()
46 {
47 }
48 
49 Factory::~Factory()
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 }
60 
61 Factory* Factory::instance()
62 {
63  if (factoryInstance == 0) {
64  factoryInstance = new Factory;
65 # ifdef G2O_DEBUG_FACTORY
66  cerr << "# Factory allocated " << (void*)factoryInstance << endl;
67 # endif
68  }
69 
70  return factoryInstance;
71 }
72 
73 void Factory::registerType(const std::string& tag, AbstractHyperGraphElementCreator* c)
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 
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
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()) {
101  case HyperGraph::HGET_VERTEX:
102  cerr << " -> Vertex";
103  break;
104  case HyperGraph::HGET_EDGE:
105  cerr << " -> Edge";
106  break;
107  case HyperGraph::HGET_PARAMETER:
108  cerr << " -> Parameter";
109  break;
110  case HyperGraph::HGET_CACHE:
111  cerr << " -> Cache";
112  break;
113  case HyperGraph::HGET_DATA:
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 }
127 
128  void Factory::unregisterType(const std::string& tag)
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  }
146 
147 HyperGraph::HyperGraphElement* Factory::construct(const std::string& tag) const
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 }
156 
157 const std::string& Factory::tag(const HyperGraph::HyperGraphElement* e) const
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 }
165 
166 void Factory::fillKnownTypes(std::vector<std::string>& types) const
167 {
168  types.clear();
169  for (CreatorMap::const_iterator it = _creator.begin(); it != _creator.end(); ++it)
170  types.push_back(it->first);
171 }
172 
173 bool Factory::knowsTag(const std::string& tag, int* elementType) const
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 }
185 
186 void Factory::destroy()
187 {
188  delete factoryInstance;
189  factoryInstance = 0;
190 }
191 
192 void Factory::printRegisteredTypes(std::ostream& os, bool comment) const
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 }
203 
204 HyperGraph::HyperGraphElement* Factory::construct(const std::string& tag, const HyperGraph::GraphElemBitset& elemsToConstruct) const
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 }
215 
216 } // end namespace
217 
virtual HyperGraph::HyperGraphElement * construct()=0
std::bitset< HyperGraph::HGET_NUM_ELEMS > GraphElemBitset
Definition: hyper_graph.h:74
create vertices and edges based on TAGs in, for example, a file
Definition: factory.h:49
AbstractHyperGraphElementCreator * creator
Definition: factory.h:102
Abstract interface for allocating HyperGraphElement.
Definition: creators.h:41
virtual const std::string & name() const =0


orb_slam2_with_maps_odom
Author(s): teng zhang
autogenerated on Fri Sep 25 2020 03:24:47