472,129 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Mapped function pointer with different parameters

consider these functions:
void foo()
{
cout << "foo() called;
}
void sayhello( std::string& msg ){
msg = "something";
}
int add( int x, int y)
{
return x + y;
}

typedef void (*fooFunc)();
typedef std::map<std::string, fooFunc> Processing_Map;

...
Processing_Map table;
table["foo"] = foo;
table["sayhello"] = ???
table["add"]= ???

how to map table["sayhello"] to sayhello function, considering it has
a parameter
also the "add"

and how to actually pass a parameter value to those mapped functions

--leaf

Feb 23 '06 #1
5 5425
leaf wrote:
consider these functions:
void foo()
{
cout << "foo() called;
}
void sayhello( std::string& msg ){
msg = "something";
}
int add( int x, int y)
{
return x + y;
}

typedef void (*fooFunc)();
typedef std::map<std::string, fooFunc> Processing_Map;

..
Processing_Map table;
table["foo"] = foo;
table["sayhello"] = ???
table["add"]= ???

how to map table["sayhello"] to sayhello function, considering it has
a parameter
also the "add"

and how to actually pass a parameter value to those mapped functions


function pointers are typed. You can't.

One way of doing it would be to wrap foo:
foo(std::string& msg) {
foo();
}

change your typedef:
typdef void (*fooFunc)(std::string&);

That only provides you with limited ability to call heterogeneous
function prototypes.

A typical "solution" is:
typedef void* (*fooFunc)(void*, void*);

Which allows you to return and accept arbitrary data by completely
removing type safety.

The difficulty is in keeping track of what to pass to which function.
If you know at the call site which type of data to pass, then just call
the correct function (or at least a function through a typesafe function
pointer).

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 23 '06 #2
Ben,
I use a struct to store function information.
struct TFunction{
int id; // function to call
ULONG* params; // function parameters
unsigned int nprams; // number of function parameters, ( just to
keep track )
}

The TFuncion is the stuct containing information about which function
to call and what and how many parameters to pass.
This stuct is filled from the others side of the Application.

---
leaf

Feb 23 '06 #3
leaf wrote:
Ben,
I use a struct to store function information.
struct TFunction{
int id; // function to call
ULONG* params; // function parameters
unsigned int nprams; // number of function parameters, ( just to
keep track )
}

The TFuncion is the stuct containing information about which function
to call and what and how many parameters to pass.
This stuct is filled from the others side of the Application.


You'd probably be much better off with a type safe implementation.

If you explain at a higher level what it is you're trying to achieve (as
opposed to how you want to achieve it), then a better solution may be
presented.

Check out the command pattern, to see if it better suits your needs.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 23 '06 #4
At this moment any implementation would do, just a while ago i read
articles about command pattern specifically, Generalized functors.
somehow i did understand how to use it, but i can't figure out the
final resolution.

This is the overall implementation of my program:
=====================================
All the functions are imported from a DLL ( much like a functions
library )

Based on TFunction structure, say passed to the function :
Functionizer( TFunction fn );

this 'Functionizer' call the right function as well as pass the
parameters stored in fn.

-----
leaf

Feb 23 '06 #5
leaf wrote:
consider these functions:
void foo();
void sayhello( std::string& msg );
int add( int x, int y);

typedef void (*fooFunc)();
typedef std::map<std::string, fooFunc> Processing_Map;
..
Processing_Map table;
table["foo"] = foo;
table["sayhello"] = ???
table["add"]= ??? and how to actually pass a parameter value to those mapped functions


"A parameter"? How many parameters do you pass to table[X] ? You can't
tell at compile time, so the compiler forbids it.

Typically you'd use something like this in a script language engine. In
that
case, you define a ArgumentList class. All functions take that, but
foo(ArgumentList al) complains if (al.size() > 0)

HTH,
Michiel Salters

Feb 23 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by JKop | last post: by
10 posts views Thread by Barbrawl McBribe | last post: by
23 posts views Thread by bluejack | last post: by
12 posts views Thread by srinivas.satish | last post: by
20 posts views Thread by MikeC | last post: by

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.