diff --git a/src/emu/emucore.c b/src/emu/emucore.c index feea859693e..d4c970223fe 100644 --- a/src/emu/emucore.c +++ b/src/emu/emucore.c @@ -9,3 +9,19 @@ Visit http://mamedev.org for licensing and usage restrictions. ****************************************************************************/ + +#include "emu.h" + +void bitch_bad_cast(const std::type_info &src_type, const std::type_info &dst_type) +{ + fprintf(stderr, "Error: bad downcast<> or device<>. Tried to convert a %s to a %s, which are incompatible.\n", + src_type.name(), dst_type.name()); + abort(); +} + +void bitch_bad_device_cast(const device_t *dev, const std::type_info &dst_type) +{ + fprintf(stderr, "Error: bad downcast<> or device<>. Tried to convert the device %s of type %s to a %s, which are incompatible.\n", + dev->tag(), dev->name(), dst_type.name()); + abort(); +} diff --git a/src/emu/emucore.h b/src/emu/emucore.h index b3104c6167b..831b360a94d 100644 --- a/src/emu/emucore.h +++ b/src/emu/emucore.h @@ -332,19 +332,40 @@ private: // CASTING TEMPLATES //************************************************************************** +class device_t; + +void bitch_bad_cast(const std::type_info &src_type, const std::type_info &dst_type); +void bitch_bad_device_cast(const device_t *dev, const std::type_info &dst_type); + // template function for casting from a base class to a derived class that is checked // in debug builds and fast in release builds template inline _Dest downcast(_Source *src) { - assert(dynamic_cast<_Dest>(src) == src); +#ifndef NDEBUG + if(dynamic_cast<_Dest>(src) != src) { + fprintf(stderr, "buh\n"); + if(dynamic_cast(src)) + bitch_bad_device_cast(dynamic_cast(src), typeid(_Dest)); + else + bitch_bad_cast(typeid(src), typeid(_Dest)); + } +#endif return static_cast<_Dest>(src); } template inline _Dest downcast(_Source &src) { - assert(&dynamic_cast<_Dest>(src) == &src); +#ifndef NDEBUG + if(&dynamic_cast<_Dest>(src) != &src) { + fprintf(stderr, "gah\n"); + if(dynamic_cast(&src)) + bitch_bad_device_cast(dynamic_cast(&src), typeid(_Dest)); + else + bitch_bad_cast(typeid(src), typeid(_Dest)); + } +#endif return static_cast<_Dest>(src); }