read file, execute functions

M

ma740988

I'm perusing a question and curiosity got the best of me in part
because I cant solve the problem

Consider:

typedef unsigned int word_type ;
class foo {

public:
void set_mx_digits_1 ( word_type wt ) {}
void set_mx_digits_2 ( double dt ) { }
// lots more.
};

Within a file, you're given a typedef'd representation of the foo
functions and the input arguments to the functions. In other words:

// filename input_file.dat
typedef void ( foo::*set_mx_digits_1 )( word_type wt ), 15
typedef void ( foo::*set_mx_digits_2 )( double dt ), 3.6
// lots more

The goal is to read the contents of the file into an appropriate
container then invoke the member functions with the input arguments.
Now I could read the contents into a string and convert the numbers
from string to int, string to double etc, but how could one store the
typedef'd representation into a map without manual intervention ( I'm
not seeing a way to read a file and store function pointers in a
map ..to compound things the input arguments are different) is beyond
me.

Maybe I misunderstood, however, if a solution exists I'd like to see
it because I'm coming up short. Thanks in advance.
 
E

Erik Wikström

I'm perusing a question and curiosity got the best of me in part
because I cant solve the problem

Consider:

typedef unsigned int word_type ;
class foo {

public:
void set_mx_digits_1 ( word_type wt ) {}
void set_mx_digits_2 ( double dt ) { }
// lots more.
};

Within a file, you're given a typedef'd representation of the foo
functions and the input arguments to the functions. In other words:

// filename input_file.dat
typedef void ( foo::*set_mx_digits_1 )( word_type wt ), 15
typedef void ( foo::*set_mx_digits_2 )( double dt ), 3.6
// lots more

The goal is to read the contents of the file into an appropriate
container then invoke the member functions with the input arguments.
Now I could read the contents into a string and convert the numbers
from string to int, string to double etc, but how could one store the
typedef'd representation into a map without manual intervention ( I'm
not seeing a way to read a file and store function pointers in a
map ..to compound things the input arguments are different) is beyond
me.

You have to create a map from the typedef (or some for of it) as a
string to a member-function pointer and then fill the map when the
program starts. Then read the line from the input-file, look it up in
the map, and use the member-function pointer to invoke the function.

What you really want is probably reflection, which is not possible in
standard C++.
 
M

ma740988

You have to create a map from the typedef (or some for of it) as a
string to a member-function pointer and then fill the map when the
program starts. Then read the line from the input-file, look it up in
the map, and use the member-function pointer to invoke the function.

I think I'm following you. The key is essentially a string and the
value a function pointer. Correct?
Follow on question for you. There's 60 'set_WHATEVER* (WHATEVER could
be header_word, hex_digits_1, tgt_north etc. etc. etc.)' methods with
arguments double or unsigned int (word_type). Is it possible to have
templated arguments to function pointers? If how could you provide
source illustrating this. I'd like to believe I could have a generic
function pointer inside .. say a map or some appropriate container.
That function then will call the appropriate set_WHATEVER method.

What you really want is probably reflection, which is not possible in
standard C++.
I see.
 
E

Erik Wikström

I think I'm following you. The key is essentially a string and the
value a function pointer. Correct?
Follow on question for you. There's 60 'set_WHATEVER* (WHATEVER could
be header_word, hex_digits_1, tgt_north etc. etc. etc.)' methods with
arguments double or unsigned int (word_type). Is it possible to have
templated arguments to function pointers? If how could you provide
source illustrating this. I'd like to believe I could have a generic
function pointer inside .. say a map or some appropriate container.
That function then will call the appropriate set_WHATEVER method.

You can probably not have just one map, since the values in a map all
have to have the same type, so you need one map for each combination of
parameter types.

You could probably use some kind of wrapper-class which container
several function-pointers and only initialise the correct one and use
that as the value, but I'm not sure if that's a good solution.
 
M

ma740988

Ypu would be better off by directly calling
the member functions by reading the file, or by storing the read strings
to call them later.

The latter I understand, the former - "You would be better off by
directly calling the member functions by reading the file" - I'm not
following. I understanding reading and storing a string then invoking
the member functions. I'm not understanding how I could read a file
then invoke the member functions directly.
Worse case show me an example in source.

Thanks
 
M

ma740988

int main() {

        {
                // Prepare an example file
                std::eek:fstream os("input_file.dat");
                os <<
           "typedef void ( foo::*set_mx_digits_1 )( word_type wt ), 15\n";
                os <<
           "typedef void ( foo::*set_mx_digits_2 )( double dt ), 3.6\n";
        }

        foo myfoo;
        ReadParseAndExecute("input_file.dat", myfoo);

}

You cleared up my confusion. Thanks
 

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
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top