Add support for r-value parameters and unique_ptr
Notes * Due to the limitations for how Boxed_Value is handled the unique_ptrs must still be wrapped in a shared_ptr * However, this caveat does not directly affect the user
This commit is contained in:
parent
d6d50478de
commit
fb7f8f194c
@ -135,6 +135,39 @@ namespace chaiscript
|
||||
}
|
||||
};
|
||||
|
||||
/// Cast_Helper_Inner for casting to a && type
|
||||
template<typename Result>
|
||||
struct Cast_Helper_Inner<Result &&>
|
||||
{
|
||||
static Result&& cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||
{
|
||||
return std::move(*static_cast<Result *>(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<typename Result>
|
||||
struct Cast_Helper_Inner<std::unique_ptr<Result> &&>
|
||||
{
|
||||
static std::unique_ptr<Result> &&cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||
{
|
||||
return std::move(*(ob.get().cast<std::shared_ptr<std::unique_ptr<Result>>>()));
|
||||
}
|
||||
};
|
||||
|
||||
/// 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<typename Result>
|
||||
struct Cast_Helper_Inner<std::unique_ptr<Result> &>
|
||||
{
|
||||
static std::unique_ptr<Result> &cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||
{
|
||||
return *(ob.get().cast<std::shared_ptr<std::unique_ptr<Result>>>());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/// Cast_Helper_Inner for casting to a std::shared_ptr<> type
|
||||
template<typename Result>
|
||||
struct Cast_Helper_Inner<std::shared_ptr<Result> >
|
||||
|
@ -119,6 +119,8 @@ namespace chaiscript
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename T>
|
||||
static auto get(T *t, bool t_return_value)
|
||||
{
|
||||
@ -145,6 +147,19 @@ namespace chaiscript
|
||||
);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static auto get(std::unique_ptr<T> &&obj, bool t_return_value)
|
||||
{
|
||||
auto ptr = obj.get();
|
||||
return std::make_shared<Data>(
|
||||
detail::Get_Type_Info<T>::get(),
|
||||
chaiscript::detail::Any(std::make_shared<std::unique_ptr<T>>(std::move(obj))),
|
||||
false,
|
||||
ptr,
|
||||
t_return_value
|
||||
);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static auto get(T t, bool t_return_value)
|
||||
{
|
||||
|
@ -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<int> &&)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
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<int>(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<int>(1)), "iptr");
|
||||
|
||||
|
||||
CHECK(chai.eval<int>("iptr") == 1);
|
||||
chai.eval("inci(iptr)");
|
||||
CHECK(chai.eval<int>("iptr") == 2);
|
||||
chai.eval("copyi(iptr)");
|
||||
CHECK(chai.eval<int>("iptr") == 2);
|
||||
chai.eval("derefi(iptr)");
|
||||
CHECK(chai.eval<int>("iptr") == 3);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user