🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Registering global opEquals

Started by
0 comments, last by WitchLord 9 years, 8 months ago

I have a C++ function that looks like this:


namespace MyNamespace
{
    inline bool operator==(const MyVec3& a, const MyVec3& b)
    {
        return ...; // left out for brevity
    }
}

MyVec3 is registered like this:


SetDefaultNamespace("MyNamespace");

RegisterObjectType("MyVec3",  sizeof(MyNamespace::MyVec3),  asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CDAK | asOBJ_APP_CLASS_ALLFLOATS);

I initially tried registering a global opEquals like so:


RegisterGlobalFunction("bool opEquals(const MyVec3&in, const MyVec3&in)", asFUNCTIONPR(MyNamespace::operator==, (const MyNamespace::MyVec3&,const MyNamespace::MyVec3&), bool), asCALL_CDECL)

which compiled, but at runtime I wasn't able to compare two MyVec3 instances as a == b

Then I tried registering the C++ function like so:


RegisterObjectMethod("MyVec3", "bool opEquals(const MyVec3& in) const", asFUNCTIONPR(MyNamespace::operator==, (const MyNamespace::MyVec3&,const MyNamespace::MyVec3&), bool), asCALL_CDECL_OBJLAST)

which so far is working fine.. but I'm slightly concerned that a asCALL_CDECL_OBJLAST calling convention would expect the C++ function to take a MyVec3*, but in this case the actual function takes a const MyVec3&. Is it just happening to work because under the hood a reference and a pointer are the same thing?

What's your advice for registering this C++ function with AngelScript?

Thanks!

Advertisement
A pointer and a reference in C++ are identical with regards to the ABI.

The only difference is at a higher level where the reference has the restriction that it is not expected to be null, neither can it be reassigned. But for the calling convention this has no importance.

You are correct in registering this function using RegisterObjectMethod and the asCALL_CDECL_OBJLAST calling convention.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement