By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,486 Members | 1,716 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,486 IT Pros & Developers. It's quick & easy.

callback revisited

P: n/a
I was perusing modern C++ designs and the loki library and I might add
I hit overload when I hit select part of that book and the library.
The template _stuff_ is very intense ...... whew!!

Oso long ago I found - for the most part a nice callback class.
Current approach works well for functions that accept no argments.
Trouble is I now need to tweak the code to accomodate the case for an
integer argument. How could I achieve this?

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

class CallbackBase
{
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 );
}
// std::auto_ptr version ... akin to the above

If it's in the two hard pile then I'll end up having to go study the
loki library and re-read those chapters of Modern C++ design

Thanks

Jul 26 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ma******@pegasus.cc.ucf.edu wrote:
I was perusing modern C++ designs and the loki library and I might add
I hit overload when I hit select part of that book and the library.
The template _stuff_ is very intense ...... whew!!

Oso long ago I found - for the most part a nice callback class.
Current approach works well for functions that accept no argments.
Trouble is I now need to tweak the code to accomodate the case for an
integer argument. How could I achieve this?

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

class CallbackBase
{
public:
virtual void operator()() const { };
You should probably add an 'int' argument to the operator function call
above. BTW, did you mean to make it pure as well?
virtual ~CallbackBase() = 0;
};

CallbackBase::~CallbackBase() { }
template<typename T>
class Callback : public CallbackBase
{
public:
typedef void (T::*F)(); ^^^^^^^^^^^^^^
Add an 'int' argument to this declaration.
Callback( T& t, F f ) : t_(&t), f_(f) { }
void operator()() const { (t_->*f_)(); }
Add an 'int' argument to this function too and pass it along to
the member.
private:
T* t_;
F f_;
};

template<typename T>
Callback<T> make_callback( T& t, void (T::*f) () ) .. ^^^^^^^^^^^^^^^
The 'f' argument declaration has to change here too.
{
return Callback<T>( t, f );
}
// std::auto_ptr version ... akin to the above


V
Jul 26 '05 #2

P: n/a

Vic, I envisioned what you alluded to. Those changes require a
'separate' class. I was hoping to maintain one class. So now:

class CallbackBase
{
public:
virtual void operator()() const { }; // no argument
virtual void operator()(int) const { }; // int argument
virtual ~CallbackBase() = 0;
};

CallbackBase::~CallbackBase() { }
template<typename T>
class Callback : public CallbackBase
{
public:
typedef void (T::*F)(); <--------- This
presents a problem
// typedef void (T::*F)(int); <--------- This
presents a problem
Callback( T& t, F f ) : t_(&t), f_(f) { }

void operator()() const { (t_->*f_)(); }
void operator()(int) const { (t_->*f_)(int); }

private:
T* t_;
F f_;
};

template<typename T>
Callback<T> make_callback( T& t, void (T::*f) () )
{
// stuff
}

template<typename T>
Callback<T> make_callback( T& t, void (T::*f) (int idx) )
{
// stuff
}

Jul 27 '05 #3

P: n/a


ma******@pegasus.cc.ucf.edu wrote:
Vic, I envisioned what you alluded to. Those changes require a
'separate' class. I was hoping to maintain one class. So now:

class CallbackBase
{
public:
virtual void operator()() const { }; // no argument
virtual void operator()(int) const { }; // int argument
virtual ~CallbackBase() = 0;
};

CallbackBase::~CallbackBase() { }
template<typename T>
class Callback : public CallbackBase
{
public:
typedef void (T::*F)(); <--------- This
presents a problem
// typedef void (T::*F)(int); <--------- This
presents a problem ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
you are trying to declare the type F twice here
once as a pointer to a function with no arguments and the secon time as
a pointer to a function with an angument of type int. can't do that.
make up your mind. or don't use typedefs at all.

Callback( T& t, F f ) : t_(&t), f_(f) { }

void operator()() const { (t_->*f_)(); }
void operator()(int) const { (t_->*f_)(int); }

private:
T* t_;
F f_; ^^^^^^^^^^^^ what F are you reffering here the firs one or the second
one?
};

<snip>

/dan

Jul 27 '05 #4

P: n/a
|| you are trying to declare the type F twice here
I knew that I was just trying to illustrate the point.

| or don't use typedefs at all
Good point, one solution lies here...

One other question with respect to the following. All indications seem
to suggest that the following is NOT a wise move.

class Base {
static std::vector < std::auto_ptr< CallbackBase > > my_callback;
/// vector of auto_ptrs..
public:
// stuff
};

Jul 28 '05 #5

P: n/a
|| you are trying to declare the type F twice here
I knew that I was just trying to illustrate the point.

| or don't use typedefs at all
Good point, one solution lies here...

One other question with respect to the following. All indications seem
to suggest that the following is NOT a wise move.

class Base {
static std::vector < std::auto_ptr< CallbackBase > > my_callback;
/// vector of auto_ptrs..
public:
// stuff
};

Potential pitfalls?

Jul 28 '05 #6

P: n/a
ma******@pegasus.cc.ucf.edu wrote:
you are trying to declare the type F twice here

I knew that I was just trying to illustrate the point.
or don't use typedefs at all

Good point, one solution lies here...

One other question with respect to the following. All indications
seem to suggest that the following is NOT a wise move.

class Base {
static std::vector < std::auto_ptr< CallbackBase > > my_callback;
/// vector of auto_ptrs..
public:
// stuff
};

Potential pitfalls?


std::auto_ptr cannot be stored in a container, it doesn't meet the
requirements. You need to roll your own or use some other solution.

V
Jul 28 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.