From 3f87210dc5d7f59cf23c6219780fe9b268272c53 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Fri, 8 Oct 2010 15:18:58 +0000 Subject: [PATCH] Several tests and fixes related to type conversions added. Still more to go. --- .../dispatchkit/boxed_cast_helper.hpp | 103 ++++++++++-------- .../dispatchkit/boxed_pod_value.hpp | 16 +-- .../chaiscript/dispatchkit/boxed_value.hpp | 4 +- include/chaiscript/dispatchkit/type_info.hpp | 16 +++ unittests/boxed_cast_test.cpp | 47 ++++++-- 5 files changed, 112 insertions(+), 74 deletions(-) diff --git a/include/chaiscript/dispatchkit/boxed_cast_helper.hpp b/include/chaiscript/dispatchkit/boxed_cast_helper.hpp index 160ec7e..a0beb0a 100644 --- a/include/chaiscript/dispatchkit/boxed_cast_helper.hpp +++ b/include/chaiscript/dispatchkit/boxed_cast_helper.hpp @@ -50,33 +50,17 @@ namespace chaiscript } }; + template + struct Cast_Helper_Inner : Cast_Helper_Inner + { + }; + /** * Cast_Helper_Inner for casting to a const & type */ template - struct Cast_Helper_Inner + struct Cast_Helper_Inner : Cast_Helper_Inner { - typedef typename boost::reference_wrapper::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 >(ob.get())).get()); - } else { - return boost::any_cast >(ob.get()); - } - } else { - if (!ob.get_type_info().is_const()) - { - return boost::cref(*(boost::any_cast >(ob.get()))); - } else { - return boost::cref(*(boost::any_cast >(ob.get()))); - } - } - } }; /** @@ -183,33 +167,27 @@ namespace chaiscript * Cast_Helper_Inner for casting to a const boost::shared_ptr<> & type */ template - struct Cast_Helper_Inner &> + struct Cast_Helper_Inner > : Cast_Helper_Inner > { - typedef typename boost::shared_ptr Result_Type; - - static Result_Type cast(const Boxed_Value &ob) - { - return boost::any_cast >(ob.get()); - } }; + template + struct Cast_Helper_Inner &> : Cast_Helper_Inner > + { + }; + + /** * Cast_Helper_Inner for casting to a const boost::shared_ptr & type */ template - struct Cast_Helper_Inner &> + struct Cast_Helper_Inner > : Cast_Helper_Inner > { - typedef typename boost::shared_ptr Result_Type; + }; - static Result_Type cast(const Boxed_Value &ob) - { - if (!ob.get_type_info().is_const()) - { - return boost::const_pointer_cast(boost::any_cast >(ob.get())); - } else { - return boost::any_cast >(ob.get()); - } - } + template + struct Cast_Helper_Inner &> : Cast_Helper_Inner > + { }; @@ -232,16 +210,49 @@ namespace chaiscript * Cast_Helper_Inner for casting to a const Boxed_Value & type */ template<> - struct Cast_Helper_Inner + struct Cast_Helper_Inner : Cast_Helper_Inner { - typedef const Boxed_Value & Result_Type; + }; - static Result_Type cast(const Boxed_Value &ob) - { - return ob; - } + template<> + struct Cast_Helper_Inner : Cast_Helper_Inner + { }; + + /** + * Cast_Helper_Inner for casting to a boost::reference_wrapper type + */ + template + struct Cast_Helper_Inner > : Cast_Helper_Inner + { + }; + + template + struct Cast_Helper_Inner > : Cast_Helper_Inner + { + }; + + template + struct Cast_Helper_Inner &> : Cast_Helper_Inner + { + }; + + template + struct Cast_Helper_Inner > : Cast_Helper_Inner + { + }; + + template + struct Cast_Helper_Inner > : Cast_Helper_Inner + { + }; + + template + struct Cast_Helper_Inner & > : Cast_Helper_Inner + { + }; + /** * The exposed Cast_Helper object that by default just calls the Cast_Helper_Inner */ diff --git a/include/chaiscript/dispatchkit/boxed_pod_value.hpp b/include/chaiscript/dispatchkit/boxed_pod_value.hpp index 870a275..55552c7 100644 --- a/include/chaiscript/dispatchkit/boxed_pod_value.hpp +++ b/include/chaiscript/dispatchkit/boxed_pod_value.hpp @@ -242,28 +242,16 @@ namespace chaiscript * Cast_Helper for converting from Boxed_Value to Boxed_POD_Value */ template<> - struct Cast_Helper + struct Cast_Helper : Cast_Helper { - 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 + struct Cast_Helper : Cast_Helper { - typedef Boxed_POD_Value Result_Type; - - static Result_Type cast(const Boxed_Value &ob) - { - return Boxed_POD_Value(ob); - } }; } diff --git a/include/chaiscript/dispatchkit/boxed_value.hpp b/include/chaiscript/dispatchkit/boxed_value.hpp index 59584fa..2356ee1 100644 --- a/include/chaiscript/dispatchkit/boxed_value.hpp +++ b/include/chaiscript/dispatchkit/boxed_value.hpp @@ -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 Boxed_Value const_var(T *t) { - return Boxed_Value( const_cast::type>(t) ); + return Boxed_Value( const_cast::type *>(t) ); } template diff --git a/include/chaiscript/dispatchkit/type_info.hpp b/include/chaiscript/dispatchkit/type_info.hpp index eb16d91..6d07058 100644 --- a/include/chaiscript/dispatchkit/type_info.hpp +++ b/include/chaiscript/dispatchkit/type_info.hpp @@ -177,6 +177,8 @@ namespace chaiscript template struct Get_Type_Info > { + typedef T type; + static Type_Info get() { return Type_Info(boost::is_const::value, boost::is_reference::value, boost::is_pointer::value, @@ -185,6 +187,20 @@ namespace chaiscript &typeid(Bare_Type::type)); } }; + + template + struct Get_Type_Info &> + { + typedef T type; + + static Type_Info get() + { + return Type_Info(boost::is_const::value, boost::is_reference::value, boost::is_pointer::value, + boost::is_void::value, + &typeid(const boost::reference_wrapper &), + &typeid(Bare_Type::type)); + } + }; } template diff --git a/unittests/boxed_cast_test.cpp b/unittests/boxed_cast_test.cpp index d0b46cb..5862ccf 100644 --- a/unittests/boxed_cast_test.cpp +++ b/unittests/boxed_cast_test.cpp @@ -13,9 +13,9 @@ bool run_test_type_conversion(const Boxed_Value &bv, bool expectedpass) try { To ret = chaiscript::boxed_cast(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::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(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 >(bv, ConstSharedConstPtrT); passed &= test_type_conversion &>(bv, ConstSharedPtrTRef); passed &= test_type_conversion &>(bv, ConstSharedPtrTConstRef); -// passed &= test_type_conversion >(bv, BoostRef); -// passed &= test_type_conversion >(bv, BoostConstRef); + passed &= test_type_conversion >(bv, BoostRef); + passed &= test_type_conversion >(bv, BoostConstRef); passed &= test_type_conversion &>(bv, false); passed &= test_type_conversion &>(bv, false); passed &= test_type_conversion >(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 &>(bv, ConstBoostConstRefRef); passed &= test_type_conversion(bv, PODValue); passed &= test_type_conversion(bv, ConstPODValue); - passed &= test_type_conversion(bv, PODValueRef); + passed &= test_type_conversion(bv, false); passed &= test_type_conversion(bv, ConstPODValueRef); passed &= test_type_conversion(bv, false); passed &= test_type_conversion(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(bv, false); passed &= test_type_conversion(bv, false); passed &= test_type_conversion(bv, false); - passed &= test_type_conversion(bv, false); - passed &= test_type_conversion(bv, false); + passed &= test_type_conversion(bv, TPtrConstRef); + passed &= test_type_conversion(bv, ConstTPtrConstRef); + passed &= test_type_conversion(bv, true); + passed &= test_type_conversion(bv, true); + passed &= test_type_conversion(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(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(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(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(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) {