• Advertisement
Sign in to follow this  

C++ class as window callback function again!

This topic is 4412 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm am just playing around with some code(below) that implements a windows callback function as a class. Basically how is works is that the class contains a templated static member function where the template arguemnts is only integers. This integer is so that the compiler creates a new function for each instance of the class. A preprocessor macro named Init which contains VC++ __COUNTER__ creates a unique(hopefully) integer for each instance. The macro maps to a class member function Init that saves the address of the templated static member function as a key in a <map> to the object pointer. So when the callback functions in entered it uses the address function to extract the object pointer from the <map> What I like to know is there a way to remove the dependence of #define Init() Init<__COUNTER__>() and if the implementation sound?
#include <iostream>
#include <map>

#define CALLBACK __stdcall

using namespace std;

typedef int UNIT;
typedef int HWND;
typedef int WPARAM;
typedef int LPARAM;
typedef int LRESULT;
typedef LRESULT (*WNDPROC)(HWND,UNIT,WPARAM,LPARAM);

class A{
public:
	
	A():proc(NULL){};
	
	operator WNDPROC()
	{
		return proc;
	};

	virtual LRESULT fn2(HWND z,UNIT y,WPARAM x,LPARAM w) {
		cout << "Class A " << "fn2 "<<(int)this << endl;
		return 0;
	};


	template<int T> void Init(void)
	{
		proc = WndProc<T>;
		disp[proc] = this;
	};



private:

	template <int T> static LRESULT WndProc(HWND z,UNIT y,WPARAM x,LPARAM w)
	{
		A * a = disp[WndProc<T>];
		return a->fn2(z,y,x,w);	
	};


private:
	WNDPROC proc;
	static map<WNDPROC,A *> disp;
};

map<WNDPROC,A *> A::disp;


class C : public A
{
public:
	virtual LRESULT fn2(HWND z,UNIT y,WPARAM x,LPARAM w) {
		cout << "Class C " << "fn2 "<<(int)this << endl;
		return 0;
	};
};

#define Init() Init<__COUNTER__>()

int main(int argc, char* argv[])
{
	A a;
	a.Init();
	C b;
	b.Init();

	A * d = &b;

	WNDPROC c = a;
	c(0,0,0,0);
	c = b;
	c(0,0,0,0);
	c = *d;
	c(0,0,0,0);
	return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
What the hell? Are you trying to avoid including windows.h, or to come up with a new way to do this? If so, I got nothing for you, but if you're just interested in how this is generally done, I've a few links for you: Creating a Win32 Window Wrapper Class (GameDev), Window Wrapper for WinCE (but applicable for Win32), and A Minimal Windows API Wrapper (both on the Code Project).

Share this post


Link to post
Share on other sites
Quote:
Original post by yckx
What the hell? Are you trying to avoid including windows.h, or to come up with a new way to do this?


Nah, It is a console app(VC++) and I put the typedef's just in case I want to copy it to a win32 app.
I have seen this problem mentioned a couple of times so I thought I would give it a shot. Under basic/minimal conditions it appears to work however I would like to remove the Init() macro I have.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement