473,545 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The callback-to-derived-member function problem

Hi all

I have a class (cPort) that is designed to receive objects and,
depending on the type, call a handler (callback) in any descendant of a
cProcessBlock class. Callback functions take a shared_ptr<cBas eas a
parameter, and return void.

The code was working fine, although I have encountered problems (under
a Microsoft compiler, of course - VC 8.0) when I attempt to add
callbacks to a class with multiple inheritance. I hate multiple
inheritance in general, but this is necessary to interface with another
library. I think there is some issue regarding the size of member
function pointers in VC.

Anyway - I believe that this code can be converted to use
boost::function and/or boost::bind to iron out these issues. The
problem is that I am going more than slightly squint when I read the
boost docs.

I have attached a reduced set of my code below. I would be extremely
grateful if someone could point out how this code could be converted to
use the boost representations .

Thanks for any and all replies!

Ryan

typedef void (cProcessBlock: :*PortCallbackF nPtr)(shared_pt r<cBase>);

// structure that associates a callback function pointer with the
required caller
class cPortCallback : public cBase
{
public:
cPortCallback() :
m_pPortCallback Fn(NULL), m_pCaller(m_pNU LL) { }
cPortCallback(P ortCallbackFnPt r pPortCallbackFn ,
shared_ptr<cBas epCaller) :
m_pPortCallback Fn(pPortCallbac kFn), m_pCaller(pCall er) { }
PortCallbackFnP tr m_pPortCallback Fn;
shared_ptr<cBas em_pCaller;
};

class cPort : public cProcessBlock
{
public:
/// Add a new callback
void AddCallback(Por tCallbackFnPtr NewCallbackFnPt r, std::string
strClassName,
shared_ptr<cBas epCaller);

protected:
/// Callback registry (map of callback handlers)
map<string, cPortCallbackm_ CallbackRegistr y;
};

void cPort::AddCallb ack(PortCallbac kFnPtr NewCallbackFnPt r, std::string
strClassName,
shared_ptr<cBas epCaller)
{
m_CallbackRegis try[strClassName] = cPortCallback(N ewCallbackFnPtr ,
pCaller);
}

bool cPort::Timeslic e()
// (called repeatedly in thread) - continuously receive new objects and
process using callbacks
{
shared_ptr<cBas epNewObject;
pNewObject = Receive();

// Call appropriate callback depending on the received object's
class name
if (pNewObject) {
PortCallbackFnP tr CallbackFn =
m_CallbackRegis try[pNewObject->ClassName()].m_pPortCallbac kFn;
if (CallbackFn) // if appropriate callback handler is found

((*(shared_poly morphic_cast<cP rocessBlock>(m_ CallbackRegistr y[pNewObject->ClassName()].m_pCaller))).* (CallbackFn))(p NewObject);
}
return true;
}
An example of adding a callback:
AddCallback(sta tic_cast<PortCa llbackFnPtr>(&c Graph::OnNewGra phData),
"cRealMatri x", this);

Aug 25 '06 #1
3 3045
ry***********@g mail.com wrote:
Hi all

I have a class (cPort) that is designed to receive objects and,
depending on the type, call a handler (callback) in any descendant of a
cProcessBlock class. Callback functions take a shared_ptr<cBas eas a
parameter, and return void.

The code was working fine, although I have encountered problems (under
a Microsoft compiler, of course - VC 8.0) when I attempt to add
callbacks to a class with multiple inheritance. I hate multiple
inheritance in general, but this is necessary to interface with another
library. I think there is some issue regarding the size of member
function pointers in VC.

Anyway - I believe that this code can be converted to use
boost::function and/or boost::bind to iron out these issues. The
problem is that I am going more than slightly squint when I read the
boost docs.

I have attached a reduced set of my code below. I would be extremely
grateful if someone could point out how this code could be converted to
use the boost representations .

Thanks for any and all replies!

Ryan

typedef void (cProcessBlock: :*PortCallbackF nPtr)(shared_pt r<cBase>);

// structure that associates a callback function pointer with the
required caller
class cPortCallback : public cBase
{
public:
cPortCallback() :
m_pPortCallback Fn(NULL), m_pCaller(m_pNU LL) { }
cPortCallback(P ortCallbackFnPt r pPortCallbackFn ,
shared_ptr<cBas epCaller) :
m_pPortCallback Fn(pPortCallbac kFn), m_pCaller(pCall er) { }
PortCallbackFnP tr m_pPortCallback Fn;
shared_ptr<cBas em_pCaller;
};

class cPort : public cProcessBlock
{
public:
/// Add a new callback
void AddCallback(Por tCallbackFnPtr NewCallbackFnPt r, std::string
strClassName,
shared_ptr<cBas epCaller);

protected:
/// Callback registry (map of callback handlers)
map<string, cPortCallbackm_ CallbackRegistr y;
};

void cPort::AddCallb ack(PortCallbac kFnPtr NewCallbackFnPt r, std::string
strClassName,
shared_ptr<cBas epCaller)
{
m_CallbackRegis try[strClassName] = cPortCallback(N ewCallbackFnPtr ,
pCaller);
}

bool cPort::Timeslic e()
// (called repeatedly in thread) - continuously receive new objects and
process using callbacks
{
shared_ptr<cBas epNewObject;
pNewObject = Receive();

// Call appropriate callback depending on the received object's
class name
if (pNewObject) {
PortCallbackFnP tr CallbackFn =
m_CallbackRegis try[pNewObject->ClassName()].m_pPortCallbac kFn;
if (CallbackFn) // if appropriate callback handler is found

((*(shared_poly morphic_cast<cP rocessBlock>(m_ CallbackRegistr y[pNewObject->ClassName()].m_pCaller))).* (CallbackFn))(p NewObject);
}
return true;
}
An example of adding a callback:
AddCallback(sta tic_cast<PortCa llbackFnPtr>(&c Graph::OnNewGra phData),
"cRealMatri x", this);
You might want to ask on the Boost user's list, but since the
components you're talking about are mostly in TR1 (not counting
shared_polymorp hic_cast), if you post a more complete example that
demonstrates the problem and that we can cut and paste into our editors
unchanged (see the guidelines for posting code:
http://parashift.com/c++-faq-lite/ho....html#faq-5.8), we can
probably help you here.

Cheers! --M

Aug 25 '06 #2
Using boost::bind and boost::function for member function call backs is
actually surprisingly easy. You just make sure the first parameter you
bind is the 'this' object you want to call the member function on.

boost::function <void (void)f;
f = boost::bind(&My Class::MyMethod , this);

If you have additional parameters left that should be called on f:

boost::function <void (int, int)f;
f = boost::bind(&My Class::MyMethod , this, _1, _2);

This says, "MyMethod is a member function that takes two ints and
returns void. For the first parameter always give it 'this.'" which
effectively makes f a function that now takes 2 variables instead of 3
(before it took the 'this' pointer and two additional parameters). If
you did:

f = boost::bind(&My Class::MyMethod , this, _1, _1);

Now f will take only 1 parameter, and it will pass it as both the first
and second parameter to MyMethod.

I usually just make the particular boost::function <template needed a
typedef.

mlimber wrote:
ry***********@g mail.com wrote:
Hi all

I have a class (cPort) that is designed to receive objects and,
depending on the type, call a handler (callback) in any descendant of a
cProcessBlock class. Callback functions take a shared_ptr<cBas eas a
parameter, and return void.

The code was working fine, although I have encountered problems (under
a Microsoft compiler, of course - VC 8.0) when I attempt to add
callbacks to a class with multiple inheritance. I hate multiple
inheritance in general, but this is necessary to interface with another
library. I think there is some issue regarding the size of member
function pointers in VC.

Anyway - I believe that this code can be converted to use
boost::function and/or boost::bind to iron out these issues. The
problem is that I am going more than slightly squint when I read the
boost docs.

I have attached a reduced set of my code below. I would be extremely
grateful if someone could point out how this code could be converted to
use the boost representations .

Thanks for any and all replies!

Ryan

typedef void (cProcessBlock: :*PortCallbackF nPtr)(shared_pt r<cBase>);

// structure that associates a callback function pointer with the
required caller
class cPortCallback : public cBase
{
public:
cPortCallback() :
m_pPortCallback Fn(NULL), m_pCaller(m_pNU LL) { }
cPortCallback(P ortCallbackFnPt r pPortCallbackFn ,
shared_ptr<cBas epCaller) :
m_pPortCallback Fn(pPortCallbac kFn), m_pCaller(pCall er) { }
PortCallbackFnP tr m_pPortCallback Fn;
shared_ptr<cBas em_pCaller;
};

class cPort : public cProcessBlock
{
public:
/// Add a new callback
void AddCallback(Por tCallbackFnPtr NewCallbackFnPt r, std::string
strClassName,
shared_ptr<cBas epCaller);

protected:
/// Callback registry (map of callback handlers)
map<string, cPortCallbackm_ CallbackRegistr y;
};

void cPort::AddCallb ack(PortCallbac kFnPtr NewCallbackFnPt r, std::string
strClassName,
shared_ptr<cBas epCaller)
{
m_CallbackRegis try[strClassName] = cPortCallback(N ewCallbackFnPtr ,
pCaller);
}

bool cPort::Timeslic e()
// (called repeatedly in thread) - continuously receive new objects and
process using callbacks
{
shared_ptr<cBas epNewObject;
pNewObject = Receive();

// Call appropriate callback depending on the received object's
class name
if (pNewObject) {
PortCallbackFnP tr CallbackFn =
m_CallbackRegis try[pNewObject->ClassName()].m_pPortCallbac kFn;
if (CallbackFn) // if appropriate callback handler is found

((*(shared_poly morphic_cast<cP rocessBlock>(m_ CallbackRegistr y[pNewObject->ClassName()].m_pCaller))).* (CallbackFn))(p NewObject);
}
return true;
}
An example of adding a callback:
AddCallback(sta tic_cast<PortCa llbackFnPtr>(&c Graph::OnNewGra phData),
"cRealMatri x", this);

You might want to ask on the Boost user's list, but since the
components you're talking about are mostly in TR1 (not counting
shared_polymorp hic_cast), if you post a more complete example that
demonstrates the problem and that we can cut and paste into our editors
unchanged (see the guidelines for posting code:
http://parashift.com/c++-faq-lite/ho....html#faq-5.8), we can
probably help you here.

Cheers! --M
Aug 25 '06 #3

k0*****@gmail.c om wrote:
Using boost::bind and boost::function for member function call backs is
actually surprisingly easy. You just make sure the first parameter you
bind is the 'this' object you want to call the member function on.
Thanks! The conversion did indeed turn out to be surprisingly more easy
than I was expecting . . .

I hope the performance penalty of the "bind" stuff is not too high, but
I think the clarity of the code is probably worth it.

Ryan

Aug 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
551
by: Pratik | last post by:
what are callback functions? Where we require callback functions? In what scenario we require callback functions?
0
2248
by: johnny | last post by:
I'm hoping someone can help me better understand the scenario below In a multi-threaded application, say a worker thread makes an asynchronous call and specifies a callback method. But before the callback is executed, the thread is aborted by its creator. What is the expected behavior for this scenario? Does the thread stay alive until...
1
2609
by: johnny | last post by:
In a multi-threaded application, say a worker thread makes an asynchronous call and specifies a callback method. But before the callback is executed, the thread is aborted by its creator. What is the expected behavior for this scenario? Does the thread stay alive until the callback is executed? If an exception is thrown, can it be caught? ...
4
1763
by: Zach | last post by:
(1.) What is the general meaning of the term 'callback'? (2.) What does 'callback' mean, as used in the context of threading? Many thanks, Zach.
2
3211
by: Fabiano Maciel | last post by:
Hi, I am needing information about CallBack Functions / Delegates in the VB.NET. If somebody has some material on the subject, favor to indicate me where I can get, or to send me for email. thanks, Fabiano Maciel fabiano.maciel@dotnetraptors.com.br
3
7200
by: Guru | last post by:
How do I refresh GridView using CallBack? I am populating the grid at runtime using OLEDB.
2
1155
by: Bob | last post by:
Hi, I develop aspnet applications (no webservices) often using gridviews with sorting and dropdownlist ... I was wondering whether i could use 'ajax' methods for speeding up my applications (e.g. when sorting, the whole page is refreshed) . Now i read about client callback and atlas and i have some questions about that. 1) what's the...
5
2304
by: archana | last post by:
Hi all, I am having one confusion regarding invoking method through threading. I have delcared one delegate. and i have one class which has parameter of type delegate which i declared. I have passed address of function which needs to be invoked when my thread procedure complete.
9
1774
by: fabien.benard | last post by:
Hello, I'm trying to find how to use a callback in a SOAP client using SOAPpy. Does SOAPpy have to manage it, or does Python include some API to do it? Thanks a lot. Fabien
18
2724
by: kid joe | last post by:
Hello, I have seen the WndProc prototyped two ways, LONG WINAPI WndProc ( .... ); and LRESULT CALLBACK WndProc ( .... ); Are these functionally equivalent? What are WINAPI and CALLBACK defined as?
0
7468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7808
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7423
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5329
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4945
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.