473,320 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

callback revisited

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
6 1295
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

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


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
|| 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
|| 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: scott ocamb | last post by:
hello I have implemented a solution using async methods. There is one async method that can be invoked multiple times, ie there are multiple async "threads" running at a time. When these...
0
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion is as follows **************************************** The Original Fucntion in the dll...
4
by: ma740988 | last post by:
// file sltest.h #ifndef SLTEST_H #define SLTEST_H class CallbackBase // herb shutters gotW source .. { public: virtual void operator()() const { }; virtual ~CallbackBase() = 0; };
15
by: Felix Kater | last post by:
Hi, in a given library I register callback functions with this function: bool set_callback(int index, int (*callback_function)(long)); I need the callback function to also pass the index...
2
by: MR | last post by:
help! I have an unmanaged DLL that I do not have the source code, so i can't recompile or make changes. the DLL requires a callback function. I would like to implement the callback method in a...
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
10
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
5
by: Jef Driesen | last post by:
I have a C DLL that I want to use from a C# project. The C header file contains these declarations: typedef void (*callback_t) (const unsigned char *data, unsigned int size, void *userdata);...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.