MRPT  2.0.2
TMetricMapTypesRegistry.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "obs-precomp.h" // Precomp header
11 
12 #include <mrpt/maps/CMetricMap.h>
16 
17 using namespace std;
18 using namespace mrpt;
19 using namespace mrpt::maps;
20 using namespace mrpt::maps::internal;
21 
22 TMetricMapTypesRegistry& TMetricMapTypesRegistry::Instance()
23 {
24  static TMetricMapTypesRegistry reg;
25  return reg;
26 }
27 
28 static std::string stripNamespace(const std::string& n)
29 {
30  std::string ret = n;
31  const auto pos = ret.rfind("::");
32  if (pos != std::string::npos) ret = ret.substr(pos + 2);
33 
34  return ret;
35 }
36 
37 size_t TMetricMapTypesRegistry::doRegister(
38  const std::string& names, MapDefCtorFunctor func1,
40 {
41  std::vector<std::string> lstNames;
42  mrpt::system::tokenize(names, " \t\r\n,", lstNames);
43  for (const auto& lstName : lstNames)
44  {
45  const auto p = std::make_pair(func1, func2);
46  m_registry[lstName] = p;
47  // register also the version without the "mrpt::NS::" prefix, for
48  // backwards compatibility:
49  m_registry[stripNamespace(lstName)] = p;
50  }
51  return m_registry.size();
52 }
53 
55  TMetricMapTypesRegistry::factoryMapDefinition(
56  const std::string& className) const
57 {
58  // 1st attempt: full qualified name:
59  auto it = m_registry.find(className);
60  // 2nd attempt: without namespace prefix:
61  if (it == m_registry.end()) it = m_registry.find(stripNamespace(className));
62 
63  if (it == m_registry.end()) return nullptr;
64  ASSERT_(it->second.first);
65  return (it->second.first)();
66 }
67 
68 mrpt::maps::CMetricMap* TMetricMapTypesRegistry::factoryMapObjectFromDefinition(
69  const mrpt::maps::TMetricMapInitializer& mi) const
70 {
71  auto it = m_registry.find(mi.getMetricMapClassType()->className);
72  if (it == m_registry.end())
73  {
75  "[TMetricMapTypesRegistry] Error: Cannot create map of "
76  "unregistered map type '%s'",
77  mi.getMetricMapClassType()->className);
78  }
79 
80  ASSERT_(it->second.second);
81  mrpt::maps::CMetricMap* theMap = (it->second.second)(mi);
82 
83  // Common params for all maps:
85 
86  return theMap;
87 }
Virtual base for specifying the kind and parameters of one map (normally, to be inserted into mrpt::m...
STL namespace.
TMapGenericParams genericMapParams
Common params to all maps.
Definition: CMetricMap.h:274
const mrpt::rtti::TRuntimeClassId::Ptr & getMetricMapClassType() const
Query the map type (C++ class), as set by the factory method MapDefinition()
void tokenize(const std::string &inString, const std::string &inDelimiters, OUT_CONTAINER &outTokens, bool skipBlankTokens=true) noexcept
Tokenizes a string according to a set of delimiting characters.
mrpt::maps::TMapGenericParams genericMapParams
Common params for all maps: These are automatically set in TMetricMapTypesRegistry::factoryMapObjectF...
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
std::vector< string > names
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Declares a virtual base class for all metric maps storage classes.
Definition: CMetricMap.h:52
std::function< mrpt::maps::CMetricMap *(const mrpt::maps::TMetricMapInitializer &)> MapCtorFromDefFunctor
Class factory & registry for map classes.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
std::function< mrpt::maps::TMetricMapInitializer *(void)> MapDefCtorFunctor
static std::string stripNamespace(const std::string &n)



Page generated by Doxygen 1.8.14 for MRPT 2.0.2 Git: 9b4fd2465 Mon May 4 16:59:08 2020 +0200 at lun may 4 17:26:07 CEST 2020