C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

callback revisited

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
ma740988@pegasus.cc.ucf.e
Guest





PostPosted: Tue Jul 26, 2005 7:05 pm    Post subject: callback revisited Reply with quote



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

Back to top
Victor Bazarov
Guest





PostPosted: Tue Jul 26, 2005 7:12 pm    Post subject: Re: callback revisited Reply with quote



[email]ma740988 (AT) pegasus (DOT) cc.ucf.edu[/email] wrote:
Quote:
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?

Quote:
virtual ~CallbackBase() = 0;
};

CallbackBase::~CallbackBase() { }
template<typename T
class Callback : public CallbackBase
{
public:
typedef void (T::*F)();
^^^^^^^^^^^^^^

Add an 'int' argument to this declaration.

Quote:
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.

Quote:
private:
T* t_;
F f_;
};

template<typename T
Callback .. ^^^^^^^^^^^^^^^

The 'f' argument declaration has to change here too.

Quote:
{
return Callback<T>( t, f );
}
// std::auto_ptr version ... akin to the above


V

Back to top
ma740988@pegasus.cc.ucf.e
Guest





PostPosted: Wed Jul 27, 2005 11:10 am    Post subject: Re: callback revisited Reply with quote




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
}

Back to top
Dan Cernat
Guest





PostPosted: Wed Jul 27, 2005 12:34 pm    Post subject: Re: callback revisited Reply with quote



[email]ma740988 (AT) pegasus (DOT) cc.ucf.edu[/email] wrote:
Quote:
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.


Quote:
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?

Quote:
};

snip


/dan


Back to top
ma740988@pegasus.cc.ucf.e
Guest





PostPosted: Thu Jul 28, 2005 12:35 am    Post subject: Re: callback revisited Reply with quote

|| you are trying to declare the type F twice here
I knew that I was just trying to illustrate the point.

Quote:
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
};


Back to top
ma740988@pegasus.cc.ucf.e
Guest





PostPosted: Thu Jul 28, 2005 12:36 am    Post subject: Re: callback revisited Reply with quote

|| you are trying to declare the type F twice here
I knew that I was just trying to illustrate the point.

Quote:
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?


Back to top
Victor Bazarov
Guest





PostPosted: Thu Jul 28, 2005 1:11 am    Post subject: Re: callback revisited Reply with quote

[email]ma740988 (AT) pegasus (DOT) cc.ucf.edu[/email] wrote:
Quote:
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



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.