diff --git a/include/chaiscript/dispatchkit/type_conversions.hpp b/include/chaiscript/dispatchkit/type_conversions.hpp index 2580fb1..bc6049a 100644 --- a/include/chaiscript/dispatchkit/type_conversions.hpp +++ b/include/chaiscript/dispatchkit/type_conversions.hpp @@ -394,6 +394,19 @@ namespace chaiscript return std::make_shared>(user_type(), user_type(), func); } + template + Type_Conversion type_conversion() + { + static_assert(std::is_convertible::value, "Types are not automatically convertible"); + auto func = [](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 + auto to = To{detail::Cast_Helper::cast(t_bv, nullptr)}; + return chaiscript::Boxed_Value(std::move(to)); + }; + + return std::make_shared>(user_type(), user_type(), func); + } + } diff --git a/src/test_module.cpp b/src/test_module.cpp index 5dac6f4..9029360 100644 --- a/src/test_module.cpp +++ b/src/test_module.cpp @@ -28,7 +28,8 @@ class Type2 { public: Type2(TestBaseType t_bt) - : m_bt(std::move(t_bt)) + : m_bt(std::move(t_bt)), + m_str("Hello World") { } @@ -37,8 +38,14 @@ class Type2 return m_bt.val; } + const char *get_str() const + { + return m_str.c_str(); + } + private: TestBaseType m_bt; + std::string m_str; }; enum TestEnum @@ -158,6 +165,9 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_test_mo m->add(chaiscript::type_conversion([](const TestBaseType &t_bt) { return Type2(t_bt); })); m->add(chaiscript::fun(&Type2::get_val), "get_val"); + m->add(chaiscript::fun(&Type2::get_str), "get_str"); + m->add(chaiscript::type_conversion()); + return m; } diff --git a/unittests/user_defined_conversions_2.chai b/unittests/user_defined_conversions_2.chai index 8a349e1..1226ba1 100644 --- a/unittests/user_defined_conversions_2.chai +++ b/unittests/user_defined_conversions_2.chai @@ -7,3 +7,4 @@ auto t := TestBaseType(); // "get_val()" which exists on the Type2 type assert_equal(t.get_val(), 10); +assert_equal(t.get_str().size(), 11);