Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions Modules/Core/Common/include/itkExceptionObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
*
*=========================================================================*/
#ifndef itkExceptionObject_h
# error "Do not include itkExceptionObject.h directly, include itkMacro.h instead."
#else // itkExceptionObject_h
#define itkExceptionObject_h

# include "itkMacro.h"
// NOTE: This itkExceptionObject.h file is included by itkMacro.h, and should never be included directly.
#ifndef allow_inclusion_of_itkExceptionObject_h
# error "Do not include itkExceptionObject.h directly, include itkMacro.h instead."
#endif

# include <memory> // For shared_ptr.
# include <string>
# include <stdexcept>
#include <memory> // For shared_ptr.
#include <string>

namespace itk
{
Expand Down Expand Up @@ -241,6 +242,28 @@ class ITKCommon_EXPORT ProcessAborted : public ExceptionObject
/** \see LightObject::GetNameOfClass() */
itkOverrideGetNameOfClassMacro(ProcessAborted);
};
} // end namespace itk

// Forward declaration in Macro.h, implementation here to avoid circular dependency
template <typename TTarget, typename TSource>
TTarget
itkDynamicCastInDebugMode(TSource x)
{
#ifndef NDEBUG
if (x == nullptr)
{
return nullptr;
}
TTarget rval = dynamic_cast<TTarget>(x);
if (rval == nullptr)
{
itkGenericExceptionMacro("Failed dynamic cast to " << typeid(TTarget).name()
<< " object type = " << x->GetNameOfClass());
}
return rval;
#else
return static_cast<TTarget>(x);
#endif
}

} // end namespace itk
#endif // itkExceptionObject_h
38 changes: 5 additions & 33 deletions Modules/Core/Common/include/itkMacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -1340,39 +1340,6 @@ compilers.
} \
ITK_MACROEND_NOOP_STATEMENT


#define itkExceptionObject_h
#include "itkExceptionObject.h"
#undef itkExceptionObject_h

/** itkDynamicCastInDebugMode
* Use static_cast in Release builds, and dynamic_cast in Debug
*
* Note: this must come after:
*
* #include "itkExceptionObject.h"
*/
template <typename TTarget, typename TSource>
TTarget
itkDynamicCastInDebugMode(TSource x)
{
#ifndef NDEBUG
if (x == nullptr)
{
return nullptr;
}
TTarget rval = dynamic_cast<TTarget>(x);
if (rval == nullptr)
{
itkGenericExceptionMacro("Failed dynamic cast to " << typeid(TTarget).name()
<< " object type = " << x->GetNameOfClass());
}
return rval;
#else
return static_cast<TTarget>(x);
#endif
}

// ITK_FUTURE_DEPRECATED is only for internal use, within the implementation of ITK. It allows triggering "deprecated"
// warnings when legacy support is removed, which warn that a specific feature may be removed in the future.
#if defined(ITK_LEGACY_REMOVE) && !defined(ITK_LEGACY_SILENT)
Expand Down Expand Up @@ -1471,4 +1438,9 @@ itkDynamicCastInDebugMode(TSource x)
# define ITK_ITERATOR_FINAL /*purposefully empty for ITKv6, iterators are not virtual for performance reasons*/
#endif

#define allow_inclusion_of_itkExceptionObject_h
// Must include itkExceptionObject.h at the end of the file
// because it depends on the macros defined above
#include "itkExceptionObject.h"
#undef allow_inclusion_of_itkExceptionObject_h
#endif // itkMacro_h