473,714 Members | 2,149 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 5615
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
2710
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
10030
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
7812
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
8387
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
4351
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
2735
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
7600
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
1213
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
2229
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
8713
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9318
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
9187
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9080
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6638
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
5961
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
4730
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3164
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
3
2113
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.