Several tests and fixes related to type conversions added. Still more to go.

This commit is contained in:
Jason Turner
2010-10-08 15:18:58 +00:00
parent c3da778103
commit 3f87210dc5
5 changed files with 112 additions and 74 deletions

View File

@@ -50,33 +50,17 @@ namespace chaiscript
}
};
template<typename Result>
struct Cast_Helper_Inner<const Result> : Cast_Helper_Inner<Result>
{
};
/**
* Cast_Helper_Inner for casting to a const & type
*/
template<typename Result>
struct Cast_Helper_Inner<const Result &>
struct Cast_Helper_Inner<const Result &> : Cast_Helper_Inner<Result>
{
typedef typename boost::reference_wrapper<typename boost::add_const<Result>::type > Result_Type;
static Result_Type cast(const Boxed_Value &ob)
{
if (ob.is_ref())
{
if (!ob.get_type_info().is_const())
{
return boost::cref((boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get());
} else {
return boost::any_cast<boost::reference_wrapper<const Result> >(ob.get());
}
} else {
if (!ob.get_type_info().is_const())
{
return boost::cref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
} else {
return boost::cref(*(boost::any_cast<boost::shared_ptr<const Result> >(ob.get())));
}
}
}
};
/**
@@ -183,33 +167,27 @@ namespace chaiscript
* Cast_Helper_Inner for casting to a const boost::shared_ptr<> & type
*/
template<typename Result>
struct Cast_Helper_Inner<const boost::shared_ptr<Result> &>
struct Cast_Helper_Inner<const boost::shared_ptr<Result> > : Cast_Helper_Inner<boost::shared_ptr<Result> >
{
typedef typename boost::shared_ptr<Result> Result_Type;
static Result_Type cast(const Boxed_Value &ob)
{
return boost::any_cast<boost::shared_ptr<Result> >(ob.get());
}
};
template<typename Result>
struct Cast_Helper_Inner<const boost::shared_ptr<Result> &> : Cast_Helper_Inner<boost::shared_ptr<Result> >
{
};
/**
* Cast_Helper_Inner for casting to a const boost::shared_ptr<const> & type
*/
template<typename Result>
struct Cast_Helper_Inner<const boost::shared_ptr<const Result> &>
struct Cast_Helper_Inner<const boost::shared_ptr<const Result> > : Cast_Helper_Inner<boost::shared_ptr<const Result> >
{
typedef typename boost::shared_ptr<const Result> Result_Type;
};
static Result_Type cast(const Boxed_Value &ob)
{
if (!ob.get_type_info().is_const())
{
return boost::const_pointer_cast<const Result>(boost::any_cast<boost::shared_ptr<Result> >(ob.get()));
} else {
return boost::any_cast<boost::shared_ptr<const Result> >(ob.get());
}
}
template<typename Result>
struct Cast_Helper_Inner<const boost::shared_ptr<const Result> &> : Cast_Helper_Inner<boost::shared_ptr<const Result> >
{
};
@@ -232,16 +210,49 @@ namespace chaiscript
* Cast_Helper_Inner for casting to a const Boxed_Value & type
*/
template<>
struct Cast_Helper_Inner<const Boxed_Value &>
struct Cast_Helper_Inner<const Boxed_Value> : Cast_Helper_Inner<Boxed_Value>
{
typedef const Boxed_Value & Result_Type;
};
static Result_Type cast(const Boxed_Value &ob)
{
return ob;
}
template<>
struct Cast_Helper_Inner<const Boxed_Value &> : Cast_Helper_Inner<Boxed_Value>
{
};
/**
* Cast_Helper_Inner for casting to a boost::reference_wrapper type
*/
template<typename Result>
struct Cast_Helper_Inner<boost::reference_wrapper<Result> > : Cast_Helper_Inner<Result &>
{
};
template<typename Result>
struct Cast_Helper_Inner<const boost::reference_wrapper<Result> > : Cast_Helper_Inner<Result &>
{
};
template<typename Result>
struct Cast_Helper_Inner<const boost::reference_wrapper<Result> &> : Cast_Helper_Inner<Result &>
{
};
template<typename Result>
struct Cast_Helper_Inner<boost::reference_wrapper<const Result> > : Cast_Helper_Inner<const Result &>
{
};
template<typename Result>
struct Cast_Helper_Inner<const boost::reference_wrapper<const Result> > : Cast_Helper_Inner<const Result &>
{
};
template<typename Result>
struct Cast_Helper_Inner<const boost::reference_wrapper<const Result> & > : Cast_Helper_Inner<const Result &>
{
};
/**
* The exposed Cast_Helper object that by default just calls the Cast_Helper_Inner
*/

View File

@@ -242,28 +242,16 @@ namespace chaiscript
* Cast_Helper for converting from Boxed_Value to Boxed_POD_Value
*/
template<>
struct Cast_Helper<const Boxed_POD_Value &>
struct Cast_Helper<const Boxed_POD_Value &> : Cast_Helper<Boxed_POD_Value>
{
typedef Boxed_POD_Value Result_Type;
static Result_Type cast(const Boxed_Value &ob)
{
return Boxed_POD_Value(ob);
}
};
/**
* Cast_Helper for converting from Boxed_Value to Boxed_POD_Value
*/
template<>
struct Cast_Helper<const Boxed_POD_Value>
struct Cast_Helper<const Boxed_POD_Value> : Cast_Helper<Boxed_POD_Value>
{
typedef Boxed_POD_Value Result_Type;
static Result_Type cast(const Boxed_Value &ob)
{
return Boxed_POD_Value(ob);
}
};
}

View File

@@ -235,7 +235,7 @@ namespace chaiscript
}
}
boost::any get() const
const boost::any & get() const
{
return m_data->m_obj;
}
@@ -282,7 +282,7 @@ namespace chaiscript
template<typename T>
Boxed_Value const_var(T *t)
{
return Boxed_Value( const_cast<typename boost::add_const<T>::type>(t) );
return Boxed_Value( const_cast<typename boost::add_const<T>::type *>(t) );
}
template<typename T>

View File

@@ -177,6 +177,8 @@ namespace chaiscript
template<typename T>
struct Get_Type_Info<boost::reference_wrapper<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,
@@ -185,6 +187,20 @@ namespace chaiscript
&typeid(Bare_Type<T>::type));
}
};
template<typename T>
struct Get_Type_Info<const boost::reference_wrapper<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,
boost::is_void<T>::value,
&typeid(const boost::reference_wrapper<T> &),
&typeid(Bare_Type<T>::type));
}
};
}
template<typename T>

View File

@@ -13,9 +13,9 @@ bool run_test_type_conversion(const Boxed_Value &bv, bool expectedpass)
try {
To ret = chaiscript::boxed_cast<To>(bv);
use(ret);
} catch (const chaiscript::bad_boxed_cast &e) {
} catch (const chaiscript::bad_boxed_cast &/*e*/) {
if (expectedpass) {
std::cerr << "Failure in run_test_type_conversion: " << e.what() << std::endl;
// std::cerr << "Failure in run_test_type_conversion: " << e.what() << std::endl;
return false;
} else {
return true;
@@ -43,7 +43,10 @@ bool test_type_conversion(const Boxed_Value &bv, bool expectedpass)
if (!ret)
{
std::cerr << "Error with type conversion test. From: " << bv.get_type_info().name() << " To: " << typeid(To).name()
std::cerr << "Error with type conversion test. From: "
<< (bv.is_const()?(std::string("const ")):(std::string())) << bv.get_type_info().name()
<< " To: "
<< (boost::is_const<To>::value?(std::string("const ")):(std::string())) << typeid(To).name()
<< " test was expected to " << ((expectedpass)?(std::string("succeed")):(std::string("fail"))) << " but did not" << std::endl;
}
@@ -56,7 +59,7 @@ bool do_test(const Boxed_Value &bv, bool T, bool ConstT, bool TRef, bool ConstTR
bool ConstSharedPtrT, bool ConstSharedConstPtrT, bool ConstSharedPtrTRef, bool ConstSharedPtrTConstRef,
bool BoostRef, bool BoostConstRef, bool ConstBoostRef, bool ConstBoostConstRef,
bool ConstBoostRefRef, bool ConstBoostConstRefRef, bool PODValue,
bool ConstPODValue, bool PODValueRef, bool ConstPODValueRef)
bool ConstPODValue, bool ConstPODValueRef, bool TPtrConstRef, bool ConstTPtrConstRef)
{
bool passed = true;
passed &= test_type_conversion<Type>(bv, T);
@@ -75,8 +78,8 @@ bool do_test(const Boxed_Value &bv, bool T, bool ConstT, bool TRef, bool ConstTR
passed &= test_type_conversion<const boost::shared_ptr<const Type> >(bv, ConstSharedConstPtrT);
passed &= test_type_conversion<const boost::shared_ptr<Type> &>(bv, ConstSharedPtrTRef);
passed &= test_type_conversion<const boost::shared_ptr<const Type> &>(bv, ConstSharedPtrTConstRef);
// passed &= test_type_conversion<boost::reference_wrapper<Type> >(bv, BoostRef);
// passed &= test_type_conversion<boost::reference_wrapper<const Type> >(bv, BoostConstRef);
passed &= test_type_conversion<boost::reference_wrapper<Type> >(bv, BoostRef);
passed &= test_type_conversion<boost::reference_wrapper<const Type> >(bv, BoostConstRef);
passed &= test_type_conversion<boost::reference_wrapper<Type> &>(bv, false);
passed &= test_type_conversion<boost::reference_wrapper<const Type> &>(bv, false);
passed &= test_type_conversion<const boost::reference_wrapper<Type> >(bv, ConstBoostRef);
@@ -85,7 +88,7 @@ bool do_test(const Boxed_Value &bv, bool T, bool ConstT, bool TRef, bool ConstTR
passed &= test_type_conversion<const boost::reference_wrapper<const Type> &>(bv, ConstBoostConstRefRef);
passed &= test_type_conversion<Boxed_POD_Value>(bv, PODValue);
passed &= test_type_conversion<const Boxed_POD_Value>(bv, ConstPODValue);
passed &= test_type_conversion<Boxed_POD_Value &>(bv, PODValueRef);
passed &= test_type_conversion<Boxed_POD_Value &>(bv, false);
passed &= test_type_conversion<const Boxed_POD_Value &>(bv, ConstPODValueRef);
passed &= test_type_conversion<Boxed_POD_Value *>(bv, false);
passed &= test_type_conversion<const Boxed_POD_Value *>(bv, false);
@@ -93,8 +96,11 @@ bool do_test(const Boxed_Value &bv, bool T, bool ConstT, bool TRef, bool ConstTR
passed &= test_type_conversion<const Boxed_POD_Value *const>(bv, false);
passed &= test_type_conversion<Type *&>(bv, false);
passed &= test_type_conversion<const Type *&>(bv, false);
passed &= test_type_conversion<Type * const&>(bv, false);
passed &= test_type_conversion<const Type * const&>(bv, false);
passed &= test_type_conversion<Type * const&>(bv, TPtrConstRef);
passed &= test_type_conversion<const Type * const&>(bv, ConstTPtrConstRef);
passed &= test_type_conversion<Boxed_Value>(bv, true);
passed &= test_type_conversion<const Boxed_Value>(bv, true);
passed &= test_type_conversion<const Boxed_Value &>(bv, true);
return passed;
}
@@ -105,21 +111,38 @@ int main()
{
bool passed = true;
int i;
/*
bool T, bool ConstT, bool TRef, bool ConstTRef, bool TPtr,
bool ConstTPtr, bool TPtrConst, bool ConstTPtrConst, bool SharedPtrT, bool SharedConstPtrT,
bool ConstSharedPtrT, bool ConstSharedConstPtrT, bool ConstSharedPtrTRef, bool ConstSharedPtrTConstRef, bool BoostRef,
bool BoostConstRef, bool ConstBoostRef, bool ConstBoostConstRef, bool ConstBoostRefRef, bool ConstBoostConstRefRef,
bool PODValue, bool ConstPODValue, bool PODValueRef, bool ConstPODValueRef
bool PODValue, bool ConstPODValue, bool ConstPODValueRef
*/
int i = 5;
passed &= do_test<int>(var(i), true, true, true, true, true,
true, true, true, true, true,
true, true, true, true, true,
true, true, true, true, true,
true, true, true, true);
true, true, true, true, true);
passed &= do_test<int>(const_var(i), true, true, false, true, false,
true, false, true, false, true,
false, true, false, true, false,
true, false, true, false, true,
true, true, true, false, true);
passed &= do_test<int>(var(&i), true, true, true, true, true,
true, true, true, false, false,
false, false, false, false, true,
true, true, true, true, true,
true, true, true, true, true);
passed &= do_test<int>(const_var(&i), true, true, false, true, false,
true, false, true, false, false,
false, false, false, false, false,
true, false, true, false, true,
true, true, true, false, true);
if (passed)
{