473,705 Members | 2,333 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 3059
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
2257
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 the callback is executed? If an exception is thrown, can it be caught?
1
2617
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? I posted this message last week, but got no response. I am really hoping someone can help me with...
4
1773
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
3217
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
7208
by: Guru | last post by:
How do I refresh GridView using CallBack? I am populating the grid at runtime using OLEDB.
2
1164
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 best i can use (atlas or client callback)?
5
2321
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
1779
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
2737
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
8767
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9136
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9030
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7893
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5933
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4439
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2081
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.