Function Binding for Objects
Hi out there!
Perhaps you know the generic function binding interface from Scott Bilas (book: Game Programming Gems)
http://members.aa.net/~scottb/publish/gpgems1_fubi.htm
It works fine, but I''m trying to do the same for objects now and have some problems, with pointer to member functions.
So, has anyone implemented such an interface for member functions yet, or does anyone know another articles about that topic?
Bunnz
Have fun BunnzPunch'n'Crunch
Say you have the following class:
class A
{
public:
int f(char a, float b);
int g(char a, floar b);
};
The type of member function f is:
int (A::*)(char, float)
To make a pointer to a member function of a class, you can use the following typedef:
typedef int (A::*a_member_fn)(char x, float y);
a_member_fn is then a pointer to a member function of A that returns an int and has a char and float as parameters.
If a is an instance of class A, and pf is a pointer to a member function of A, then you can call the function like this:
pf = &A::f;
int r = (a.*pf)(''a'', 3.14);
pf = &A::g;
int r2 = (a.*pf)(''b'', 3.15);
HTH
class A
{
public:
int f(char a, float b);
int g(char a, floar b);
};
The type of member function f is:
int (A::*)(char, float)
To make a pointer to a member function of a class, you can use the following typedef:
typedef int (A::*a_member_fn)(char x, float y);
a_member_fn is then a pointer to a member function of A that returns an int and has a char and float as parameters.
If a is an instance of class A, and pf is a pointer to a member function of A, then you can call the function like this:
pf = &A::f;
int r = (a.*pf)(''a'', 3.14);
pf = &A::g;
int r2 = (a.*pf)(''b'', 3.15);
HTH
Some useful C++ links:Free multiplatform ANSI C++ Standard Library implementationVisual C++ 6.0 STL fixesVisual C++ 6.0 noncompliance issuesC++ FAQ Lite
Templates solve a lot of problems like this.
Depending on how much of a masochist you are, you could try to figure out how the boost python library works. It does not need to use assembly language.
http://www.boost.org/libs/python/doc/index.html
If you want to try to extend Scott Bilas''s code for member functions, remember that the calling convention for member functions places ''this'' in register ECX in MSVC. You cannot call a non-static member function safely without a valid ''this'' pointer. Good luck determining how to distinguish between virtual and non-virtual member functions, and how pointers to them are represented!
Depending on how much of a masochist you are, you could try to figure out how the boost python library works. It does not need to use assembly language.
http://www.boost.org/libs/python/doc/index.html
If you want to try to extend Scott Bilas''s code for member functions, remember that the calling convention for member functions places ''this'' in register ECX in MSVC. You cannot call a non-static member function safely without a valid ''this'' pointer. Good luck determining how to distinguish between virtual and non-virtual member functions, and how pointers to them are represented!
Thanks for your replies.
Actually I''ve defined a datatype for pointer to member functions like this:
typedef void (CObject:: *P2MF)();
Well normally it works for all kind of member functions. Normally the sizeof(P2MF) operator returns 4 bytes, but under some special circumstances it returns 16 Bytes ?!?
In VC 6.0 the watch output looks like that:
0x0040165e [thunk]:''vcall''{24,{flat}}''
The bad thing about this is, that if the P2MF is 16 byte long, the compiler only allows member functions with the same types of arguments to cast to P2MF.
I have no idea why this happens - does anyone know?
Thanks a lot!
Bunnz
Actually I''ve defined a datatype for pointer to member functions like this:
typedef void (CObject:: *P2MF)();
Well normally it works for all kind of member functions. Normally the sizeof(P2MF) operator returns 4 bytes, but under some special circumstances it returns 16 Bytes ?!?
In VC 6.0 the watch output looks like that:
0x0040165e [thunk]:''vcall''{24,{flat}}''
The bad thing about this is, that if the P2MF is 16 byte long, the compiler only allows member functions with the same types of arguments to cast to P2MF.
I have no idea why this happens - does anyone know?
Thanks a lot!
Bunnz
Have fun BunnzPunch'n'Crunch
I think it could potentially be longer if you''re using multiple inheritence, as it needs seperate vtblptr''s for each super-class.
Can you determine the exact conditions where it''s different?
There''s no difference between a virtual and non-virtual method pointer. What it points at is different. You can even ''up cast'' derived classes methods (non-inherient, non-overridden ones - i.e. new methods) to a base class method pointer, and have the base class safely call into you''re derived class with the up cast''ed method pointer - so long as you have hard set rules for the parameters, e.g. (MSG, WPARAM, LPARAM).
Magmai Kai Holmlor
- The disgruntled & disillusioned
Can you determine the exact conditions where it''s different?
There''s no difference between a virtual and non-virtual method pointer. What it points at is different. You can even ''up cast'' derived classes methods (non-inherient, non-overridden ones - i.e. new methods) to a base class method pointer, and have the base class safely call into you''re derived class with the up cast''ed method pointer - so long as you have hard set rules for the parameters, e.g. (MSG, WPARAM, LPARAM).
Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Hi Magmai Kai Holmlor
Well, I think I try to show you the circumstances:
I have a module:
Function.hpp / cpp
..
class CObject; // only declarations!
typedef void (CObject:: *P2MF)();
CMetaFunction(string name, P2MF p, bla bla)
{
};
Object.hpp / cpp
#include "Function.hpp"
CObject
{
};
So if I typedef and use P2MF in Function.hpp, the size is 16 bytes. (If I work with a declared-only class CObject);
But if I don''t declare and use it in Function.hpp but in Object.hpp, it just has 4 bytes. (class also defined)
Actually I''m not sure, if the declaration/definition of the class is the reason, but I can''t find another reason (I use VC 6.0).
Bunnz
Visit: www.Bunnz.com
Well, I think I try to show you the circumstances:
I have a module:
Function.hpp / cpp
..
class CObject; // only declarations!
typedef void (CObject:: *P2MF)();
CMetaFunction(string name, P2MF p, bla bla)
{
};
Object.hpp / cpp
#include "Function.hpp"
CObject
{
};
So if I typedef and use P2MF in Function.hpp, the size is 16 bytes. (If I work with a declared-only class CObject);
But if I don''t declare and use it in Function.hpp but in Object.hpp, it just has 4 bytes. (class also defined)
Actually I''m not sure, if the declaration/definition of the class is the reason, but I can''t find another reason (I use VC 6.0).
Bunnz
Visit: www.Bunnz.com
Have fun BunnzPunch'n'Crunch
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement