473,704 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

callBack implementation question


// file sltest.h
#ifndef SLTEST_H
#define SLTEST_H

class CallbackBase // herb shutters gotW source ..
{
public:
virtual void operator()() const { };
virtual ~CallbackBase() = 0;
};

CallbackBase::~ CallbackBase() { }

template<typena me T>
class Callback : public CallbackBase
{
public:
typedef void (T::*F)();

Callback( T& t, F f ) : t_(&t), f_(f) { }
void operator()() const { (t_->*f_)(); }

private:
T* t_;
F f_;
};

template<typena me T>
Callback<T> make_callback( T& t, void (T::*f) () )
{
return Callback<T>( t, f );
}

class test {
static void callBack() {
// here we'll call the apporpriate fuction
// passed into test.
}
public:
test ()
// : CallBack<>( )
{
}

};

#endif

I'd like to do two things:
1. Retrofit the constructor of the class test to take an object and a
member function. test's initializer list will in turn call the
constructor of CallBack with the parameters passed into test.
2. With test::callBack, call the apporpriate member function that was
passed into the constructor of test. NOTE: The impetus behind this is
predicated upon the use of a vendors API suite, where each call to the
vendors 'doorbellWrite' function (not shown) results in execution of
the member function callBack within test. For greater flexibility I
desire to call my own function from with callBack. So now typical
usage:
// file usetest.
#ifndef FOO_H
#define FOO_H

class FOO {
test* t;
public:
void foosCallBack() {
// gives me greater flexibility to do whatever .. here
}
FOO() {
t = new (std::nothrow)t est( this, &FOO::foosCallB ack); //or just
rely on std::bad_alloc
if ( !t ) return;
}
};
#endif

Now: each execution of test::callBack will result in a call to
FOO::foosCallBa ck

Thanks in advance for the help

Jul 23 '05 #1
4 2050
<ma******@pegas us.cc.ucf.edu> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .

// file sltest.h
#ifndef SLTEST_H
#define SLTEST_H

class CallbackBase // herb shutters gotW source ..
{
public:
virtual void operator()() const { };
virtual ~CallbackBase() = 0;
};

CallbackBase::~ CallbackBase() { }

template<typena me T>
class Callback : public CallbackBase
{
public:
typedef void (T::*F)();

Callback( T& t, F f ) : t_(&t), f_(f) { }
void operator()() const { (t_->*f_)(); }

private:
T* t_;
F f_;
};

template<typena me T>
Callback<T> make_callback( T& t, void (T::*f) () )
{
return Callback<T>( t, f );
}
Because the CallbackBase instances are used polymorphically ,
the previous function might best be replaced with:
template<class T>
std::auto_ptr<C allbackBase> new_callback( T& t, void(T::*f)() )
{
return std::auto_ptr<C allbackBase>( new Callback<T>(t,f ) );
}
class test {
static void callBack() {
// here we'll call the apporpriate fuction
// passed into test.
}
public:
test ()
// : CallBack<>( )
{
}

};

#endif

I'd like to do two things:
1. Retrofit the constructor of the class test to take an object and a
member function. test's initializer list will in turn call the
constructor of CallBack with the parameters passed into test.
2. With test::callBack, call the apporpriate member function that was
passed into the constructor of test. NOTE: The impetus behind this is
predicated upon the use of a vendors API suite, where each call to the
vendors 'doorbellWrite' function (not shown) results in execution of
the member function callBack within test. For greater flexibility I
desire to call my own function from with callBack.


A properly designed callback system should normally provide a "cookie"
(typically a void* pointer) to the callback function.
You would then implement this function with something like:
void test_callback(v oid* cookie)
{
CallbackBase& cb = *static_cast<Ca llbackBase*>(co okie);
cb();
}

Encapsulation in a class with RAII could look like:
class Test
{
public:
template<class T>
Test( T& t, void(T::*f)() )
: my_cb( new_callback(t, f) )
{
someAPI_setupCa llback( test_callback, my_cb.get() );
//NB: test_callback is the func above - can be static member as well
}

~Test()
{
someAPI_removeC allback( test_callback, my_cb.get() );
}

private:
std::auto_ptr<C allbackBase> my_cb;

//disable copy-assignment and constructor:
Test(Test const&);
Test& operator=(Test const&);
};

Of course in practice, it is a good idea to use boost::function
instead of a custom CallbackBase class...

I hope this helps,
Ivan

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 23 '05 #2
Ivan appreaciate the help. I think I see where you're headed.

Thanks alot.

| Of course in practice, it is a good idea to use boost::function
At some point I need to twist my advisors arm and get him to embrace
'boost' but for now it seems that most folks I converse with find boost
convoluted (or 'overkill). Strange!!
Admittidely, it's quite advanced for some of us and at first when I
initially viewed boost I thought 'whew'.
Oh well, it's hard for me to get folks to embrace it.

Jul 23 '05 #3

Ivan, 'studied your source' and with that I have one other question.

The fuction passed into

' someAPI_setupCa llback ' needs to be of the form
void test_callback( unsigned int *slDev unsigned int dBellVal ); //
API requirements.

as opposed to:
| void test_callback(v oid* cookie)

In that case how would I call the call back function passed in as the
second argument to the Test constructor.

void test_callback( unsigned int *slDev unsigned int dBellVal )
{
// the approach here?
}

Jul 23 '05 #4
Hi,
<ma******@pegas us.cc.ucf.edu> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .

Ivan, 'studied your source' and with that I have one other question.

The fuction passed into

' someAPI_setupCa llback ' needs to be of the form
void test_callback( unsigned int *slDev unsigned int dBellVal ); //
API requirements.

as opposed to:
| void test_callback(v oid* cookie)
The question is:
do you need to simultaneously set multiple independent callbacks?
If so, there has to be something in the parameters to the callback
function that allows you to identify *which* of your current
callbacks is currently being triggered.
Your (gobal or static) callback function therefore needs to use
this information to 'dispatch' the callback event to the right
object instance.
In that case how would I call the call back function passed in as the
second argument to the Test constructor.

void test_callback( unsigned int *slDev unsigned int dBellVal )
{
// the approach here?
}

If 'dBellVal' is what distinguishes a callback from another,
you could have a global(/static) table(/map) that associates
each dBellVal with a given callback object.

NB: if you use an STL container, be aware that instances of
auto_ptr cannot be stored in them...
hth --Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 23 '05 #5

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

Similar topics

6
5325
by: Eric Entressangle | last post by:
Hi all, is there any trouble setting an C++ static class method as callback function for a C program or library ? Thanks
5
4173
by: Francois De Serres | last post by:
Hiho, could somebody please enlighten me about the mechanics of C callbacks to Python? My domain is more specifically callbacks from the win32 API, but I'm not sure that's where the problem lies. Here's a description... I want a callback-based MIDI input/processing, so PortMidi was not an alternative. I have written a C extension module that links to the mmsys MIDI API. I separated the win32-dependant code from the Python extension...
0
1054
by: Francois De Serres | last post by:
Hiho, When there's an unhandled exception in my extension-module's-callback-into-Python-function-object, I get a GPF and Python exits. When the exception is being handled within the callback (hence in Python), I get a very significant hiccup (1 to 5 seconds freeze). Question: is there a specific way to deal with exceptions when they happen inside such a callback? I searched the web with no avail. All
6
2238
by: db_from_mn | last post by:
I'm having a lot of difficulty getting even the simplest callback function (void return, no arguments) to work, from a dll to C#. When the callback is invoked from the dll, I get a fatal error, that shuts down the app. I suspect that the function pointer is passed incorrectly. I've studied the documentation, and I seem to have things configured properly. Are there any bugs that I need to know of, or any hints, other than what I've found...
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.
5
1945
by: sajin | last post by:
Hi All.. We are using VB .Net 2005 for implementing an API. API needs to generate events. For this client wants us to use Windows Callback (delegate implementation). The intention of using Windows Callback is to generalise our API so that it can be compatible with any other language e.g. C++. Using normal delegates will not help us since delegates is a VB.Net feature and any other language cant make a use of it. Could please
3
3869
by: Builder | last post by:
Hello, I created a DirectShow filter which detects faces from video. Filter exposes interface, which enables to set a callback, which is called when a face is detected. I made an application in C# which creates a DirectShow graph with mentioned face detector filter and calls its interface. Excerpt of the face detector interface declaration is as follows (method SetCallback is important):
6
2163
by: jmDesktop | last post by:
In a function that takes another function (function pointer) as a argument, or the callback function, which is the one that "calls back"? I'm having a hard time understanding the language. Am I right that if function A is: function A(*function pointer to B aka callback func, other arguments) { call (B); //calls }
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
8689
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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
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...
1
6605
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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...
0
4703
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3137
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.