map of function pointer

S

shahehe

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
 
A

Andrew McDonagh

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.
 
J

Jesper Madsen

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
 
M

msalters

(e-mail address removed) 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top