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
#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