Fix problem with method specifiers for bad_dynamic_boxed_cast exception. Add support for operators to Utility.hpp

This commit is contained in:
Jason Turner
2011-03-11 17:54:55 -07:00
parent d9a92a5148
commit 46a669dab1
6 changed files with 21 additions and 11 deletions

View File

@@ -21,7 +21,7 @@ namespace chaiscript
{ {
public: public:
bad_boxed_cast(const Type_Info &t_from, const std::type_info &t_to, bad_boxed_cast(const Type_Info &t_from, const std::type_info &t_to,
const std::string &what) const std::string &what) throw()
: from(t_from), to(&t_to), m_what(what) : from(t_from), to(&t_to), m_what(what)
{ {
} }

View File

@@ -21,7 +21,7 @@ namespace chaiscript
{ {
public: public:
bad_boxed_dynamic_cast(const Type_Info &t_from, const std::type_info &t_to, bad_boxed_dynamic_cast(const Type_Info &t_from, const std::type_info &t_to,
const std::string &t_what) const std::string &t_what) throw()
: bad_boxed_cast(t_from, t_to, t_what) : bad_boxed_cast(t_from, t_to, t_what)
{ {
} }
@@ -35,6 +35,8 @@ namespace chaiscript
: bad_boxed_cast(w) : bad_boxed_cast(w)
{ {
} }
virtual ~bad_boxed_dynamic_cast() throw() {}
}; };
} }

View File

@@ -53,10 +53,10 @@ namespace chaiscript
{ {
inline std::string class_name_translator(const std::string &t_name) inline std::string class_name_translator(const std::string &t_name)
{ {
size_t colon = t_name.find_last_of("::"); size_t colon = t_name.rfind("::");
if (colon != std::string::npos) if (colon != std::string::npos)
{ {
return t_name.substr(colon+1, std::string::npos); return t_name.substr(colon+2, std::string::npos);
} else { } else {
return t_name; return t_name;
} }
@@ -64,13 +64,16 @@ namespace chaiscript
inline std::string method_name_translator(const std::string &t_name) inline std::string method_name_translator(const std::string &t_name)
{ {
size_t colon = t_name.find_last_of("::"); size_t namestart = t_name.rfind("operator");
if (colon != std::string::npos) namestart = (namestart == std::string::npos)?0:namestart+strlen("operator");
if (namestart == 0)
{ {
return t_name.substr(colon+1, std::string::npos); namestart = t_name.rfind("::");
} else { namestart = (namestart == std::string::npos)?0:namestart+strlen("::");
return t_name; }
}
return t_name.substr(namestart, std::string::npos);
} }
} }
} }

View File

@@ -5,6 +5,8 @@
class TestBaseType class TestBaseType
{ {
public: public:
TestBaseType() {}
TestBaseType(int) {}
virtual ~TestBaseType() {} virtual ~TestBaseType() {}
virtual int func() { return 0; } virtual int func() { return 0; }
@@ -40,6 +42,7 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_test_mo
m->add(chaiscript::user_type<TestDerivedType>(), "TestDerivedType"); m->add(chaiscript::user_type<TestDerivedType>(), "TestDerivedType");
m->add(chaiscript::constructor<TestBaseType ()>(), "TestBaseType"); m->add(chaiscript::constructor<TestBaseType ()>(), "TestBaseType");
// m->add(chaiscript::constructor<TestBaseType (int)>(), "TestBaseType");
m->add(chaiscript::constructor<TestBaseType (const TestBaseType &)>(), "TestBaseType"); m->add(chaiscript::constructor<TestBaseType (const TestBaseType &)>(), "TestBaseType");
m->add(chaiscript::constructor<TestDerivedType ()>(), "TestDerivedType"); m->add(chaiscript::constructor<TestDerivedType ()>(), "TestDerivedType");

View File

@@ -1,6 +1,6 @@
load_module("test_module") load_module("test_module")
var t0 = TestBaseType(); var t0 = TestBaseType()
var t = TestDerivedType(); var t = TestDerivedType();
assert_equal(t0.func(), 0); assert_equal(t0.func(), 0);

View File

@@ -24,6 +24,7 @@ int main()
((function3)) ((function3))
((functionOverload)(std::string (Test::*)(double))) ((functionOverload)(std::string (Test::*)(double)))
((functionOverload)(std::string (Test::*)(int))) ((functionOverload)(std::string (Test::*)(int)))
((operator=))
); );
chaiscript::ChaiScript chai; chaiscript::ChaiScript chai;
@@ -32,6 +33,7 @@ int main()
&& chai.eval<std::string>("var t = Test(); t.functionOverload(1); ") == "int" && chai.eval<std::string>("var t = Test(); t.functionOverload(1); ") == "int"
&& chai.eval<std::string>("var t = Test(); t.functionOverload(1.1); ") == "double") && chai.eval<std::string>("var t = Test(); t.functionOverload(1.1); ") == "double")
{ {
chai.eval("t = Test();");
return EXIT_SUCCESS; return EXIT_SUCCESS;
} else { } else {
return EXIT_FAILURE; return EXIT_FAILURE;