Skip to content

Commit

Permalink
ENH: Check thisClass argument of GetNameOfClass macro calls
Browse files Browse the repository at this point in the history
Asserted in both `itkVirtualGetNameOfClassMacro` and
`itkOverrideGetNameOfClassMacro` that `thisClass` is indeed the name of the
class that contains the macro call.

The assert helped finding three classes whose `GetNameOfClass()` member function
returned the wrong name.
  • Loading branch information
N-Dekker authored and hjmjohnson committed Jan 2, 2024
1 parent 4ee0617 commit d45127b
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions Modules/Core/Common/include/itkMacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#endif

#include <sstream>
#include <type_traits> // For is_same, remove_const, and remove_reference.

/** \namespace itk
* \brief The "itk" namespace contains all Insight Segmentation and
Expand Down Expand Up @@ -436,12 +437,20 @@ namespace itk
/** Macro's used to add `GetNameOfClass()` member functions to polymorphic ITK classes: `itkVirtualGetNameOfClassMacro`
* adds a virtual `GetNameOfClass()` member function to the class definition, and `itkOverrideGetNameOfClassMacro` adds
* a `GetNameOfClass()` override. */
#define itkVirtualGetNameOfClassMacro(thisClass) \
virtual const char * GetNameOfClass() const { return #thisClass; } \
#define itkVirtualGetNameOfClassMacro(thisClass) \
virtual const char * GetNameOfClass() const \
{ \
static_assert(std::is_same_v<thisClass, std::remove_const_t<std::remove_reference_t<decltype(*this)>>>); \
return #thisClass; \
} \
ITK_MACROEND_NOOP_STATEMENT

#define itkOverrideGetNameOfClassMacro(thisClass) \
const char * GetNameOfClass() const override { return #thisClass; } \
#define itkOverrideGetNameOfClassMacro(thisClass) \
const char * GetNameOfClass() const override \
{ \
static_assert(std::is_same_v<thisClass, std::remove_const_t<std::remove_reference_t<decltype(*this)>>>); \
return #thisClass; \
} \
ITK_MACROEND_NOOP_STATEMENT

#ifdef ITK_FUTURE_LEGACY_REMOVE
Expand Down

0 comments on commit d45127b

Please sign in to comment.