mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2025-12-06 16:57:04 +08:00
Fix/enhance unique_ptr support
This commit is contained in:
parent
914bca6295
commit
077c93ab27
@ -167,6 +167,17 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// 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<const 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
|
/// Cast_Helper_Inner for casting to a std::shared_ptr<> type
|
||||||
template<typename Result>
|
template<typename Result>
|
||||||
|
|||||||
@ -35,11 +35,11 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
Data(const Type_Info &ti,
|
Data(const Type_Info &ti,
|
||||||
chaiscript::detail::Any to,
|
chaiscript::detail::Any to,
|
||||||
bool tr,
|
bool is_ref,
|
||||||
const void *t_void_ptr,
|
const void *t_void_ptr,
|
||||||
bool t_return_value)
|
bool t_return_value)
|
||||||
: m_type_info(ti), m_obj(std::move(to)), m_data_ptr(ti.is_const()?nullptr:const_cast<void *>(t_void_ptr)), m_const_data_ptr(t_void_ptr),
|
: m_type_info(ti), m_obj(std::move(to)), m_data_ptr(ti.is_const()?nullptr:const_cast<void *>(t_void_ptr)), m_const_data_ptr(t_void_ptr),
|
||||||
m_is_ref(tr), m_return_value(t_return_value)
|
m_is_ref(is_ref), m_return_value(t_return_value)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,7 +154,7 @@ namespace chaiscript
|
|||||||
return std::make_shared<Data>(
|
return std::make_shared<Data>(
|
||||||
detail::Get_Type_Info<T>::get(),
|
detail::Get_Type_Info<T>::get(),
|
||||||
chaiscript::detail::Any(std::make_shared<std::unique_ptr<T>>(std::move(obj))),
|
chaiscript::detail::Any(std::make_shared<std::unique_ptr<T>>(std::move(obj))),
|
||||||
false,
|
true,
|
||||||
ptr,
|
ptr,
|
||||||
t_return_value
|
t_return_value
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1113,6 +1113,9 @@ TEST_CASE("Use unique_ptr")
|
|||||||
chai.add(chaiscript::fun([](int &i){ ++i; }), "inci");
|
chai.add(chaiscript::fun([](int &i){ ++i; }), "inci");
|
||||||
chai.add(chaiscript::fun([](int i){ ++i; }), "copyi");
|
chai.add(chaiscript::fun([](int i){ ++i; }), "copyi");
|
||||||
chai.add(chaiscript::fun([](int *i){ ++(*i); }), "derefi");
|
chai.add(chaiscript::fun([](int *i){ ++(*i); }), "derefi");
|
||||||
|
chai.add(chaiscript::fun([](const std::unique_ptr<int> &i){ ++(*i); }), "constrefuniqptri");
|
||||||
|
chai.add(chaiscript::fun([](std::unique_ptr<int> &i){ ++(*i); }), "refuniqptri");
|
||||||
|
chai.add(chaiscript::fun([](std::unique_ptr<int> &&i){ ++(*i); }), "rvaluniqptri");
|
||||||
chai.add(chaiscript::var(std::make_unique<int>(1)), "iptr");
|
chai.add(chaiscript::var(std::make_unique<int>(1)), "iptr");
|
||||||
|
|
||||||
|
|
||||||
@ -1123,6 +1126,12 @@ TEST_CASE("Use unique_ptr")
|
|||||||
CHECK(chai.eval<int>("iptr") == 2);
|
CHECK(chai.eval<int>("iptr") == 2);
|
||||||
chai.eval("derefi(iptr)");
|
chai.eval("derefi(iptr)");
|
||||||
CHECK(chai.eval<int>("iptr") == 3);
|
CHECK(chai.eval<int>("iptr") == 3);
|
||||||
|
chai.eval("constrefuniqptri(iptr)");
|
||||||
|
CHECK(chai.eval<int>("iptr") == 4);
|
||||||
|
chai.eval("refuniqptri(iptr)");
|
||||||
|
CHECK(chai.eval<int>("iptr") == 5);
|
||||||
|
chai.eval("rvaluniqptri(iptr)");
|
||||||
|
CHECK(chai.eval<int>("iptr") == 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user