473,507 Members | 6,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Similar topics

11
2676
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
10
10003
by: Barbrawl McBribe | last post by:
Is is possible to use typedefs to cast function pointers? I think I saw this in the WINGs src; grep for '(hashFunc)'. So far, trying to use a typedef to cast function pointers so that a return...
23
7768
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
12
8348
by: srinivas.satish | last post by:
Hi, is it possible to typecast a function pointer to two different prototypes. eg., typedef void (functptr1 *) (int , int); typedef void (functptr2 *) (int); functptr1 fptr; fptr =...
18
4311
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
11
2714
by: Felix Kater | last post by:
Hi, I can compile and run this code (see below) which twice calls the function f, first with too less, second with too much arguments. But is it legal and free of memory leaks and other...
16
7570
by: arne | last post by:
Hi all, imagine I call a function, but omit one of the parameters, like: foo.c: void foo( int a, int b ) { /* do something with a and b */ return; }
0
1208
by: gwampach | last post by:
Hello, I'm a new Perl programmer and I'm using the Win32::API module to use functions of a C dll. I'm using ActivePerl 5.8.8. Here is the C code of the function I need to use: const char*...
20
2199
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7030
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5041
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.