Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > read file, execute functions

Reply
Thread Tools

read file, execute functions

 
 
ma740988
Guest
Posts: n/a
 
      11-16-2008

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.


 
Reply With Quote
 
 
 
 
Erik Wikström
Guest
Posts: n/a
 
      11-16-2008
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
 
Reply With Quote
 
 
 
 
ma740988
Guest
Posts: n/a
 
      11-16-2008
On Nov 16, 6:48*am, Erik Wikstrm <(E-Mail Removed)> 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.

 
Reply With Quote
 
Erik Wikström
Guest
Posts: n/a
 
      11-16-2008
On 2008-11-16 17:47, ma740988 wrote:
> On Nov 16, 6:48 am, Erik Wikström <(E-Mail Removed)> 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
 
Reply With Quote
 
ma740988
Guest
Posts: n/a
 
      11-17-2008
> 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

 
Reply With Quote
 
ma740988
Guest
Posts: n/a
 
      11-17-2008

> int main() {
>
> * * * * {
> * * * * * * * * // Prepare an example file
> * * * * * * * * std: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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need ideas for progress bar, as java functions execute Piper707@hotmail.com Java 0 02-24-2006 11:39 AM
open and read functions (can't make 'read' work..) ferbar C Programming 9 09-25-2005 07:52 AM
Can i execute aspx file in my asp pag with server.execute method(sorry) Savas Ates ASP General 1 08-17-2004 04:52 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
execute javascript functions in the page load event Bernie V ASP .Net 3 11-29-2003 07:00 PM



Advertisments