fix ChaiScript#537

This commit is contained in:
Bernd Amend 2021-05-22 14:30:47 +02:00
parent 5ff3679811
commit cf7821cb1e
2 changed files with 53 additions and 6 deletions

View File

@ -22,6 +22,14 @@ namespace chaiscript
template<typename Class, typename ... Params > template<typename Class, typename ... Params >
Proxy_Function build_constructor_(Class (*)(Params...)) Proxy_Function build_constructor_(Class (*)(Params...))
{ {
if constexpr (!std::is_copy_constructible_v<Class>) {
auto call = [](auto && ... param) {
return std::make_shared<Class>(std::forward<decltype(param)>(param)...);
};
return Proxy_Function(
chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<std::shared_ptr<Class> (Params...), decltype(call)>>(call));
} else if constexpr (true) {
auto call = [](auto && ... param){ auto call = [](auto && ... param){
return Class(std::forward<decltype(param)>(param)...); return Class(std::forward<decltype(param)>(param)...);
}; };
@ -31,6 +39,7 @@ namespace chaiscript
} }
} }
} }
}
/// \brief Generates a constructor function for use with ChaiScript /// \brief Generates a constructor function for use with ChaiScript

View File

@ -1417,7 +1417,45 @@ TEST_CASE("Throw an exception when trying to add same conversion twice")
std::cout << "My_int type conversion 2\n"; std::cout << "My_int type conversion 2\n";
return my_int(x); return my_int(x);
})), chaiscript::exception::conversion_error); })), chaiscript::exception::conversion_error);
} }
TEST_CASE("Test if non copyable/movable types can be registered")
{
struct Noncopyable {
Noncopyable() {str = "test";}
Noncopyable(const Noncopyable&) = delete;
Noncopyable& operator=(const Noncopyable&) = delete;
std::string str;
};
struct Nonmovable {
Nonmovable() {str = "test";}
Nonmovable(Nonmovable&&) = delete;
Nonmovable& operator=(Nonmovable&&) = delete;
std::string str;
};
struct Nothing {
Nothing() {str = "test";}
Nothing(Nothing&&) = delete;
Nothing& operator=(Nothing&&) = delete;
Nothing(const Nothing&) = delete;
Nothing& operator=(const Nothing&) = delete;
std::string str;
};
chaiscript::ChaiScript chai;
chai.add(chaiscript::user_type<Noncopyable>(), "Noncopyable");
chai.add(chaiscript::constructor<Noncopyable()>(), "Noncopyable");
chai.add(chaiscript::user_type<Nonmovable>(), "Nonmovable");
chai.add(chaiscript::constructor<Nonmovable()>(), "Nonmovable");
chai.add(chaiscript::user_type<Nothing>(), "Nothing");
chai.add(chaiscript::constructor<Nothing()>(), "Nothing");
}