Reduce runtime calls into the new dynamic cast system by first making sure the type is polymorphic.

Cleanup some std::cout calls
This commit is contained in:
Jason Turner
2010-08-02 02:30:41 +00:00
parent edee892cad
commit 8be4aa08db
3 changed files with 34 additions and 13 deletions

View File

@@ -19,6 +19,7 @@
#include <boost/ref.hpp>
#include <boost/cstdint.hpp>
#include <boost/type_traits/add_const.hpp>
#include <boost/type_traits/is_polymorphic.hpp>
#include <boost/integer_traits.hpp>
namespace chaiscript
@@ -35,11 +36,18 @@ namespace chaiscript
try {
return detail::Cast_Helper<Type>::cast(bv);
} catch (const boost::bad_any_cast &) {
try {
// 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(boxed_dynamic_cast<Type>(bv));
} catch (const boost::bad_any_cast &) {
if (boost::is_polymorphic<typename Stripped_Type<Type>::type>::value)
{
try {
// 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(boxed_dynamic_cast<Type>(bv));
} catch (const boost::bad_any_cast &) {
throw 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
// attempted dynamic_cast
throw bad_boxed_cast(bv.get_type_info(), typeid(Type));
}
}

View File

@@ -5,6 +5,8 @@
#include "boxed_value.hpp"
#include "boxed_cast_helper.hpp"
#include "bad_boxed_cast.hpp"
#include <boost/static_assert.hpp>
#include <boost/type_traits/is_polymorphic.hpp>
namespace chaiscript
{
@@ -83,7 +85,6 @@ namespace chaiscript
return Boxed_Value(data);
} else {
std::cout << "performing boxed_dynamic_cast" << std::endl;
boost::shared_ptr<Base> data
= boost::dynamic_pointer_cast<Base>(detail::Cast_Helper<boost::shared_ptr<Derived> >::cast(derived));
@@ -92,11 +93,7 @@ namespace chaiscript
throw std::bad_cast();
}
std::cout << "typeinfo " << typeid(data).name() << std::endl;
Boxed_Value ret(data);
std::cout << " It worked " << ret.get_type_info().name() << std::endl;
return ret;
return Boxed_Value(data);
}
} else {
// Pull the reference out of the contained boxed value, which we know is the type we want
@@ -177,6 +174,12 @@ namespace chaiscript
template<typename Base, typename Derived>
void register_base_class()
{
//Can only be used with related polymorphic types
//may be expanded some day to support conversions other than child -> parent
BOOST_STATIC_ASSERT((boost::is_base_of<Base,Derived>::value));
BOOST_STATIC_ASSERT(boost::is_polymorphic<Base>::value);
BOOST_STATIC_ASSERT(boost::is_polymorphic<Derived>::value);
detail::Dynamic_Conversions::add_conversion<Base, Derived>();
}
@@ -194,8 +197,6 @@ namespace chaiscript
template<typename Base>
Boxed_Value boxed_dynamic_cast(const Boxed_Value &derived)
{
std::cout << " Attempting conversion from " << derived.get_type_info().bare_name() << " to "<< typeid(Base).name()
<< std::endl;
try {
return detail::Dynamic_Conversions::get_conversion(user_type<Base>(), derived.get_type_info())->convert(derived);
} catch (const std::out_of_range &) {

View File

@@ -135,6 +135,8 @@ namespace chaiscript
template<typename T>
struct Get_Type_Info
{
typedef T type;
static Type_Info get()
{
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
@@ -147,6 +149,8 @@ namespace chaiscript
template<typename T>
struct Get_Type_Info<boost::shared_ptr<T> >
{
typedef T type;
static Type_Info get()
{
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
@@ -159,6 +163,8 @@ namespace chaiscript
template<typename T>
struct Get_Type_Info<const boost::shared_ptr<T> &>
{
typedef T type;
static Type_Info get()
{
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
@@ -181,6 +187,12 @@ namespace chaiscript
};
}
template<typename T>
struct Stripped_Type
{
typedef typename Bare_Type<typename detail::Get_Type_Info<T>::type>::type type;
};
template<typename T>
Type_Info user_type(T)
{