Click here to Skip to main content
15,884,099 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.3K   398   19  
A simple and generic solution of making a member function become a callback function with the help of thunk technology.
#include "TestClass.h"
#include <Thunk/ThunkBase.h>
#include <iostream>
using namespace Thunk;
using namespace std;

double C1::Fun1(int v1,int v2) const
{
	cout<<m_name<<".Fun1("<<v1<<","<<v2<<"); this->m_v="<<m_v<<endl;
	double result = m_v*1+v1*2+v2*3;
	cout<<m_v<<"*1+"<<v1<<"*2+"<<v2<<"*3="<<result<<endl;
	cout<<"after calling m_v="<<m_v<<endl;
	return result;
}

int C1::Fun2(int v1,int v2,int v3) const
{
	cout<<m_name<<".Fun2("<<v1<<","<<v2<<","<<v3<<"); this->m_v="<<m_v<<endl;
	int result = m_v+v1+v2+v3;
	cout<<m_v<<"+"<<v1<<"+"<<v2<<"+"<<v3<<"="<<result<<endl;
	cout<<"after calling m_v="<<m_v<<endl;
	return result;
}

void C1::Fun3(int v)
{
	cout<<m_name<<".Fun3("<<v<<"); m_v="<<m_v<<endl;
	m_v = v;
	cout<<"after calling m_v="<<m_v<<endl<<endl;
}

ostream& operator<<(ostream &os,const CLarge &obj)
{
	//os<<"char = "<<obj.m_c<<" short = "<<obj.m_s<<" int = "<<obj.m_i
	//	<<"long  = "obj.m_l<<" double = "<<obj.m_d;
	obj.Print(os);
	return os;
}

void CLarge::Print(std::ostream &os) const
{
	os<<"char = "<<m_c<<" short = "<<m_s<<" int = "<<m_i
		<<" long  = "<<m_l<<" double = "<<m_d;
}

CLarge C1::Fun4(char c,short s,long l,double d) const
{
	return CLarge(c,s,m_v,l,d);
}

int  C1::Fun5(int factor) const
{
	cout<<m_name<<".Fun5("<<factor<<"); this->m_v="<<m_v<<endl;
	int i = m_v;
	i*=factor;
	cout<<m_v<<"*"<<factor<<"="<<i<<endl;
	cout<<"after calling m_v="<<m_v<<endl;
	return i;
}

int C1::Fun6() const
{
	cout<<m_name<<".Fun6"<<"(); this->m_v="<<m_v<<endl;
	cout<<"after calling m_v="<<m_v<<endl;
	return m_v;
}


CBFun1 CB1 = NULL;
CBFun2 CB2 = NULL;
CBFun3 CB3 = NULL;
CBFun4 CB4 = NULL;
CBFun5 CB5 = NULL;

const int MF1 = Thunk::Helper::PointerToInt32(&C1::Fun1);
const int MF2 = Thunk::Helper::PointerToInt32(&C1::Fun2);
const int MF3 = Thunk::Helper::PointerToInt32(&C1::Fun3);
const int MF4 = Thunk::Helper::PointerToInt32(&C1::Fun4);
const int MF5 = Thunk::Helper::PointerToInt32(&C1::Fun5);

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