473,796 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

read file, execute functions


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_di gits_1 )( word_type wt ), 15
typedef void ( foo::*set_mx_di gits_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.
Nov 16 '08 #1
6 2287
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_di gits_1 )( word_type wt ), 15
typedef void ( foo::*set_mx_di gits_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
Nov 16 '08 #2
On Nov 16, 6:48*am, Erik Wikström <Erik-wikst...@telia. comwrote:
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_di gits_1 )( word_type wt ), 15
* typedef void ( foo::*set_mx_di gits_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.

Nov 16 '08 #3
On 2008-11-16 17:47, ma740988 wrote:
On Nov 16, 6:48 am, Erik Wikström <Erik-wikst...@telia. comwrote:
>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_di gits_1 )( word_type wt ), 15
typedef void ( foo::*set_mx_di gits_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
Nov 16 '08 #4
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

Nov 17 '08 #5
ma740988 <ma******@gmail .comkirjutas:
>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.
Here you are, it got a bit longer than I thought, and the parser is not
very smart, and error handling is lacking, and this most probably is not
what you want, but anyway, I figure as I already wrote this for fun, I
can as well post it. (Tested by cygwin g++ 3.4.4.)

Cheers, Paavo

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>

typedef std::string::si ze_type indx_t;

typedef unsigned int word_type ;
class foo {

public:
void set_mx_digits_1 ( word_type wt ) {
std::cout << "set_mx_digits_ 1(" << wt << ") called\n";
}
void set_mx_digits_2 ( double dt ) {
std::cout << "set_mx_digits_ 2(" << dt << ") called\n";
}
// lots more.
};

std::string FindFuncName(co nst std::string& buffer) {
indx_t k = buffer.find("fo o::*");
if (k==buffer.npos ) return "";
k += strlen("foo::*" );
k = buffer.find_fir st_not_of(" \t", k);
if (k==buffer.npos ) return "";
indx_t l = buffer.find_fir st_of(" \t)", k);
if (l==buffer.npos ) return "";
return buffer.substr(k , l-k);
}

std::string FindArgType(con st std::string& buffer) {
indx_t k = buffer.rfind("( ");
if (k==buffer.npos ) return "";
++k;
k = buffer.find_fir st_not_of(" \t", k);
if (k==buffer.npos ) return "";
indx_t l = buffer.find_fir st_of(" \t)", k);
if (l==buffer.npos ) return "";
return buffer.substr(k , l-k);
}

std::string FindArgument(co nst std::string& buffer) {
indx_t k = buffer.rfind(", ");
if (k==buffer.npos ) return "";
++k;
k = buffer.find_fir st_not_of(" \t", k);
if (k==buffer.npos ) return "";
return buffer.substr(k );
}
// Something to silence compiler warnings and errors on invalid
// function and argument combinations.
template<typena me TT ConvertForDigit s1(T x) {return x;}
int ConvertForDigit s1(double x) {/* should be never called*/ return 0;}

template<typena me TT ConvertForDigit s2(T x) {return x;}

template<typena me T>
void CallMemberFunc( const std::string& funcname, const std::string&
argstring, foo& myfoo) {
std::istringstr eam argstream(argst ring);
T arg1;
if (argstream >arg1) {
if (funcname=="set _mx_digits_1") {
myfoo.set_mx_di gits_1(ConvertF orDigits1(arg1) );
} else if (funcname=="set _mx_digits_2") {
myfoo.set_mx_di gits_2(ConvertF orDigits2(arg1) );
} // lots more
}
}

void ReadParseAndExe cute(const std::string& filename, foo& myfoo) {
std::ifstream is(filename.c_s tr());
std::string buffer;
while(std::getl ine(is, buffer)) {

// Parse the input line
std::string funcname = FindFuncName(bu ffer);
std::string argstring = FindArgument(bu ffer);
std::string argtype = FindArgType(buf fer);

// dispatch to template according to argument type
if (argtype=="word _type") {
CallMemberFunc< word_type>(func name, argstring, myfoo);
} else if (argtype=="doub le") {
CallMemberFunc< double>(funcnam e, argstring, myfoo);
} // some more
}
}

int main() {

{
// Prepare an example file
std::ofstream os("input_file. dat");
os <<
"typedef void ( foo::*set_mx_di gits_1 )( word_type wt ), 15\n";
os <<
"typedef void ( foo::*set_mx_di gits_2 )( double dt ), 3.6\n";
}
foo myfoo;
ReadParseAndExe cute("input_fil e.dat", myfoo);
}
Nov 17 '08 #6
int main() {

* * * * {
* * * * * * * * // Prepare an example file
* * * * * * * * std::ofstream os("input_file. dat");
* * * * * * * * os <<
* * * * * *"typedef void ( foo::*set_mx_di gits_1 )( word_typewt ), 15\n";
* * * * * * * * os <<
* * * * * *"typedef void ( foo::*set_mx_di gits_2 )( double dt), 3.6\n";
* * * * }

* * * * foo myfoo;
* * * * ReadParseAndExe cute("input_fil e.dat", myfoo);

}
You cleared up my confusion. Thanks

Nov 17 '08 #7

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

Similar topics

3
5189
by: Lance | last post by:
Hi All, I would like to read commands from a file and execute them in VB, something like reading str = "hello world" into VB and execute it. Any suggestions will be appreciated. Thanks, Lance
5
2930
by: Pete | last post by:
I having a problem reading all characters from a file. What I'm trying to do is open a file with "for now" a 32bit hex value 0x8FB4902F which I want to && with a mask 0xFF000000 then >> right shift 24 bits storing in result then printing the result. I thing a while or for loop is needed but I'm not quite sure how to go about it. How do I step through each character in this case and store it for use and passing to another function. ...
2
3579
by: Dave Griffin | last post by:
This is a followup to my question in doing unmanaged DLLs in .NET. I was able to create the DLL and the application was able to see it. So far so good. Now I need to make that unmanaged DLL read an XML file which contains a scenario I plan to follow within the library. Basically this DLL is an interface to a 1553 network with data being sent and received. By creating an artificial version, I hope to pretend to send and receive a huge...
7
2040
by: msxkim | last post by:
How to execute functions in the parent class first and then functions in the child class? For example, I have a parent class with functions 'ONE' and 'TWO' and child class has a function 'THREE'. How should I declare classes so that all three functions are executed when child class is called? Class Parent public function ONE 'code end fuction
3
8283
by: eholz1 | last post by:
Hello PHP Group, I am having trouble setting permissions correctly so that the magickwand api (php 5.2) can read and write images. I usually read a file from one directory, create a magickwand resource from that file, and transform the image, and save the new image with a new name to a different directory. I have seen that my file and folder permissions when set incorrectly,
23
3007
by: asit dhal | last post by:
hello friends, can anyone explain me how to use read() write() function in C. and also how to read a file from disk and show it on the monitor using onlu read(), write() function ??????
9
4340
by: lajet | last post by:
I need to read some word file from my c++ project through user interface. I'm using dev C++ and FLTK GUI tool kit. If any of you have an idea how to read/execute word file from c++ project, please let me know. Or if you can recommend some tools/functions that can read the word file, it would also be helpful. Thanks much in advance.
70
1059
by: quickcur | last post by:
hi can anyone explain me to read image to memory from a url it is very easy in java but it is hard to find an complete solution in c/c++. Thanks,
3
8911
by: sam | last post by:
same as subject?
0
9680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10012
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6788
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
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 we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.