469,903 Members | 1,807 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

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 1853
<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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Eric Entressangle | last post: by
5 posts views Thread by Francois De Serres | last post: by
reply views Thread by Francois De Serres | last post: by
6 posts views Thread by db_from_mn | last post: by
4 posts views Thread by Zach | last post: by
3 posts views Thread by Builder | last post: by
18 posts views Thread by kid joe | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.