473,513 Members | 2,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

map of function pointer

Hi

I have a list of strings in a file and I want to call different
functions according to each string I parsed from the file.

For example, if I read "LOAD" from the file, I should call void
read(void) . If I read "SAVE" from the file, I should call function
void save(string fileName).

I do not want to use switch here. Also, I know I may use map of
function pointers here.
typedef void (*Function_Ptr)(void );
typedef std::map<string, Function_Ptr> Processing_Map;
....

However, each function has different parameters, what's the best to
code the above case?

thanks

Jul 23 '05 #1
3 3821
sh*****@gmail.com wrote:
Hi

I have a list of strings in a file and I want to call different
functions according to each string I parsed from the file.

For example, if I read "LOAD" from the file, I should call void
read(void) . If I read "SAVE" from the file, I should call function
void save(string fileName).

I do not want to use switch here. Also, I know I may use map of
function pointers here.
typedef void (*Function_Ptr)(void );
typedef std::map<string, Function_Ptr> Processing_Map;
...

However, each function has different parameters, what's the best to
code the above case?

thanks


You don't say what or where the arguments come from, so I'll assume the
arguments are also read from the file.

In this case I'd start off using a command pattern. Having a map of
objects rather than function pointers, call a virtual 'execute(..)'
method of that object, passing in the arguments retrieved.

You could pass the string of args un-parsed into the commands, or use a
double dispatch mechanism, whereby you pass the 'this' reference to the
command's 'execute(..)' method. Then the Command could call back into
'this' to retrieve just the arguments they want.

Jul 23 '05 #2
For simplicity you could do a std::vector<boost::any> for parameters.

void load(std::vector<boost::any>&)
void save(std::vector<boost::any>&)
void dostuff(std::vector<boost::any>&)

or you can do a parameters class
class Parameters {
public:
virtual unsigned int paramCount() const = 0;
};

class NoParameters : public Parameters {
public:
virtual unsigned int paramCount() const { return 0; };
};

class LoadParameters : public Parameters {
std::string filename_;
public:
LoadParameters(std::string const & f):filename_(f) {};
virtual unsigned int paramCount() const { return 1; };
std::string const & filename() const { return filename_; }
};
....

If your parameters are the same from invocation to invocation. A
boost::function from a map might cut it, or you could make a factory that
read the parameters from your command sequence, and created a function..

There are lots of implementations, if your commands were translated from
strings to ints perhaps enums, you could do the switching using templates..
--
Jesper Madsen, SAXoTECH

"The truth of the matter is that you always know the right thing to do. The
hard part is doing it"
- General H. Norman Schwartzoff
<sh*****@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Hi

I have a list of strings in a file and I want to call different
functions according to each string I parsed from the file.

For example, if I read "LOAD" from the file, I should call void
read(void) . If I read "SAVE" from the file, I should call function
void save(string fileName).

I do not want to use switch here. Also, I know I may use map of
function pointers here.
typedef void (*Function_Ptr)(void );
typedef std::map<string, Function_Ptr> Processing_Map;
...

However, each function has different parameters, what's the best to
code the above case?

thanks

Jul 23 '05 #3

shah...@gmail.com schreef:
Hi

I have a list of strings in a file and I want to call different
functions according to each string I parsed from the file.

For example, if I read "LOAD" from the file, I should call void
read(void) . If I read "SAVE" from the file, I should call function
void save(string fileName).

I do not want to use switch here. Also, I know I may use map of
function pointers here.
typedef void (*Function_Ptr)(void );
typedef std::map<string, Function_Ptr> Processing_Map;
...

However, each function has different parameters, what's the best to
code the above case?


Simple:
void getArgsAndSave(void) {
string fileName = ... ;
save(fileName);
}
Processing_Map["SAVE"] = &getArgsAndSave;

or

void read( string /* ignored*/ )
{
...
}

HTH,
Michiel Salters

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
10050
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
37
4946
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
2
12664
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
27
2443
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints...
23
7770
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...
3
3628
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
54
24439
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
26
4823
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
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...
10
3564
by: Richard Heathfield | last post by:
Stephen Sprunk said: <snip> Almost. A function name *is* a pointer-to-function. You can do two things with it - copy it (assign its value to an object of function pointer type, with a...
0
7257
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
7157
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...
0
7379
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,...
0
4745
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...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1591
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
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
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.