Skip to content

Commit

Permalink
FIX: MCP_SAVE() with class enums
Browse files Browse the repository at this point in the history
  • Loading branch information
jlblancoc committed Nov 8, 2023
1 parent c4e09e2 commit aebf74b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions doc/source/doxygen-docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
- mrpt::maps::TSetOfMetricMapInitializers::loadFromConfigFile() now throws if it finds a `*_count` entry with an unknown map class name.
- \ref mrpt_math_grp
- New template mrpt::math::confidenceIntervalsFromHistogram()
- BUG FIXES:
- Fix compilation errors if using the `MCP_SAVE()` macro with class enum types.


# Version 2.11.2: Released Oct 25th, 2023
Expand Down
22 changes: 17 additions & 5 deletions libs/containers/include/mrpt/containers/yaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,22 @@ std::ostream& operator<<(std::ostream& o, const yaml& p);
MCP_LOAD_OPT(Yaml__, Var__); \
Var__ = mrpt::DEG2RAD(Var__)

namespace internal
{
// We need to implement this as a template for the "if constexpr()" false branch
// not to be evaluated for enums, which would lead to build errors.
template <typename T, typename YAML_T>
void impl_mcp_save(YAML_T& y, const T& var, const char* varName)
{
using enum_t = std::remove_cv_t<T>;

if constexpr (std::is_enum_v<enum_t>)
y[varName] = mrpt::typemeta::TEnumType<enum_t>::value2name(var);
else
y[varName] = var;
}
} // namespace internal

/** Macro to store a variable into a mrpt::containers::yaml (initials MCP)
* dictionary, using as "key" the name of the variable.
*
Expand All @@ -931,11 +947,7 @@ std::ostream& operator<<(std::ostream& o, const yaml& p);
* values. Note that this requires enums to implement mrpt::typemeta::TEnumType.
*/
#define MCP_SAVE(Yaml__, Var__) \
if constexpr (std::is_enum_v<decltype(Var__)>) \
Yaml__[#Var__] = mrpt::typemeta::TEnumType< \
std::remove_cv_t<decltype(Var__)>>::value2name(Var__); \
else \
Yaml__[#Var__] = Var__;
mrpt::containers::internal::impl_mcp_save(Yaml__, Var__, #Var__);

#define MCP_SAVE_DEG(Yaml__, Var__) Yaml__[#Var__] = mrpt::RAD2DEG(Var__);

Expand Down

0 comments on commit aebf74b

Please sign in to comment.