Click here to Skip to main content
15,894,825 members
Articles / Desktop Programming / Win32

Generic Thunk with 5 combinations of Calling Conventions

Rate me:
Please Sign up or sign in to vote.
2.78/5 (8 votes)
13 Apr 2008CPOL12 min read 33.4K   398   19  
A simple and generic solution of making a member function become a callback function with the help of thunk technology.
#include <Thunk.h>

#include "TestClass.h"
using namespace std;

using Thunk::CThunk;
using Thunk::Helper;

//CThunk t;	// CThunk is also a define in Preprocessor
// these Preprocessor macro allow us test Thunk with one set of source code

CThunk t1,t2,t3;

int main()
{
	cout<<"C Style Functions "<<PLAIN_CONVENTION_NAME<<endl;
	cout<<"C++ Method "<<METHOD_CONVENTION_NAME<<endl;
	cout<<typeid(CThunk).name()<<" ,size = "<<sizeof(CThunk)<<endl<<endl;
	

	C1 obj1(4,"obj1");

	t1.Attach(&obj1);
	
	t1.Attach(MF1);
	int i = t1;
	CB1 = t1;
	{
		double result = CB1(2,3);
		cout<<"result = "<<result<<endl<<endl;
	}

	t1.Attach(MF2);
	CB2 = t1.MakeCallback<CBFun2>();
	{
		int result = CB2(7,8,6);
		cout<<"result = "<<result<<endl<<endl;
	}

	t1.AttachMethod(&C1::Fun3);
	CB3 = t1.MakeCallback<CBFun3>();
	CB3(326);

	t1.Attach(MF4);
	CB4 = t1.MakeCallback<CBFun4>();
	{
		CLarge result = CB4('i',1,2,2.2);
		cout<<"result = "<<result<<endl<<endl;
	}

	t1.Attach(MF5);
	CB5 = t1.MakeCallback<CBFun5>();
	{
		int result = CB5(15);
		cout<<"result = "<<result<<endl<<endl;
	}

	cout<<endl<<"operator =, ";
	t2 = t1;
	if (t1==t2)
		cout<<" operator == :both OK"<<endl;
	else
		cout<<" operator == :failed"<<endl;
	CB5 = t2.MakeCallback<CBFun5>();
	{
		int result = CB5(12);
		cout<<"result = "<<result<<endl<<endl;
	}
	system("pause");
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions