I'm trying to bind some binary operators in AngelScript. The operators are == and +, on std::string. I'm not quite sure if I've set them up right. These binary operators shouldn't have this pointers, so I tried them that way first. I tried them with this pointers, and they still don't work. Simply registering these behaviors - wether I use them or not - makes building fail.
Here is my binding code:
std::string CreateString(asUINT length, const char * str)
{
return std::string(str);
}
void ConstructString(std::string* str)
{
new (str) std::string();
}
void DestructString(std::string* str)
{
str->~basic_string();
}
std::string& AssignString(const std::string& src, std::string* dest)
{
*dest = src;
return *dest;
}
std::string AddString(const std::string& rhs, std::string* thisp)
{
return (*thisp)+rhs;
}
std::string& AddAssignString(const std::string& rhs, std::string* thisp)
{
*thisp += rhs;
return *thisp;
}
bool CompareStlString(const std::string& rhs, std::string* thisp)
{
return rhs == (*thisp);
}
void BindStlString(asIScriptEngine* engine)
{
engine->RegisterObjectType ("string", sizeof (std::string), asOBJ_CLASS_CDA);
engine->RegisterStringFactory ("string", asFUNCTION(CreateString), asCALL_CDECL);
engine->RegisterObjectBehaviour ("string", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructString), asCALL_CDECL_OBJLAST);
engine->RegisterObjectBehaviour ("string", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructString), asCALL_CDECL_OBJLAST);
engine->RegisterObjectBehaviour ("string", asBEHAVE_ASSIGNMENT, "string &f(const string&)", asFUNCTION(AssignString), asCALL_CDECL_OBJLAST);
engine->RegisterObjectBehaviour ("string", asBEHAVE_ADD, "string f(const string&)", asFUNCTION(AddString), asCALL_CDECL_OBJLAST);
engine->RegisterObjectBehaviour ("string", asBEHAVE_ADD_ASSIGN, "string& f(const string&)", asFUNCTION(AddAssignString), asCALL_CDECL_OBJLAST);
engine->RegisterObjectBehaviour ("string", asBEHAVE_EQUAL, "bool f(const string&)", asFUNCTION(CompareStlString), asCALL_CDECL_OBJLAST);
}
And my test script
//String test
void Test()
{
string one = "Hello ";
string two = "world";
string three = "!";
one += two;
if (three == "!") Print(one+three);
}
Also; I've never seen 'behavior' spelled with a u before. Is it a British spelling?