Skip to content

Commit

Permalink
TSetOfMetricMapInitializers throws for unknown types
Browse files Browse the repository at this point in the history
  • Loading branch information
jlblancoc committed Nov 6, 2023
1 parent 5942cf8 commit cb7fd9c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 8 deletions.
5 changes: 3 additions & 2 deletions doc/source/doxygen-docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# Version 2.11.3: UNRELEASED
- Changes in libraries:
- \ref mrpt_core_grp
- Add the `[[nodiscard]]` attribute to all functions returning a value in `<mrpt/core/bits_math.h>`
- \ref mrpt_maps_grp
- mrpt::maps::COccupancyGridMap3D::insertObservation() now also handles mrpt::obs::CObservationPointCloud
- New virtual interface mrpt::maps::NearestNeighborsCapable, implemented in:
Expand All @@ -11,8 +13,7 @@
- mrpt::maps::COccupancyGridMap2D
- New virtual method mrpt::maps::CMetricMap::boundingBox()
- mrpt::maps::TMetricMapInitializer now returns `shared_ptr`s instead of plain pointers.
- \ref mrpt_core_grp
- Add the `[[nodiscard]]` attribute to all functions returning a value in `<mrpt/core/bits_math.h>`
- mrpt::maps::TSetOfMetricMapInitializers::loadFromConfigFile() now throws if it finds a `*_count` entry with an unknown map class name.

# Version 2.11.2: Released Oct 25th, 2023
- Changes in libraries:
Expand Down
16 changes: 16 additions & 0 deletions libs/config/include/mrpt/config/CConfigFileBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,26 @@ class CConfigFileBase
/** Returns a list with all the section names. */
virtual void getAllSections(std::vector<std::string>& sections) const = 0;

/** Returns, by value, a list with all the section names. */
std::vector<std::string> sections() const
{
std::vector<std::string> ret;
getAllSections(ret);
return ret;
}

/** Returs a list with all the keys into a section */
virtual void getAllKeys(
const std::string& section, std::vector<std::string>& keys) const = 0;

/** Returs, by value, a list with all the keys into a section */
std::vector<std::string> keys(const std::string& section) const
{
std::vector<std::string> keys;
getAllKeys(section, keys);
return keys;
}

/** Checks if a given section exists (name is case insensitive)
* \sa keyExists() */
bool sectionExists(const std::string& section_name) const;
Expand Down
8 changes: 4 additions & 4 deletions libs/config/src/CConfigFileMemory_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ TEST(CConfigFileMemory, readwrite)

TEST(CConfigFileMemory, Sections)
{
std::vector<std::string> sections;
mrpt::config::CConfigFileMemory second;
second.write("one", "name", "val");
second.write("two", "names", "value");
second.getAllSections(sections);

const std::vector<std::string> sections = second.sections();
EXPECT_EQ(2U, sections.size());
if (sections.size() == 2)
{ // avoid potential crash if fails
Expand All @@ -40,11 +40,11 @@ TEST(CConfigFileMemory, Sections)

TEST(CConfigFileMemory, Names)
{
std::vector<std::string> names;
mrpt::config::CConfigFileMemory third;
third.write("sec", "name", "val");
third.write("sec", "names", "value");
third.getAllKeys("sec", names);

const std::vector<std::string> names = third.keys("sec");
EXPECT_EQ(2U, names.size());
if (names.size() == 2)
{ // avoid potential crash if fails
Expand Down
47 changes: 45 additions & 2 deletions libs/maps/src/maps/CMultiMetricMap_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
+------------------------------------------------------------------------+ */

#include <gtest/gtest.h>
#include <mrpt/config/CConfigFileMemory.h>
#include <mrpt/maps/CMultiMetricMap.h>
#include <mrpt/maps/COccupancyGridMap2D.h>
#include <mrpt/maps/CSimplePointsMap.h>
Expand All @@ -21,7 +22,9 @@ TEST(CMultiMetricMapTests, isEmpty)
}
}

static mrpt::maps::CMultiMetricMap initializer1()
namespace
{
mrpt::maps::CMultiMetricMap initializer1()
{
mrpt::maps::TSetOfMetricMapInitializers map_inits;
{
Expand All @@ -42,13 +45,14 @@ static mrpt::maps::CMultiMetricMap initializer1()
}
}

static mrpt::maps::CMultiMetricMap initializer2()
mrpt::maps::CMultiMetricMap initializer2()
{
mrpt::maps::CMultiMetricMap m;
m.maps.push_back(mrpt::maps::COccupancyGridMap2D::Create());
m.maps.push_back(mrpt::maps::CSimplePointsMap::Create());
return m;
}
} // namespace

TEST(CMultiMetricMapTests, initializers)
{
Expand Down Expand Up @@ -102,3 +106,42 @@ TEST(CMultiMetricMapTests, moveOp)

EXPECT_EQ(m2.mapByClass<CSimplePointsMap>()->size(), 1U);
}

TEST(CMultiMetricMapTests, unknownMapType)
{
{
const mrpt::config::CConfigFileMemory cfg(R""""(
[map]
// Creation of maps:
occupancyGrid_count=1
[map_occupancyGrid_00_creationOpts]
min_x=-10
max_x= 10
min_y=-10
max_y= 10
)"""");

mrpt::maps::TSetOfMetricMapInitializers map_inits;
EXPECT_NO_THROW(map_inits.loadFromConfigFile(cfg, "map"));
EXPECT_EQ(map_inits.size(), 1UL);
}

{
const mrpt::config::CConfigFileMemory cfg(R""""(
[map]
// Creation of maps:
occupancyGrid_count=1
myUnregisteredMap_count=1
[map_occupancyGrid_00_creationOpts]
min_x=-10
max_x= 10
min_y=-10
max_y= 10
)"""");

mrpt::maps::TSetOfMetricMapInitializers map_inits;
EXPECT_ANY_THROW(map_inits.loadFromConfigFile(cfg, "map"));
}
}
14 changes: 14 additions & 0 deletions libs/obs/src/TMetricMapInitializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ void TSetOfMetricMapInitializers::loadFromConfigFile(

} // end for each map kind

// Check for unknown map types and throw an error:
for (const auto& s : ini.keys(sectionName))
{
auto p = s.find("_count");
if (p == std::string::npos) continue;
const auto className = s.substr(0, p);
if (allMapKinds.count(className) != 0) continue; // ok, it exists
THROW_EXCEPTION_FMT(
"Error: found INI section '%s' while parsing "
"TSetOfMetricMapInitializers, but there is no such registered "
"CMetricMap class '%s'",
s.c_str(), className.c_str());
}

MRPT_END
}

Expand Down

0 comments on commit cb7fd9c

Please sign in to comment.