Add C++ test for user defined conversion
This commit is contained in:
parent
7b42d5307a
commit
e85be6eb3d
@ -107,7 +107,7 @@ namespace chaiscript
|
|||||||
public:
|
public:
|
||||||
static Boxed_Value cast(const Boxed_Value &t_from)
|
static Boxed_Value cast(const Boxed_Value &t_from)
|
||||||
{
|
{
|
||||||
if (t_from.get_type_info().bare_equal(user_type<From>()))
|
if (t_from.get_type_info().bare_equal(chaiscript::user_type<From>()))
|
||||||
{
|
{
|
||||||
if (t_from.is_pointer())
|
if (t_from.is_pointer())
|
||||||
{
|
{
|
||||||
@ -157,7 +157,7 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Dynamic_Conversion_Impl()
|
Dynamic_Conversion_Impl()
|
||||||
: Type_Conversion_Base(user_type<Base>(), user_type<Derived>())
|
: Type_Conversion_Base(chaiscript::user_type<Base>(), chaiscript::user_type<Derived>())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,10 +173,11 @@ namespace chaiscript
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<typename Callable>
|
||||||
class Type_Conversion_Impl : public Type_Conversion_Base
|
class Type_Conversion_Impl : public Type_Conversion_Base
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Type_Conversion_Impl(Type_Info t_from, Type_Info t_to, std::function<Boxed_Value (const Boxed_Value)> t_func)
|
Type_Conversion_Impl(Type_Info t_from, Type_Info t_to, Callable t_func)
|
||||||
: Type_Conversion_Base(std::move(t_to), std::move(t_from)),
|
: Type_Conversion_Base(std::move(t_to), std::move(t_from)),
|
||||||
m_func(std::move(t_func))
|
m_func(std::move(t_func))
|
||||||
{
|
{
|
||||||
@ -194,7 +195,7 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::function<Boxed_Value (const Boxed_Value)> m_func;
|
Callable m_func;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -329,15 +330,25 @@ namespace chaiscript
|
|||||||
static_assert(std::is_polymorphic<Base>::value, "Base class must be polymorphic");
|
static_assert(std::is_polymorphic<Base>::value, "Base class must be polymorphic");
|
||||||
static_assert(std::is_polymorphic<Derived>::value, "Derived class must be polymorphic");
|
static_assert(std::is_polymorphic<Derived>::value, "Derived class must be polymorphic");
|
||||||
|
|
||||||
return std::shared_ptr<detail::Type_Conversion_Base>(new detail::Dynamic_Conversion_Impl<Base, Derived>());
|
return std::make_shared<detail::Dynamic_Conversion_Impl<Base, Derived>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
template<typename Callable>
|
||||||
Type_Conversion type_conversion(const Type_Info &t_from, const Type_Info &t_to,
|
Type_Conversion type_conversion(const Type_Info &t_from, const Type_Info &t_to,
|
||||||
const std::function<Boxed_Value (Boxed_Value)> &t_func)
|
const Callable &t_func)
|
||||||
{
|
{
|
||||||
return std::shared_ptr<detail::Type_Conversion_Base>(new detail::Type_Conversion_Impl(t_from, t_to, t_func));
|
return std::make_shared<detail::Type_Conversion_Impl<Callable>>(t_from, t_to, t_func);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename From, typename To, typename Callable>
|
||||||
|
Type_Conversion type_conversion(const Callable &t_function)
|
||||||
|
{
|
||||||
|
auto func = [t_function](const Boxed_Value &t_bv) -> Boxed_Value {
|
||||||
|
// not even attempting to call boxed_cast so that we don't get caught in some call recursion
|
||||||
|
return chaiscript::Boxed_Value(t_function(detail::Cast_Helper<const From &>::cast(t_bv, nullptr)));
|
||||||
|
};
|
||||||
|
|
||||||
|
return std::make_shared<detail::Type_Conversion_Impl<decltype(func)>>(user_type<From>(), user_type<To>(), func);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
#include <chaiscript/chaiscript.hpp>
|
#include <chaiscript/chaiscript.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TestBaseType
|
class TestBaseType
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -22,6 +24,23 @@ class TestBaseType
|
|||||||
TestBaseType &operator=(const TestBaseType &);
|
TestBaseType &operator=(const TestBaseType &);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class Type2
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Type2(TestBaseType t_bt)
|
||||||
|
: m_bt(std::move(t_bt))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_val() const
|
||||||
|
{
|
||||||
|
return m_bt.val;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
TestBaseType m_bt;
|
||||||
|
};
|
||||||
|
|
||||||
enum TestEnum
|
enum TestEnum
|
||||||
{
|
{
|
||||||
TestValue1 = 1
|
TestValue1 = 1
|
||||||
@ -95,6 +114,7 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_test_mo
|
|||||||
m->add(chaiscript::user_type<TestBaseType>(), "TestBaseType");
|
m->add(chaiscript::user_type<TestBaseType>(), "TestBaseType");
|
||||||
m->add(chaiscript::user_type<TestDerivedType>(), "TestDerivedType");
|
m->add(chaiscript::user_type<TestDerivedType>(), "TestDerivedType");
|
||||||
m->add(chaiscript::user_type<TestMoreDerivedType>(), "TestMoreDerivedType");
|
m->add(chaiscript::user_type<TestMoreDerivedType>(), "TestMoreDerivedType");
|
||||||
|
m->add(chaiscript::user_type<Type2>(), "Type2");
|
||||||
|
|
||||||
m->add(chaiscript::constructor<TestBaseType ()>(), "TestBaseType");
|
m->add(chaiscript::constructor<TestBaseType ()>(), "TestBaseType");
|
||||||
// m->add(chaiscript::constructor<TestBaseType (int)>(), "TestBaseType");
|
// m->add(chaiscript::constructor<TestBaseType (int)>(), "TestBaseType");
|
||||||
@ -135,7 +155,9 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_test_mo
|
|||||||
m->add(chaiscript::fun(&to_int), "to_int");
|
m->add(chaiscript::fun(&to_int), "to_int");
|
||||||
m->add(chaiscript::fun(&TestBaseType::constMe), "constMe");
|
m->add(chaiscript::fun(&TestBaseType::constMe), "constMe");
|
||||||
|
|
||||||
|
m->add(chaiscript::type_conversion<TestBaseType, Type2>([](const TestBaseType &t_bt) { return Type2(t_bt); }));
|
||||||
|
|
||||||
|
m->add(chaiscript::fun(&Type2::get_val), "get_val");
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
9
unittests/user_defined_conversions_2.chai
Normal file
9
unittests/user_defined_conversions_2.chai
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
load_module("test_module")
|
||||||
|
|
||||||
|
auto t := TestBaseType();
|
||||||
|
|
||||||
|
// This uses the TestBaseType to Type2 user type
|
||||||
|
// conversion which was added in the module and then calls
|
||||||
|
// "get_val()" which exists on the Type2 type
|
||||||
|
assert_equal(t.get_val(), 10);
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user