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:
@@ -19,6 +19,7 @@
|
|||||||
#include <boost/ref.hpp>
|
#include <boost/ref.hpp>
|
||||||
#include <boost/cstdint.hpp>
|
#include <boost/cstdint.hpp>
|
||||||
#include <boost/type_traits/add_const.hpp>
|
#include <boost/type_traits/add_const.hpp>
|
||||||
|
#include <boost/type_traits/is_polymorphic.hpp>
|
||||||
#include <boost/integer_traits.hpp>
|
#include <boost/integer_traits.hpp>
|
||||||
|
|
||||||
namespace chaiscript
|
namespace chaiscript
|
||||||
@@ -35,11 +36,18 @@ namespace chaiscript
|
|||||||
try {
|
try {
|
||||||
return detail::Cast_Helper<Type>::cast(bv);
|
return detail::Cast_Helper<Type>::cast(bv);
|
||||||
} catch (const boost::bad_any_cast &) {
|
} catch (const boost::bad_any_cast &) {
|
||||||
try {
|
if (boost::is_polymorphic<typename Stripped_Type<Type>::type>::value)
|
||||||
// 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
|
try {
|
||||||
return detail::Cast_Helper<Type>::cast(boxed_dynamic_cast<Type>(bv));
|
// We will not catch any bad_boxed_dynamic_cast that is thrown, let the user get it
|
||||||
} catch (const boost::bad_any_cast &) {
|
// 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));
|
throw bad_boxed_cast(bv.get_type_info(), typeid(Type));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -5,6 +5,8 @@
|
|||||||
#include "boxed_value.hpp"
|
#include "boxed_value.hpp"
|
||||||
#include "boxed_cast_helper.hpp"
|
#include "boxed_cast_helper.hpp"
|
||||||
#include "bad_boxed_cast.hpp"
|
#include "bad_boxed_cast.hpp"
|
||||||
|
#include <boost/static_assert.hpp>
|
||||||
|
#include <boost/type_traits/is_polymorphic.hpp>
|
||||||
|
|
||||||
namespace chaiscript
|
namespace chaiscript
|
||||||
{
|
{
|
||||||
@@ -83,7 +85,6 @@ namespace chaiscript
|
|||||||
|
|
||||||
return Boxed_Value(data);
|
return Boxed_Value(data);
|
||||||
} else {
|
} else {
|
||||||
std::cout << "performing boxed_dynamic_cast" << std::endl;
|
|
||||||
boost::shared_ptr<Base> data
|
boost::shared_ptr<Base> data
|
||||||
= boost::dynamic_pointer_cast<Base>(detail::Cast_Helper<boost::shared_ptr<Derived> >::cast(derived));
|
= boost::dynamic_pointer_cast<Base>(detail::Cast_Helper<boost::shared_ptr<Derived> >::cast(derived));
|
||||||
|
|
||||||
@@ -92,11 +93,7 @@ namespace chaiscript
|
|||||||
throw std::bad_cast();
|
throw std::bad_cast();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "typeinfo " << typeid(data).name() << std::endl;
|
return Boxed_Value(data);
|
||||||
Boxed_Value ret(data);
|
|
||||||
std::cout << " It worked " << ret.get_type_info().name() << std::endl;
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Pull the reference out of the contained boxed value, which we know is the type we want
|
// 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>
|
template<typename Base, typename Derived>
|
||||||
void register_base_class()
|
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>();
|
detail::Dynamic_Conversions::add_conversion<Base, Derived>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,8 +197,6 @@ namespace chaiscript
|
|||||||
template<typename Base>
|
template<typename Base>
|
||||||
Boxed_Value boxed_dynamic_cast(const Boxed_Value &derived)
|
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 {
|
try {
|
||||||
return detail::Dynamic_Conversions::get_conversion(user_type<Base>(), derived.get_type_info())->convert(derived);
|
return detail::Dynamic_Conversions::get_conversion(user_type<Base>(), derived.get_type_info())->convert(derived);
|
||||||
} catch (const std::out_of_range &) {
|
} catch (const std::out_of_range &) {
|
||||||
|
@@ -135,6 +135,8 @@ namespace chaiscript
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
struct Get_Type_Info
|
struct Get_Type_Info
|
||||||
{
|
{
|
||||||
|
typedef T type;
|
||||||
|
|
||||||
static Type_Info get()
|
static Type_Info get()
|
||||||
{
|
{
|
||||||
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
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>
|
template<typename T>
|
||||||
struct Get_Type_Info<boost::shared_ptr<T> >
|
struct Get_Type_Info<boost::shared_ptr<T> >
|
||||||
{
|
{
|
||||||
|
typedef T type;
|
||||||
|
|
||||||
static Type_Info get()
|
static Type_Info get()
|
||||||
{
|
{
|
||||||
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
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>
|
template<typename T>
|
||||||
struct Get_Type_Info<const boost::shared_ptr<T> &>
|
struct Get_Type_Info<const boost::shared_ptr<T> &>
|
||||||
{
|
{
|
||||||
|
typedef T type;
|
||||||
|
|
||||||
static Type_Info get()
|
static Type_Info get()
|
||||||
{
|
{
|
||||||
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
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>
|
template<typename T>
|
||||||
Type_Info user_type(T)
|
Type_Info user_type(T)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user