g2o
Toggle main menu visibility
Loading...
Searching...
No Matches
g2o
core
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 <cassert>
30
#include <iostream>
31
#include <typeinfo>
32
33
#include "
cache.h
"
34
#include "
creators.h
"
35
#include "
g2o/stuff/color_macros.h
"
36
#include "
g2o/stuff/logger.h
"
37
#include "
optimizable_graph.h
"
38
#include "
parameter.h
"
39
40
using namespace
std
;
41
42
namespace
g2o
{
43
44
std::unique_ptr<Factory>
Factory::factoryInstance
;
45
46
Factory
*
Factory::instance
() {
47
if
(
factoryInstance
.get() ==
nullptr
) {
48
factoryInstance
.reset(
new
Factory
);
49
#ifdef G2O_DEBUG_FACTORY
50
G2O_DEBUG
(
"Factory allocated {}"
,
51
static_cast<
void
*
>
(
factoryInstance
.get()));
52
#endif
53
}
54
55
return
factoryInstance
.get();
56
}
57
58
void
Factory::registerType
(
59
const
std::string&
tag
,
60
const
std::shared_ptr<AbstractHyperGraphElementCreator>& c) {
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
}
112
113
void
Factory::unregisterType
(
const
std::string&
tag
) {
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
}
128
129
HyperGraph::HyperGraphElement
*
Factory::construct
(
130
const
std::string&
tag
)
const
{
131
CreatorMap::const_iterator foundIt =
_creator
.find(
tag
);
132
if
(foundIt !=
_creator
.end()) {
133
return
foundIt->second->creator->construct();
134
}
135
return
nullptr
;
136
}
137
138
const
std::string&
Factory::tag
(
const
HyperGraph::HyperGraphElement
* e)
const
{
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
}
144
145
void
Factory::fillKnownTypes
(std::vector<std::string>& types)
const
{
146
types.clear();
147
for
(CreatorMap::const_iterator it =
_creator
.begin(); it !=
_creator
.end();
148
++it)
149
types.push_back(it->first);
150
}
151
152
bool
Factory::knowsTag
(
const
std::string&
tag
,
int
* elementType)
const
{
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
}
161
162
void
Factory::destroy
() {
163
std::unique_ptr<Factory> aux;
164
factoryInstance
.swap(aux);
165
}
166
167
void
Factory::printRegisteredTypes
(std::ostream& os,
bool
comment)
const
{
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
}
176
177
HyperGraph::HyperGraphElement
*
Factory::construct
(
178
const
std::string&
tag
,
179
const
HyperGraph::GraphElemBitset
& elemsToConstruct)
const
{
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
}
190
191
}
// namespace g2o
cache.h
g2o::Factory::CreatorInformation
Definition
factory.h:103
g2o::Factory::CreatorInformation::creator
std::shared_ptr< AbstractHyperGraphElementCreator > creator
Definition
factory.h:105
g2o::Factory::CreatorInformation::elementTypeBit
int elementTypeBit
Definition
factory.h:106
g2o::Factory::unregisterType
void unregisterType(const std::string &tag)
Definition
factory.cpp:113
g2o::Factory::knowsTag
bool knowsTag(const std::string &tag, int *elementType=0) const
Definition
factory.cpp:152
g2o::Factory::_creator
CreatorMap _creator
look-up map for the existing creators
Definition
factory.h:113
g2o::Factory::printRegisteredTypes
void printRegisteredTypes(std::ostream &os, bool comment=false) const
Definition
factory.cpp:167
g2o::Factory::factoryInstance
static std::unique_ptr< Factory > factoryInstance
Definition
factory.h:117
g2o::Factory::instance
static Factory * instance()
return the instance
Definition
factory.cpp:46
g2o::Factory::_tagLookup
TagLookup _tagLookup
reverse look-up, class name to tag
Definition
factory.h:114
g2o::Factory::construct
HyperGraph::HyperGraphElement * construct(const std::string &tag) const
Definition
factory.cpp:129
g2o::Factory::destroy
static void destroy()
free the instance
Definition
factory.cpp:162
g2o::Factory::Factory
Factory(Factory const &)=delete
g2o::Factory::tag
const std::string & tag(const HyperGraph::HyperGraphElement *v) const
return the TAG given a vertex
Definition
factory.cpp:138
g2o::Factory::fillKnownTypes
void fillKnownTypes(std::vector< std::string > &types) const
Definition
factory.cpp:145
g2o::Factory::registerType
void registerType(const std::string &tag, const std::shared_ptr< AbstractHyperGraphElementCreator > &c)
Definition
factory.cpp:58
g2o::HyperGraph::GraphElemBitset
std::bitset< HyperGraph::HGET_NUM_ELEMS > GraphElemBitset
Definition
hyper_graph.h:70
color_macros.h
creators.h
factory.h
G2O_WARN
#define G2O_WARN(...)
G2O_DEBUG
#define G2O_DEBUG(...)
logger.h
g2o
Definition
dl_wrapper.cpp:55
std
Definition
jet.h:876
optimizable_graph.h
parameter.h
g2o::HyperGraph::HyperGraphElement
Definition
hyper_graph.h:80
g2o::HyperGraph::HyperGraphElement::elementType
virtual HyperGraphElementType elementType() const =0
Generated on
for g2o by
1.17.0