Massive simplification of boxed_cast. More planned
This commit is contained in:
parent
8d808f75c0
commit
6f0d02f158
@ -69,7 +69,7 @@ namespace chaiscript
|
|||||||
/// assert(i == 5);
|
/// assert(i == 5);
|
||||||
/// \endcode
|
/// \endcode
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
typename detail::Cast_Helper<Type>::Result_Type boxed_cast(const Boxed_Value &bv, const Type_Conversions_State *t_conversions = nullptr)
|
auto boxed_cast(const Boxed_Value &bv, const Type_Conversions_State *t_conversions = nullptr) -> decltype(detail::Cast_Helper<Type>::cast(bv, nullptr))
|
||||||
{
|
{
|
||||||
if (!t_conversions || bv.get_type_info().bare_equal(user_type<Type>()) || (t_conversions && !(*t_conversions)->convertable_type<Type>())) {
|
if (!t_conversions || bv.get_type_info().bare_equal(user_type<Type>()) || (t_conversions && !(*t_conversions)->convertable_type<Type>())) {
|
||||||
try {
|
try {
|
||||||
|
@ -29,21 +29,36 @@ namespace chaiscript
|
|||||||
throw std::runtime_error("Attempted to dereference null Boxed_Value");
|
throw std::runtime_error("Attempted to dereference null Boxed_Value");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const void *verify_type(const Boxed_Value &ob, const std::type_info &ti, const void *ptr) {
|
||||||
|
if (!ob.get_type_info().bare_equal_type_info(ti)) {
|
||||||
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
|
} else {
|
||||||
|
return throw_if_null(ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void *verify_type(const Boxed_Value &ob, const std::type_info &ti, void *ptr) {
|
||||||
|
if (ptr == nullptr || !ob.get_type_info().bare_equal_type_info(ti)) {
|
||||||
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
|
} else {
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
template<typename Result, typename = enable_if<Result>>
|
||||||
|
Result cast_helper_inner(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
/// Generic Cast_Helper_Inner, for casting to any type
|
/// Generic Cast_Helper_Inner, for casting to any type
|
||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper_Inner
|
struct Cast_Helper_Inner
|
||||||
{
|
{
|
||||||
typedef typename std::add_const<Result>::type Result_Type;
|
static Result cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
if (ob.get_type_info().bare_equal_type_info(typeid(Result)))
|
return *static_cast<const Result *>(verify_type(ob, typeid(Result), ob.get_const_ptr()));
|
||||||
{
|
|
||||||
auto p = throw_if_null(ob.get_const_ptr());
|
|
||||||
return *static_cast<const Result *>(p);
|
|
||||||
} else {
|
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -57,15 +72,9 @@ namespace chaiscript
|
|||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper_Inner<const Result *>
|
struct Cast_Helper_Inner<const Result *>
|
||||||
{
|
{
|
||||||
typedef const Result * Result_Type;
|
static auto cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
if (ob.get_type_info().bare_equal_type_info(typeid(Result)))
|
return static_cast<const Result *>(verify_type(ob, typeid(Result), ob.get_const_ptr()));
|
||||||
{
|
|
||||||
return static_cast<const Result *>(ob.get_const_ptr());
|
|
||||||
} else {
|
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -73,15 +82,9 @@ namespace chaiscript
|
|||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper_Inner<Result *>
|
struct Cast_Helper_Inner<Result *>
|
||||||
{
|
{
|
||||||
typedef Result * Result_Type;
|
static auto cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
if (!ob.get_type_info().is_const() && ob.get_type_info() == typeid(Result))
|
return static_cast<Result *>(verify_type(ob, typeid(Result), ob.get_ptr()));
|
||||||
{
|
|
||||||
return static_cast<Result *>(ob.get_ptr());
|
|
||||||
} else {
|
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -100,17 +103,9 @@ namespace chaiscript
|
|||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper_Inner<const Result &>
|
struct Cast_Helper_Inner<const Result &>
|
||||||
{
|
{
|
||||||
typedef const Result& Result_Type;
|
static const Result & cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
if (ob.get_type_info().bare_equal_type_info(typeid(Result)))
|
return *static_cast<const Result *>(verify_type(ob, typeid(Result), ob.get_const_ptr()));
|
||||||
{
|
|
||||||
auto p = throw_if_null(ob.get_const_ptr());
|
|
||||||
return *static_cast<const Result *>(p);
|
|
||||||
} else {
|
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -120,16 +115,9 @@ namespace chaiscript
|
|||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper_Inner<Result &>
|
struct Cast_Helper_Inner<Result &>
|
||||||
{
|
{
|
||||||
typedef Result& Result_Type;
|
static Result& cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
if (!ob.get_type_info().is_const() && ob.get_type_info().bare_equal_type_info(typeid(Result)))
|
return *static_cast<Result *>(verify_type(ob, typeid(Result), ob.get_ptr()));
|
||||||
{
|
|
||||||
return *(static_cast<Result *>(throw_if_null(ob.get_ptr())));
|
|
||||||
} else {
|
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -137,9 +125,7 @@ namespace chaiscript
|
|||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper_Inner<std::shared_ptr<Result> >
|
struct Cast_Helper_Inner<std::shared_ptr<Result> >
|
||||||
{
|
{
|
||||||
typedef std::shared_ptr<Result> Result_Type;
|
static auto cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
return ob.get().cast<std::shared_ptr<Result> >();
|
return ob.get().cast<std::shared_ptr<Result> >();
|
||||||
}
|
}
|
||||||
@ -149,9 +135,7 @@ namespace chaiscript
|
|||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper_Inner<std::shared_ptr<const Result> >
|
struct Cast_Helper_Inner<std::shared_ptr<const Result> >
|
||||||
{
|
{
|
||||||
typedef std::shared_ptr<const Result> Result_Type;
|
static auto cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
if (!ob.get_type_info().is_const())
|
if (!ob.get_type_info().is_const())
|
||||||
{
|
{
|
||||||
@ -177,10 +161,7 @@ namespace chaiscript
|
|||||||
struct Cast_Helper_Inner<std::shared_ptr<Result> &>
|
struct Cast_Helper_Inner<std::shared_ptr<Result> &>
|
||||||
{
|
{
|
||||||
static_assert(!std::is_const<Result>::value, "Non-const reference to std::shared_ptr<const T> is not supported");
|
static_assert(!std::is_const<Result>::value, "Non-const reference to std::shared_ptr<const T> is not supported");
|
||||||
|
static auto cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
typedef Boxed_Value::Sentinel<Result> Result_Type;
|
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
std::shared_ptr<Result> &res = ob.get().cast<std::shared_ptr<Result> >();
|
std::shared_ptr<Result> &res = ob.get().cast<std::shared_ptr<Result> >();
|
||||||
return ob.pointer_sentinel(res);
|
return ob.pointer_sentinel(res);
|
||||||
@ -204,9 +185,7 @@ namespace chaiscript
|
|||||||
template<>
|
template<>
|
||||||
struct Cast_Helper_Inner<Boxed_Value>
|
struct Cast_Helper_Inner<Boxed_Value>
|
||||||
{
|
{
|
||||||
typedef Boxed_Value Result_Type;
|
static Boxed_Value cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
return ob;
|
return ob;
|
||||||
}
|
}
|
||||||
@ -216,9 +195,7 @@ namespace chaiscript
|
|||||||
template<>
|
template<>
|
||||||
struct Cast_Helper_Inner<Boxed_Value &>
|
struct Cast_Helper_Inner<Boxed_Value &>
|
||||||
{
|
{
|
||||||
typedef std::reference_wrapper<Boxed_Value> Result_Type;
|
static std::reference_wrapper<Boxed_Value> cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
return std::ref(const_cast<Boxed_Value &>(ob));
|
return std::ref(const_cast<Boxed_Value &>(ob));
|
||||||
}
|
}
|
||||||
@ -272,9 +249,7 @@ namespace chaiscript
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
struct Cast_Helper
|
struct Cast_Helper
|
||||||
{
|
{
|
||||||
typedef typename Cast_Helper_Inner<T>::Result_Type Result_Type;
|
static auto cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions) -> decltype(Cast_Helper_Inner<T>::cast(ob, t_conversions))
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
|
|
||||||
{
|
{
|
||||||
return Cast_Helper_Inner<T>::cast(ob, t_conversions);
|
return Cast_Helper_Inner<T>::cast(ob, t_conversions);
|
||||||
}
|
}
|
||||||
|
@ -1009,9 +1009,7 @@ namespace chaiscript
|
|||||||
template<>
|
template<>
|
||||||
struct Cast_Helper<Boxed_Number>
|
struct Cast_Helper<Boxed_Number>
|
||||||
{
|
{
|
||||||
typedef Boxed_Number Result_Type;
|
static Boxed_Number cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
return Boxed_Number(ob);
|
return Boxed_Number(ob);
|
||||||
}
|
}
|
||||||
|
@ -424,7 +424,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
/// \brief casts an object while applying any Dynamic_Conversion available
|
/// \brief casts an object while applying any Dynamic_Conversion available
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
typename detail::Cast_Helper<Type>::Result_Type boxed_cast(const Boxed_Value &bv) const
|
auto boxed_cast(const Boxed_Value &bv) const -> decltype(chaiscript::boxed_cast<Type>(bv, nullptr))
|
||||||
{
|
{
|
||||||
Type_Conversions_State state(m_conversions, m_conversions.conversion_saves());
|
Type_Conversions_State state(m_conversions, m_conversions.conversion_saves());
|
||||||
return chaiscript::boxed_cast<Type>(bv, &state);
|
return chaiscript::boxed_cast<Type>(bv, &state);
|
||||||
|
@ -81,9 +81,7 @@ namespace chaiscript
|
|||||||
template<typename Signature>
|
template<typename Signature>
|
||||||
struct Cast_Helper<const std::function<Signature> &>
|
struct Cast_Helper<const std::function<Signature> &>
|
||||||
{
|
{
|
||||||
typedef std::function<Signature> Result_Type;
|
static std::function<Signature> cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
|
|
||||||
{
|
{
|
||||||
if (ob.get_type_info().bare_equal(user_type<Const_Proxy_Function>()))
|
if (ob.get_type_info().bare_equal(user_type<Const_Proxy_Function>()))
|
||||||
{
|
{
|
||||||
@ -98,9 +96,7 @@ namespace chaiscript
|
|||||||
template<typename Signature>
|
template<typename Signature>
|
||||||
struct Cast_Helper<std::function<Signature> >
|
struct Cast_Helper<std::function<Signature> >
|
||||||
{
|
{
|
||||||
typedef std::function<Signature> Result_Type;
|
static std::function<Signature> cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
|
|
||||||
{
|
{
|
||||||
if (ob.get_type_info().bare_equal(user_type<Const_Proxy_Function>()))
|
if (ob.get_type_info().bare_equal(user_type<Const_Proxy_Function>()))
|
||||||
{
|
{
|
||||||
@ -115,9 +111,7 @@ namespace chaiscript
|
|||||||
template<typename Signature>
|
template<typename Signature>
|
||||||
struct Cast_Helper<const std::function<Signature> >
|
struct Cast_Helper<const std::function<Signature> >
|
||||||
{
|
{
|
||||||
typedef std::function<Signature> Result_Type;
|
static std::function<Signature> cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
|
|
||||||
{
|
{
|
||||||
if (ob.get_type_info().bare_equal(user_type<Const_Proxy_Function>()))
|
if (ob.get_type_info().bare_equal(user_type<Const_Proxy_Function>()))
|
||||||
{
|
{
|
||||||
|
@ -690,7 +690,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
/// \brief casts an object while applying any Dynamic_Conversion available
|
/// \brief casts an object while applying any Dynamic_Conversion available
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
typename detail::Cast_Helper<Type>::Result_Type boxed_cast(const Boxed_Value &bv) const
|
auto boxed_cast(const Boxed_Value &bv) const -> decltype(m_engine.boxed_cast<Type>(bv))
|
||||||
{
|
{
|
||||||
return m_engine.boxed_cast<Type>(bv);
|
return m_engine.boxed_cast<Type>(bv);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user