First cast up chain, if that fails, cast down

This commit is contained in:
Jason Turner
2014-03-26 10:52:56 -06:00
parent 56b036052f
commit 656b438002
3 changed files with 83 additions and 46 deletions

View File

@@ -84,11 +84,18 @@ namespace chaiscript
if (boost::is_polymorphic<typename detail::Stripped_Type<Type>::type>::value && t_conversions)
{
try {
std::cout << "trying an up conversion " << typeid(Type).name() << std::endl;
// We will not catch any bad_boxed_dynamic_cast that is thrown, let the user get it
// either way, we are not responsible if it doesn't work
return detail::Cast_Helper<Type>::cast(t_conversions->boxed_dynamic_cast<Type>(bv), t_conversions);
} catch (const boost::bad_any_cast &) {
throw exception::bad_boxed_cast(bv.get_type_info(), typeid(Type));
} catch (...) {
try {
std::cout << "trying a down conversion " << typeid(Type).name() << std::endl;
// try going the other way - down the inheritance graph
return detail::Cast_Helper<Type>::cast(t_conversions->boxed_dynamic_down_cast<Type>(bv), t_conversions);
} catch (const boost::bad_any_cast &) {
throw exception::bad_boxed_cast(bv.get_type_info(), typeid(Type));
}
}
} else {
// If it's not polymorphic, just throw the error, don't waste the time on the