callBack implementation question

M

ma740988

// 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
 
I

Ivan Vecerina

// 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)() )
{
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
 
M

ma740988

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.
 
M

ma740988

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?
}
 
I

Ivan Vecerina

Hi,
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top