Merge pull request #453 from AlekMosingiewicz/error_on_double_conversion

Error on double conversion
This commit is contained in:
Jason Turner 2018-08-15 13:33:13 -06:00 committed by GitHub
commit c737f2419b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 1 deletions

View File

@ -30,6 +30,16 @@ namespace chaiscript
{
namespace exception
{
/// \brief Error thrown when there's a problem with type conversion
class conversion_error: public bad_boxed_cast
{
public:
conversion_error(const Type_Info t_to, const Type_Info t_from, const utility::Static_String what) noexcept
: bad_boxed_cast(t_from, (*t_to.bare_type_info()), what), type_to(t_to) {};
Type_Info type_to;
};
class bad_boxed_dynamic_cast : public bad_boxed_cast
{
public:
@ -362,10 +372,14 @@ namespace chaiscript
return cache;
}
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);
/// \todo error if a conversion already exists
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!");
}
m_conversions.insert(conversion);
m_convertableTypes.insert({conversion->to().bare_type_info(), conversion->from().bare_type_info()});
m_num_types = m_convertableTypes.size();

View File

@ -1389,4 +1389,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 same 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 << "My_int type conversion 1\n";
return my_int(x);
}));
CHECK_THROWS_AS(chai.add(chaiscript::type_conversion<int, my_int>([](int x) {
std::cout << "My_int type conversion 2\n";
return my_int(x);
})), chaiscript::exception::conversion_error);
}