emucore: make downcast errors more useful [O. Galibert]

This commit is contained in:
Olivier Galibert 2012-08-12 10:48:59 +00:00
parent afe6d45126
commit e6bad2ec08
2 changed files with 39 additions and 2 deletions

View File

@ -9,3 +9,19 @@
Visit http://mamedev.org for licensing and usage restrictions. 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();
}

View File

@ -332,19 +332,40 @@ private:
// CASTING TEMPLATES // 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 // template function for casting from a base class to a derived class that is checked
// in debug builds and fast in release builds // in debug builds and fast in release builds
template<class _Dest, class _Source> template<class _Dest, class _Source>
inline _Dest downcast(_Source *src) 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<const device_t *>(src))
bitch_bad_device_cast(dynamic_cast<const device_t *>(src), typeid(_Dest));
else
bitch_bad_cast(typeid(src), typeid(_Dest));
}
#endif
return static_cast<_Dest>(src); return static_cast<_Dest>(src);
} }
template<class _Dest, class _Source> template<class _Dest, class _Source>
inline _Dest downcast(_Source &src) 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<const device_t *>(&src))
bitch_bad_device_cast(dynamic_cast<const device_t *>(&src), typeid(_Dest));
else
bitch_bad_cast(typeid(src), typeid(_Dest));
}
#endif
return static_cast<_Dest>(src); return static_cast<_Dest>(src);
} }