2

I'm trying to write a thin wrapper around AngelScript. I'm having trouble figuring out how to wrap around a particular struct.

Here is the struct definition for the struct I want to wrap, asSMethodPtr:

template <int N>
struct asSMethodPtr
{
    template<class M>
    static asSFuncPtr Convert(M Mthd)
    {
        // This version of the function should never be executed, nor compiled,
        // as it would mean that the size of the method pointer cannot be determined.

        int ERROR_UnsupportedMethodPtr[N-100];

        asSFuncPtr p;
        return p;
    }
};

Here is the definition for asSFuncPtr:

struct asSFuncPtr
{
    union
    {
        char dummy[25]; // largest known class method pointer
        struct {asFUNCTION_t func; char dummy[25-sizeof(asFUNCTION_t)];} f;
    } ptr;
    asBYTE flag; // 1 = generic, 2 = global func
};

Here is the code I've found (taken from the AngelBinder library) that allows me to 'wrap' it:

template<typename R> ClassExporter& method(std::string name, R (T::*func)())
{
    MethodClass mthd(name, Type<R>::toString(), asSMethodPtr< sizeof( void (T::*)() ) >::Convert( AS_METHOD_AMBIGUITY_CAST( R (T::*)()) (func) ));
    this->_methods.push(mthd);
    return *this;
}

Unfortunately, I have no idea what this code is doing...

What is T::* supposed to do? A pointer to the class type?

What is R (T::*func)()?

Any help appreciated...

Jarrett
  • 1,767
  • 25
  • 47
  • 1
    `R (T::*func)()` defines a variable as a function pointer named `func`, which is a member of class `T`, takes no parameters (other than the implicit `this`) and returns `R` – Dave Apr 27 '13 at 01:29
  • Does it actually compile? This will compile, as far as I can tell, only if `sizeof( void (T::*)() )` is greater than `100`... Hold on, is that C++ or this scripting language?.. And you don't have `T::*` - if you look closely it's `void (T::*)()`. – lapk Apr 27 '13 at 01:43

1 Answers1

2

T::* is a pointer-to-member. R (T::*func)() is a pointer to a member function that returns R and takes 0 parameters. For example:

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

    int x = 10;
};

int main()
{
    S s;

    int S::* ptr = &S::x;

    std::cout << s.*ptr; // 10

    int (S::*f_ptr)() = &S::f;

    std::cout << (s.*f_ptr)(); // 5
}

Read more here.

David G
  • 94,763
  • 41
  • 167
  • 253