473,666 Members | 2,334 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::s tring, 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 5611
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::s tring, 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::s tring, 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(ArgumentLis t 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
2700
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
10025
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 value of float is changed to float seems not to work; I belive it was like this: typedef int (*foo) (int); function_ptr_bar = (foo)function_that_returns_float;
23
7801
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 called, can be a genuine no-op. Consider: typedef int(*polymorphic_func)(int param);
12
8383
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 = somefunction_name; fptr(10,20); fptr = (functptr2)someotherfunction_name;
18
4343
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 can use dlopen/whatever to convert the function name into a pointer to that function, but actually calling it, with the right number of parameters, isn't easy. As far as I can see, there are only two solutions: 1) This one is portable. If...
11
2731
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 problems? Of course, I presume that inside f I don't access i in case it was called via g. int f(int i){ /* ... */ return 0; }
16
7589
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
1211
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* DecodeSomething(GUInt64 time, GUInt16 msgId, GUInt16 bodyLength, GUInt8 dxpNum, GUInt8* bodyData);
20
2222
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 think my way through what I want to do now. I don't know why - I'm fine with most other aspects of the language, but my brain goes numb when I'm reading about function pointers! I would like to have an array of structures, something like
0
8866
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7385
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6192
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4366
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
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 we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1772
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.