factory.h
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 #ifndef G2O_FACTORY_H
28 #define G2O_FACTORY_H
29 
30 #include "../../config.h"
31 #include "../stuff/misc.h"
32 #include "hyper_graph.h"
33 #include "creators.h"
34 
35 #include <string>
36 #include <map>
37 #include <iostream>
38 
39 // define to get some verbose output
40 //#define G2O_DEBUG_FACTORY
41 
42 namespace g2o {
43 
44  class AbstractHyperGraphElementCreator;
45 
49  class Factory
50  {
51  public:
52 
54  static Factory* instance();
55 
57  static void destroy();
58 
62  void registerType(const std::string& tag, AbstractHyperGraphElementCreator* c);
63 
67  void unregisterType(const std::string& tag);
68 
72  HyperGraph::HyperGraphElement* construct(const std::string& tag) const;
73 
78  HyperGraph::HyperGraphElement* construct(const std::string& tag, const HyperGraph::GraphElemBitset& elemsToConstruct) const;
79 
83  bool knowsTag(const std::string& tag, int* elementType = 0) const;
84 
86  const std::string& tag(const HyperGraph::HyperGraphElement* v) const;
87 
91  void fillKnownTypes(std::vector<std::string>& types) const;
92 
96  void printRegisteredTypes(std::ostream& os, bool comment = false) const;
97 
98  protected:
100  {
101  public:
105  {
106  creator = 0;
107  elementTypeBit = -1;
108  }
109 
111  {
112  std::cout << "Deleting " << (void*) creator << std::endl;
113 
114  delete creator;
115  }
116  };
117 
118  typedef std::map<std::string, CreatorInformation*> CreatorMap;
119  typedef std::map<std::string, std::string> TagLookup;
120  Factory();
121  ~Factory();
122 
123  CreatorMap _creator;
124  TagLookup _tagLookup;
125 
126  private:
128  };
129 
130  template<typename T>
132  {
133  public:
134  RegisterTypeProxy(const std::string& name) : _name(name)
135  {
136 #ifdef G2O_DEBUG_FACTORY
137  std::cout << __FUNCTION__ << ": Registering " << _name << " of type " << typeid(T).name() << std::endl;
138 #endif
140  }
141 
143  {
144 #ifdef G2O_DEBUG_FACTORY
145  std::cout << __FUNCTION__ << ": Unregistering " << _name << " of type " << typeid(T).name() << std::endl;
146 #endif
148  }
149 
150  private:
151  std::string _name;
152  };
153 
154 #if defined _MSC_VER && defined G2O_SHARED_LIBS
155 # define G2O_FACTORY_EXPORT __declspec(dllexport)
156 # define G2O_FACTORY_IMPORT __declspec(dllimport)
157 #else
158 # define G2O_FACTORY_EXPORT
159 # define G2O_FACTORY_IMPORT
160 #endif
161 
162  // These macros are used to automate registering types and forcing linkage
163 #define G2O_REGISTER_TYPE(name, classname) \
164  extern "C" void G2O_FACTORY_EXPORT g2o_type_##classname(void) {} \
165  static g2o::RegisterTypeProxy<classname> g_type_proxy_##classname(#name);
166 
167 #define G2O_USE_TYPE_BY_CLASS_NAME(classname) \
168  extern "C" void G2O_FACTORY_IMPORT g2o_type_##classname(void); \
169  static g2o::ForceLinker proxy_##classname(g2o_type_##classname);
170 
171 #define G2O_REGISTER_TYPE_GROUP(typeGroupName) \
172  extern "C" void G2O_FACTORY_EXPORT g2o_type_group_##typeGroupName(void) {}
173 
174 #define G2O_USE_TYPE_GROUP(typeGroupName) \
175  extern "C" void G2O_FACTORY_IMPORT g2o_type_group_##typeGroupName(void); \
176  static g2o::ForceLinker g2o_force_type_link_##typeGroupName(g2o_type_group_##typeGroupName);
177 }
178 
179 #endif
static Factory * instance()
return the instance
Definition: factory.cpp:61
TagLookup _tagLookup
reverse look-up, class name to tag
Definition: factory.h:124
std::bitset< HyperGraph::HGET_NUM_ELEMS > GraphElemBitset
Definition: hyper_graph.h:74
HyperGraph::HyperGraphElement * construct(const std::string &tag) const
Definition: factory.cpp:147
void fillKnownTypes(std::vector< std::string > &types) const
Definition: factory.cpp:166
std::string _name
Definition: factory.h:151
void printRegisteredTypes(std::ostream &os, bool comment=false) const
Definition: factory.cpp:192
templatized creator class which creates graph elements
Definition: creators.h:60
const std::string & tag(const HyperGraph::HyperGraphElement *v) const
return the TAG given a vertex
Definition: factory.cpp:157
static Factory * factoryInstance
Definition: factory.h:127
create vertices and edges based on TAGs in, for example, a file
Definition: factory.h:49
CreatorMap _creator
look-up map for the existing creators
Definition: factory.h:123
bool knowsTag(const std::string &tag, int *elementType=0) const
Definition: factory.cpp:173
RegisterTypeProxy(const std::string &name)
Definition: factory.h:134
void unregisterType(const std::string &tag)
Definition: factory.cpp:128
static void destroy()
free the instance
Definition: factory.cpp:186
std::map< std::string, CreatorInformation * > CreatorMap
Definition: factory.h:118
AbstractHyperGraphElementCreator * creator
Definition: factory.h:102
void registerType(const std::string &tag, AbstractHyperGraphElementCreator *c)
Definition: factory.cpp:73
Abstract interface for allocating HyperGraphElement.
Definition: creators.h:41
std::map< std::string, std::string > TagLookup
Definition: factory.h:119


orb_slam2_ros
Author(s):
autogenerated on Wed Apr 21 2021 02:53:05