diff --git a/include/chaiscript/dispatchkit/boxed_cast_helper.hpp b/include/chaiscript/dispatchkit/boxed_cast_helper.hpp index 999ccf3..4a0c308 100644 --- a/include/chaiscript/dispatchkit/boxed_cast_helper.hpp +++ b/include/chaiscript/dispatchkit/boxed_cast_helper.hpp @@ -135,6 +135,39 @@ namespace chaiscript } }; + /// Cast_Helper_Inner for casting to a && type + template + struct Cast_Helper_Inner + { + static Result&& cast(const Boxed_Value &ob, const Type_Conversions_State *) + { + return std::move(*static_cast(verify_type(ob, typeid(Result), ob.get_ptr()))); + } + }; + + /// Cast_Helper_Inner for casting to a std::unique_ptr<> && type + /// \todo Fix the fact that this has to be in a shared_ptr for now + template + struct Cast_Helper_Inner &&> + { + static std::unique_ptr &&cast(const Boxed_Value &ob, const Type_Conversions_State *) + { + return std::move(*(ob.get().cast>>())); + } + }; + + /// Cast_Helper_Inner for casting to a std::unique_ptr<> & type + /// \todo Fix the fact that this has to be in a shared_ptr for now + template + struct Cast_Helper_Inner &> + { + static std::unique_ptr &cast(const Boxed_Value &ob, const Type_Conversions_State *) + { + return *(ob.get().cast>>()); + } + }; + + /// Cast_Helper_Inner for casting to a std::shared_ptr<> type template struct Cast_Helper_Inner > diff --git a/include/chaiscript/dispatchkit/boxed_value.hpp b/include/chaiscript/dispatchkit/boxed_value.hpp index 16595cd..e6c669b 100644 --- a/include/chaiscript/dispatchkit/boxed_value.hpp +++ b/include/chaiscript/dispatchkit/boxed_value.hpp @@ -119,6 +119,8 @@ namespace chaiscript ); } + + template static auto get(T *t, bool t_return_value) { @@ -145,6 +147,19 @@ namespace chaiscript ); } + template + static auto get(std::unique_ptr &&obj, bool t_return_value) + { + auto ptr = obj.get(); + return std::make_shared( + detail::Get_Type_Info::get(), + chaiscript::detail::Any(std::make_shared>(std::move(obj))), + false, + ptr, + t_return_value + ); + } + template static auto get(T t, bool t_return_value) { diff --git a/unittests/compiled_tests.cpp b/unittests/compiled_tests.cpp index b86c55b..39ee06e 100644 --- a/unittests/compiled_tests.cpp +++ b/unittests/compiled_tests.cpp @@ -1083,6 +1083,47 @@ TEST_CASE("Test stdlib options") test_has_external_scripts(chai); test_no_load_modules(chai); } +} + + +void uservalueref(int &&) +{ +} + +void usemoveonlytype(std::unique_ptr &&) +{ +} + + +TEST_CASE("Pass r-value reference to func") +{ + chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser()); + + chai.add(chaiscript::fun(&uservalueref), "uservalueref"); + chai.add(chaiscript::fun(&usemoveonlytype), "usemoveonlytype"); + + chai.add(chaiscript::var(std::make_unique(1)), "iptr"); + chai.eval("usemoveonlytype(iptr)"); +} + +TEST_CASE("Use unique_ptr") +{ + chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser()); + + chai.add(chaiscript::fun([](int &i){ ++i; }), "inci"); + chai.add(chaiscript::fun([](int i){ ++i; }), "copyi"); + chai.add(chaiscript::fun([](int *i){ ++(*i); }), "derefi"); + chai.add(chaiscript::var(std::make_unique(1)), "iptr"); + + + CHECK(chai.eval("iptr") == 1); + chai.eval("inci(iptr)"); + CHECK(chai.eval("iptr") == 2); + chai.eval("copyi(iptr)"); + CHECK(chai.eval("iptr") == 2); + chai.eval("derefi(iptr)"); + CHECK(chai.eval("iptr") == 3); + }