read file, execute functions

Discussion in 'C++' started by ma740988, Nov 16, 2008.

  1. ma740988

    ma740988 Guest

    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.
    ma740988, Nov 16, 2008
    #1
    1. Advertising

  2. On 2008-11-16 03:19, ma740988 wrote:
    > 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++.

    --
    Erik Wikström
    Erik Wikström, Nov 16, 2008
    #2
    1. Advertising

  3. ma740988

    ma740988 Guest

    On Nov 16, 6:48 am, Erik Wikström <> wrote:
    > On 2008-11-16 03:19, ma740988 wrote:
    >
    >
    >
    > > 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.


    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.
    ma740988, Nov 16, 2008
    #3
  4. On 2008-11-16 17:47, ma740988 wrote:
    > On Nov 16, 6:48 am, Erik Wikström <> wrote:
    >> On 2008-11-16 03:19, ma740988 wrote:
    >>
    >>
    >>
    >> > 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.

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

    --
    Erik Wikström
    Erik Wikström, Nov 16, 2008
    #4
  5. ma740988

    ma740988 Guest

    > 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
    ma740988, Nov 17, 2008
    #5
  6. ma740988

    ma740988 Guest


    > 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
    ma740988, Nov 17, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,592
    Victor Bazarov
    Jun 21, 2004
  2. ferbar
    Replies:
    9
    Views:
    365
    Keith Thompson
    Sep 25, 2005
  3. Replies:
    4
    Views:
    344
    eric dexter
    Jun 28, 2010
  4. Savas Ates
    Replies:
    1
    Views:
    276
    Patrice
    Aug 17, 2004
  5. Alex Dowad
    Replies:
    4
    Views:
    270
    Michel Demazure
    May 1, 2010
Loading...

Share This Page