mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2025-12-06 08:46:53 +08:00
Bypass the mutex problem when looking for conversion, automatic
test.
This commit is contained in:
parent
ecd6000d54
commit
27bee4a266
@ -385,7 +385,7 @@ namespace chaiscript
|
||||
void add_conversion(const std::shared_ptr<detail::Type_Conversion_Base> &conversion)
|
||||
{
|
||||
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
|
||||
if (has_conversion(conversion)) {
|
||||
if (find_bidir(conversion->to(), conversion->from()) != m_conversions.end()) {
|
||||
throw exception::conversion_error(conversion->to(), conversion->from(),
|
||||
"Trying to re-insert an existing conversion!");
|
||||
}
|
||||
@ -475,12 +475,6 @@ namespace chaiscript
|
||||
return find_bidir(to, from) != m_conversions.end();
|
||||
}
|
||||
|
||||
/// \brief has_conversion overloaded for Type_Conversion_Base parameter
|
||||
bool has_conversion(const std::shared_ptr<detail::Type_Conversion_Base> &conversion)
|
||||
{
|
||||
return has_conversion(conversion->to(), conversion->from());
|
||||
}
|
||||
|
||||
std::shared_ptr<detail::Type_Conversion_Base> get_conversion(const Type_Info &to, const Type_Info &from) const
|
||||
{
|
||||
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
|
||||
|
||||
@ -1354,4 +1354,23 @@ TEST_CASE("Test ability to get 'use' function from default construction")
|
||||
const auto use_function = chai.eval<std::function<chaiscript::Boxed_Value (const std::string &)>>("use");
|
||||
}
|
||||
|
||||
TEST_CASE("Throw an exception when trying to add one conversion twice")
|
||||
{
|
||||
struct my_int {
|
||||
int value;
|
||||
my_int(int val): value(val) {};
|
||||
};
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
chai.add(chaiscript::type_conversion<int, my_int>([](int x) {
|
||||
std::cout << "Foo type conversion 1\n";
|
||||
return my_int(x);
|
||||
}));
|
||||
CHECK_THROWS_AS(chai.add(chaiscript::type_conversion<int, my_int>([](int x) {
|
||||
std::cout << "Foo type conversion 2\n";
|
||||
return my_int(x);
|
||||
})), chaiscript::exception::conversion_error);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user