Building the name of an executable function
Question posted by: Miner Jeff
(Guest)
on
August 21st, 2008 02:55 PM
I need to build the name of a function from data in a text file. For
example, after reading the variables 'FCTN_PREFIX' AND 'FCTN_SUFFIX'
from the text file, I need to execute the function:
FCTN_PREFIX & 'TXT1' & FCTN_SUFFIX & 'TXT2';
where TXT1 and TXT2 are hardcoded text; & indicates concatenation.
I assume I could concatenate the text to build the function name and
give it a variable name but how can I then execute that function?
Thanks,
Jeffrey Bledsoe
4
Answers Posted
On Aug 21, 9:48*am, Miner Jeff <jeff.p.bled...@lmco.comwrote:
Quote:
Originally Posted by
I need to build the name of a function from data in a text file. For
example, after reading the variables 'FCTN_PREFIX' AND 'FCTN_SUFFIX'
from the text file, I need to execute the function:
>
FCTN_PREFIX & 'TXT1' & FCTN_SUFFIX & 'TXT2';
>
where TXT1 and TXT2 are hardcoded text; & indicates concatenation.
>
I assume I could concatenate the text to build the function name and
give it a variable name but how can I then execute that function?
I just posted on this topic on another thread. You might use std::map
to associate your string with a function, something like:
typedef void (*Fn)();
typedef std::map<std::string,FnMyMap;
MyMap myMap; // global for the sake of simplicity
void Hello() {/*...*/}
void World() {/*...*/}
void Call( const std::string& str )
{
MyMap::const_iterator it = myMap.find( str );
if( it != myMap.end() )
{
it->second();
}
}
int main()
{
myMap[ "hello" ] = &Hello;
myMap[ "world" ] = &World;
Call( "hello" );
Call( "world" );
}
Cheers! --M
"Miner Jeff" <jeff.p.bledsoe@lmco.comwrote in message
news:64e4a29b-3ab8-4425-9783-df020bdd2203@j22g2000hsf.googlegroups.com...
Quote:
Originally Posted by
>I need to build the name of a function from data in a text file. For
example, after reading the variables 'FCTN_PREFIX' AND 'FCTN_SUFFIX'
from the text file, I need to execute the function:
>
FCTN_PREFIX & 'TXT1' & FCTN_SUFFIX & 'TXT2';
>
where TXT1 and TXT2 are hardcoded text; & indicates concatenation.
>
I assume I could concatenate the text to build the function name and
give it a variable name but how can I then execute that function?
Here is something that might give you some sort of idea:
http://groups.google.com/group/comp...d2d698c65429230
You need to map funcname to funcptr, then create a lookup function.
Miner Jeff <jeff.p.bledsoe@lmco.comkirjutas:
Quote:
Originally Posted by
I need to build the name of a function from data in a text file. For
example, after reading the variables 'FCTN_PREFIX' AND 'FCTN_SUFFIX'
from the text file, I need to execute the function:
>
FCTN_PREFIX & 'TXT1' & FCTN_SUFFIX & 'TXT2';
>
where TXT1 and TXT2 are hardcoded text; & indicates concatenation.
>
I assume I could concatenate the text to build the function name and
give it a variable name but how can I then execute that function?
If you are working with dynamic load libraries (.dll, .so), you might
consider declaring all needed functions as extern "C" and export them from
the dynamic lib; then you can use relevant functions to load the lib and
obtain the actual function pointer.
This is all quite non-portable and better suited for plugin-like things, so
if this is not the case you will be better off with a hand-built map as
suggested by other posters.
Regards
Paavo
On Aug 21, 8:53*am, mlimber <mlim...@gmail.comwrote:
Quote:
Originally Posted by
On Aug 21, 9:48*am, Miner Jeff <jeff.p.bled...@lmco.comwrote:
>
Quote:
Originally Posted by
I need to build the name of a function from data in a text file. For
example, after reading the variables 'FCTN_PREFIX' AND 'FCTN_SUFFIX'
from the text file, I need to execute the function:
>
Quote:
Originally Posted by
FCTN_PREFIX & 'TXT1' & FCTN_SUFFIX & 'TXT2';
>
Quote:
Originally Posted by
where TXT1 and TXT2 are hardcoded text; & indicates concatenation.
>
Quote:
Originally Posted by
I assume I could concatenate the text to build the function name and
give it a variable name but how can I then execute that function?
>
I just posted on this topic on another thread. You might use std::map
to associate your string with a function, something like:
>
*typedef void (*Fn)();
*typedef std::map<std::string,FnMyMap;
>
*MyMap myMap; // global for the sake of simplicity
>
*void Hello() {/*...*/}
*void World() {/*...*/}
>
*void Call( const std::string& str )
*{
* *MyMap::const_iterator it = myMap.find( str );
* *if( it != myMap.end() )
* *{
* * *it->second();
* *}
*}
>
*int main()
*{
* *myMap[ "hello" ] = &Hello;
* *myMap[ "world" ] = &World;
>
* *Call( "hello" );
* *Call( "world" );
*}
>
Cheers! --M
Thanks. I had asked one of my colleagues if I could 'build' a function
name and he said the function names needed to be defined before
compiling; he explained that an interpreter type of program could do
that.
After I showed him your response, he agreed that your method would
work. In fairness to him, he is correct in his original assertion that
simply building function names during execution won't work.
Next question: I'll have to map several hundred of these functions. To
keep my main code much simpler, what would I have to do to move the
mapping code to a unique C++ file?
Thanks,
Jeff
|
|
|
What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 197,050 network members.
Top Community Contributors
|