g2o
Loading...
Searching...
No Matches
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 <iostream>
31#include <map>
32#include <memory>
33#include <string>
34
35#include "creators.h"
36#include "g2o/config.h"
37#include "g2o/stuff/misc.h" // ForceLinker for the macros
38#include "hyper_graph.h"
39
40// define to get some verbose output
41// #define G2O_DEBUG_FACTORY
42
43namespace g2o {
44
49 public:
51 static Factory* instance();
52
53 Factory(Factory const&) = delete;
54 Factory& operator=(Factory const&) = delete;
55
57 static void destroy();
58
62 void registerType(const std::string& tag,
63 const std::shared_ptr<AbstractHyperGraphElementCreator>& c);
64
68 void unregisterType(const std::string& tag);
69
73 HyperGraph::HyperGraphElement* construct(const std::string& tag) const;
74
81 const std::string& tag,
82 const HyperGraph::GraphElemBitset& elemsToConstruct) const;
83
87 bool knowsTag(const std::string& tag, int* elementType = 0) const;
88
90 const std::string& tag(const HyperGraph::HyperGraphElement* v) const;
91
95 void fillKnownTypes(std::vector<std::string>& types) const;
96
100 void printRegisteredTypes(std::ostream& os, bool comment = false) const;
101
102 protected:
104 public:
105 std::shared_ptr<AbstractHyperGraphElementCreator> creator;
106 int elementTypeBit = -1;
107 };
108
109 typedef std::map<std::string, std::unique_ptr<CreatorInformation>> CreatorMap;
110 typedef std::map<std::string, std::string> TagLookup;
111 Factory() = default;
112
115
116 private:
117 static std::unique_ptr<Factory> factoryInstance;
118};
119
120template <typename T>
122 public:
123 RegisterTypeProxy(const std::string& name) : _name(name) {
124#ifdef G2O_DEBUG_FACTORY
125 std::cout << __FUNCTION__ << ": Registering " << _name << " of type "
126 << typeid(T).name() << std::endl;
127#endif
130 }
131
132 private:
133 std::string _name;
134 std::shared_ptr<AbstractHyperGraphElementCreator> _creator;
135};
136
137} // namespace g2o
138
139#if defined _MSC_VER && defined G2O_SHARED_LIBS
140#define G2O_FACTORY_EXPORT __declspec(dllexport)
141#define G2O_FACTORY_IMPORT __declspec(dllimport)
142#else
143#define G2O_FACTORY_EXPORT
144#define G2O_FACTORY_IMPORT
145#endif
146
147// These macros are used to automate registering types and forcing linkage
148#define G2O_REGISTER_TYPE(name, classname) \
149 extern "C" void G2O_FACTORY_EXPORT g2o_type_##classname(void) {} \
150 static g2o::RegisterTypeProxy<classname> g_type_proxy_##classname(#name);
151
152#define G2O_USE_TYPE_BY_CLASS_NAME(classname) \
153 extern "C" void G2O_FACTORY_IMPORT g2o_type_##classname(void); \
154 static g2o::ForceLinker proxy_##classname(g2o_type_##classname);
155
156#define G2O_REGISTER_TYPE_GROUP(typeGroupName) \
157 extern "C" void G2O_FACTORY_EXPORT g2o_type_group_##typeGroupName(void) {}
158
159#define G2O_USE_TYPE_GROUP(typeGroupName) \
160 extern "C" void G2O_FACTORY_IMPORT g2o_type_group_##typeGroupName(void); \
161 static g2o::ForceLinker g2o_force_type_link_##typeGroupName( \
162 g2o_type_group_##typeGroupName);
163
164#endif
std::shared_ptr< AbstractHyperGraphElementCreator > creator
Definition factory.h:105
create vertices and edges based on TAGs in, for example, a file
Definition factory.h:48
Factory & operator=(Factory const &)=delete
CreatorMap _creator
look-up map for the existing creators
Definition factory.h:113
Factory()=default
static std::unique_ptr< Factory > factoryInstance
Definition factory.h:117
static Factory * instance()
return the instance
Definition factory.cpp:46
std::map< std::string, std::unique_ptr< CreatorInformation > > CreatorMap
Definition factory.h:109
TagLookup _tagLookup
reverse look-up, class name to tag
Definition factory.h:114
std::map< std::string, std::string > TagLookup
Definition factory.h:110
Factory(Factory const &)=delete
void registerType(const std::string &tag, const std::shared_ptr< AbstractHyperGraphElementCreator > &c)
Definition factory.cpp:58
templatized creator class which creates graph elements
Definition creators.h:58
std::bitset< HyperGraph::HGET_NUM_ELEMS > GraphElemBitset
Definition hyper_graph.h:70
RegisterTypeProxy(const std::string &name)
Definition factory.h:123
std::string _name
Definition factory.h:133
std::shared_ptr< AbstractHyperGraphElementCreator > _creator
Definition factory.h:134
#define G2O_CORE_API
some general case utility functions