passing class/function to asMETHOD

Started by
5 comments, last by WitchLord 10 years, 11 months ago

hey all

I'm writing a thin wrapper for Angelscript for my game.

I'm a little unsure how to actually 'wrap' the asMETHOD macro.

I've got something like this so far:


void AngelScript::registerClass(Class classObject, std::vector<Method> methods)
{
registerObjectType(classObject.name.c_str(), 0, asOBJ_REF);
registerObjectBehaviour(classObject.name.c_str(), asBEHAVE_FACTORY, classObject.factorySignature.c_str(), asFUNCTION(classObject.factoryPointer), asCALL_CDECL);
registerObjectBehaviour(classObject.name.c_str(), asBEHAVE_ADDREF, "void f()", asMETHOD(classObject.pointer, classObject.addRefMethodPointer), asCALL_THISCALL);
registerObjectBehaviour(classObject.name.c_str(), asBEHAVE_RELEASE, "void f()", asMETHOD(classObject.pointer, classObject.releaseRefMethodPointer), asCALL_THISCALL);


for ( Method m : methods)
{
registerMethod(classObject, m);
}
}
 

where Class and Method are defined as:


 struct Class {
std::string name;
std::string factorySignature;
void* pointer;
void* factoryPointer;
void* addRefMethodPointer;
void* releaseRefMethodPointer;
};


struct Method {
std::string name;
std::string signature;
void* pointer;
};
 

Unfortuantely, my compiler is giving me an error like this:


 src/common/as_wrapper/AngelScript.cpp: In member function ‘void as_wrapper::AngelScript::registerClass(as_wrapper::Class, std::vector<as_wrapper::Method>)’:
src/common/as_wrapper/AngelScript.cpp:100:160: error: template argument 1 is invalid
src/common/as_wrapper/AngelScript.cpp:101:165: error: template argument 1 is invalid
 

Where line 100 and 101 are these two lines:


...
registerObjectBehaviour(classObject.name.c_str(), asBEHAVE_ADDREF, "void f()", asMETHOD(classObject.pointer, classObject.addRefMethodPointer), asCALL_THISCALL);
registerObjectBehaviour(classObject.name.c_str(), asBEHAVE_RELEASE, "void f()", asMETHOD(classObject.pointer, classObject.releaseRefMethodPointer), asCALL_THISCALL);
...
 

I'm not sure how I would go about passing (whatever it is) I need to pass to the asMETHOD to get this sucker compiled.
Anyone have any ideas?
Advertisement
asMETHOD is a macro, which means that it works on the actual text that it's passed at compile time. You can't try to pass the values of variables to macros; the macro will try to use the name of the variable instead of the value. However, the result of the asMETHOD macro is an asSFuncPtr, which you can store if you want. You can also manually create an asSFuncPtr from a pointer to member function, but there isn't really much point to that.

Ahh that makes sense. I'll give wrapping asSFuncPtr a go.

Thanks SiCrane.

Ok, I'm trying to actually figure out how to do it...How do you create a asSMethodPtr struct object without the Macro...?

I've got this:



auto methodPtr = asSMethodPtr<sizeof(void (classObject.pointer)())>::Convert((void (classObject.pointer)())(classObject.addRefMethodPointer));
 

(Where 'classObject' is a struct of type 'Class' (defined in my first post))

But it doesn't compile, I get:


 src/common/as_wrapper/AngelScript.cpp:98:66: error: void value not ignored as it ought to besrc/common/as_wrapper/AngelScript.cpp:98:68: error: template argument 1 is invalid
src/common/as_wrapper/AngelScript.cpp:98:107: error: void value not ignored as it ought to be
 

Any ideas? I've afraid this asSMethodPtr struct is pushing the limits of my C++ abilities...

You are just storing void* and expecting C++ to call correct function.

C++ can not call the correct function just with a void*.

There was a project called AngelBinder, google it.

What you want is doable, but require advanced c++ templates knowledge.

Ah, right. Thanks saejox, AngelBinder was a big help.

Well, I guess it's pretty clear by now that I have no idea what I'm doing:

i_have_no_idea_what_i_m_doing.jpg

So, after more researching, and asking around a little on stackoverflow, I have something compiling.

However, now I seem to be unable to create a asSMethodPtr object...

I define the following struct:


struct S
{
    int f()
    {
        return 5;
    }


    int x = 10;
};
 

and use it thusly:


asSMethodPtr pt = asSMethodPtr<sizeof( void (S::*)() )>::Convert( AS_METHOD_AMBIGUITY_CAST( int (S::*)()) (&S::f) );
 

However, I get the following compile time exception:

template_tests.cpp: In function ‘int main()’:
template_tests.cpp:64:15: error: missing template arguments before ‘pt’
template_tests.cpp:64:15: error: expected ‘;’ before ‘pt’
Any idea why in the world I get that?
If I try to add an integer in there as a template, like so:


asSMethodPtr<16> pt = asSMethodPtr<sizeof( void (S::*)() )>::Convert( AS_METHOD_AMBIGUITY_CAST( int (S::*)()) (&S::f) );
 

I get this error instead:
template_tests.cpp: In function ‘int main()’:
template_tests.cpp:66:120: error: conversion from ‘asSFuncPtr’ to non-scalar type ‘asSMethodPtr<16>’ requested

asSFuncPtr and asMethodPtr are implemented in angelscript.h. The asMETHOD method instanciates the asMethodPtr template structure, and then call the method Convert() to return an asSFuncPtr.

I can understand you want to wrap the AngelScript library, but there is really no need to reimplement the code. Just take the code from angelscript.h and rename it to your liking rather than trying to implement it from zero.

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