473,473 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Create 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<typename 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<typename 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)test( this, &FOO::foosCallBack); //or just
rely on std::bad_alloc
if ( !t ) return;
}
};
#endif

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

Thanks in advance for the help

Jul 23 '05 #1
4 2034
<ma******@pegasus.cc.ucf.edu> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.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<typename 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<typename 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<CallbackBase> new_callback( T& t, void(T::*f)() )
{
return std::auto_ptr<CallbackBase>( 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(void* cookie)
{
CallbackBase& cb = *static_cast<CallbackBase*>(cookie);
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_setupCallback( test_callback, my_cb.get() );
//NB: test_callback is the func above - can be static member as well
}

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

private:
std::auto_ptr<CallbackBase> 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_setupCallback ' needs to be of the form
void test_callback( unsigned int *slDev unsigned int dBellVal ); //
API requirements.

as opposed to:
| void test_callback(void* 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******@pegasus.cc.ucf.edu> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

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

The fuction passed into

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

as opposed to:
| void test_callback(void* 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
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
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...
0
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...
6
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,...
4
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
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...
3
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...
6
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...
18
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.