Factory.cpp
Go to the documentation of this file.
2 
4 
5 namespace lanelet {
6 namespace io_handlers {
7 namespace {
8 std::string format(const std::vector<std::string>& strings, const std::string& delim = ", ") {
9  std::string formatted;
10  for (const auto& str : strings) {
11  if (!formatted.empty()) {
12  formatted += delim;
13  }
14  formatted += str;
15  }
16  return formatted;
17 }
18 } // namespace
19 
20 //===============================================================================================================
21 // CLASS PARSER FACTORY
22 //===============================================================================================================
23 Parser::Ptr ParserFactory::create(const std::string& parserName, const Projector& projector,
24  const io::Configuration& config) {
26  auto it = inst.registry_.find(parserName);
27  if (it != inst.registry_.end()) {
28  auto newObj = std::shared_ptr<Parser>(it->second(projector, config));
29  return newObj;
30  }
31  throw UnsupportedIOHandlerError("Requested parser " + parserName +
32  " does not exist! Available parsers are: " + format(availableParsers()));
33 }
34 
35 Parser::Ptr ParserFactory::createFromExtension(const std::string& extension, const Projector& projector,
36  const io::Configuration& config) {
38  auto it = inst.extensionRegistry_.find(extension);
39  if (it != inst.extensionRegistry_.end()) {
40  auto newObj = std::shared_ptr<Parser>(it->second(projector, config));
41  return newObj;
42  }
43  throw UnsupportedExtensionError("Requested extension " + extension +
44  " is not supported! Supported extensions are: " + format(availableExtensions()));
45 }
46 
47 std::vector<std::string> ParserFactory::availableParsers() {
48  std::vector<std::string> parsers;
49  for (const auto& parser : ParserFactory::instance().registry_) {
50  parsers.push_back(parser.first);
51  }
52  std::sort(parsers.begin(), parsers.end());
53  return parsers;
54 }
55 
56 std::vector<std::string> ParserFactory::availableExtensions() {
57  std::vector<std::string> extensions;
58  for (const auto& parser : ParserFactory::instance().extensionRegistry_) {
59  extensions.push_back(parser.first);
60  }
61  std::sort(extensions.begin(), extensions.end());
62  return extensions;
63 }
64 
66  static ParserFactory factory;
67  return factory;
68 }
69 
70 void ParserFactory::registerParser(const std::string& strategy, const std::string& extension,
71  const ParserCreationFcn& factoryFunction) {
72  registry_[strategy] = factoryFunction;
73  if (!extension.empty()) {
74  extensionRegistry_[extension] = factoryFunction;
75  }
76 }
77 
78 //===============================================================================================================
79 // CLASS WRITER FACTORY
80 //===============================================================================================================
81 Writer::Ptr WriterFactory::create(const std::string& writerName, const lanelet::Projector& projector,
82  const io::Configuration& config) {
84  auto it = inst.registry_.find(writerName);
85  if (it != inst.registry_.end()) {
86  auto newObj = std::shared_ptr<Writer>(it->second(projector, config));
87  return newObj;
88  }
89  throw UnsupportedIOHandlerError("Requested writer " + writerName +
90  " does not exist! Available writer are: " + format(availableWriters()));
91 }
92 
93 Writer::Ptr WriterFactory::createFromExtension(const std::string& extension, const Projector& projector,
94  const io::Configuration& config) {
96  auto it = inst.extensionRegistry_.find(extension);
97  if (it != inst.extensionRegistry_.end()) {
98  auto newObj = std::shared_ptr<Writer>(it->second(projector, config));
99  return newObj;
100  }
101  throw UnsupportedExtensionError("Requested extension " + extension +
102  " is not supported! Supported extensions are: " + format(availableExtensions()));
103 }
104 
105 std::vector<std::string> WriterFactory::availableWriters() {
106  std::vector<std::string> writers;
107  for (const auto& writer : WriterFactory::instance().registry_) {
108  writers.push_back(writer.first);
109  }
110  std::sort(writers.begin(), writers.end());
111  return writers;
112 }
113 
114 std::vector<std::string> WriterFactory::availableExtensions() {
115  std::vector<std::string> extensions;
116  for (const auto& writer : WriterFactory::instance().extensionRegistry_) {
117  extensions.push_back(writer.first);
118  }
119  std::sort(extensions.begin(), extensions.end());
120  return extensions;
121 }
122 
124  static WriterFactory factory;
125  return factory;
126 }
127 
128 void WriterFactory::registerWriter(const std::string& strategy, const std::string& extension,
129  const WriterCreationFcn& factoryFunction) {
130  registry_[strategy] = factoryFunction;
131  if (!extension.empty()) {
132  extensionRegistry_[extension] = factoryFunction;
133  }
134 }
135 } // namespace io_handlers
136 } // namespace lanelet
lanelet::io_handlers::WriterFactory::availableExtensions
static std::vector< std::string > availableExtensions()
returns all available extensions as vector
Definition: Factory.cpp:114
lanelet::io_handlers::WriterFactory::extensionRegistry_
std::map< std::string, WriterCreationFcn > extensionRegistry_
Definition: Factory.h:125
lanelet::io_handlers::WriterFactory::registry_
std::map< std::string, WriterCreationFcn > registry_
Definition: Factory.h:124
lanelet::UnsupportedExtensionError
Error for an unsupported extension.
Definition: Exceptions.h:27
lanelet::io_handlers::ParserFactory::ParserCreationFcn
std::function< Parser *(const Projector &, const io::Configuration &)> ParserCreationFcn
Definition: Factory.h:21
lanelet::io_handlers::ParserFactory::availableParsers
static std::vector< std::string > availableParsers()
returns all available parsers as vector
Definition: Factory.cpp:47
lanelet::io_handlers::ParserFactory::createFromExtension
static Parser::Ptr createFromExtension(const std::string &extension, const Projector &projector, const io::Configuration &config=io::Configuration())
creates a matching parser for the given file extension
Definition: Factory.cpp:35
lanelet::io_handlers::ParserFactory
Factory class for all suppored lanelet map parsers.
Definition: Factory.h:19
lanelet
lanelet::io::Configuration
std::map< std::string, Attribute > Configuration
Definition: Configuration.h:8
lanelet::io_handlers::ParserFactory::registerParser
void registerParser(const std::string &strategy, const std::string &extension, const ParserCreationFcn &factoryFunction)
Definition: Factory.cpp:70
lanelet::io_handlers::WriterFactory::WriterCreationFcn
std::function< Writer *(const Projector &, const io::Configuration &)> WriterCreationFcn
Definition: Factory.h:77
lanelet::io_handlers::WriterFactory
Factory class for all supported lanelet map writers.
Definition: Factory.h:75
lanelet::io_handlers::ParserFactory::extensionRegistry_
std::map< std::string, ParserCreationFcn > extensionRegistry_
Definition: Factory.h:69
lanelet::io_handlers::WriterFactory::instance
static WriterFactory & instance()
Definition: Factory.cpp:123
lanelet::Projector
Definition: Projection.h:22
lanelet::io_handlers::ParserFactory::registry_
std::map< std::string, ParserCreationFcn > registry_
Definition: Factory.h:68
lanelet::io_handlers::WriterFactory::create
static Writer::Ptr create(const std::string &writerName, const Projector &projector, const io::Configuration &config=io::Configuration())
creates a writer that matches the given name.
Definition: Factory.cpp:81
lanelet::io_handlers::ParserFactory::instance
static ParserFactory & instance()
Definition: Factory.cpp:65
lanelet::io_handlers::WriterFactory::createFromExtension
static Writer::Ptr createFromExtension(const std::string &extension, const Projector &projector, const io::Configuration &config=io::Configuration())
creates a matching writer for the given file extension
Definition: Factory.cpp:93
lanelet::io_handlers::ParserFactory::create
static Parser::Ptr create(const std::string &parserName, const Projector &projector, const io::Configuration &config=io::Configuration())
creates a parser that matches the given name.
Definition: Factory.cpp:23
lanelet::io_handlers::Parser::Ptr
std::shared_ptr< Parser > Ptr
Definition: Parser.h:26
lanelet::UnsupportedIOHandlerError
Error thrown if an unsupported handler (parser/writer) has been specified.
Definition: Exceptions.h:35
lanelet::io_handlers::ParserFactory::availableExtensions
static std::vector< std::string > availableExtensions()
returns all available extensions as vector
Definition: Factory.cpp:56
lanelet::io_handlers::Writer::Ptr
std::shared_ptr< Writer > Ptr
Definition: Writer.h:26
lanelet::io_handlers::WriterFactory::registerWriter
void registerWriter(const std::string &strategy, const std::string &extension, const WriterCreationFcn &factoryFunction)
Definition: Factory.cpp:128
Factory.h
lanelet::io_handlers::WriterFactory::availableWriters
static std::vector< std::string > availableWriters()
returns all available writers as vector
Definition: Factory.cpp:105
Exceptions.h
writer
ToFileWriter & writer
Definition: OsmHandlerWrite.cpp:246


lanelet2_io
Author(s): Fabian Poggenhans
autogenerated on Thu Mar 6 2025 03:26:03