473,411 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Building the name of an executable function

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
Aug 21 '08 #1
4 1409
On Aug 21, 9:48*am, Miner Jeff <jeff.p.bled...@lmco.comwrote:
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
Aug 21 '08 #2
"Miner Jeff" <je************@lmco.comwrote in message
news:64**********************************@j22g2000 hsf.googlegroups.com...
>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....2d698c65429230

You need to map funcname to funcptr, then create a lookup function.

Aug 21 '08 #3
Miner Jeff <je************@lmco.comkirjutas:
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
Aug 21 '08 #4
On Aug 21, 8:53*am, mlimber <mlim...@gmail.comwrote:
On Aug 21, 9:48*am, Miner Jeff <jeff.p.bled...@lmco.comwrote:
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
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
Aug 25 '08 #5

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

Similar topics

0
by: PatchFactory Support | last post by:
Description: Professional and easy-to-use patch building environment that can help you to create instant patch packages for software and file updating. Generated patch packages are small size...
8
by: Olaf Meyer | last post by:
Sometimes if find it clumsy unsing the following approach building strings: cmd = "%s -start %s -end %s -dir %s" % (executable, startTime, endTime, directory) Especially if you have a lot of...
33
by: Jacek Generowicz | last post by:
I would like to write a metaclass which would allow me to overload names in the definition of its instances, like this class Foo(object): __metaclass__ = OverloadingClass att = 1 att = 3
2
by: StudioRookie | last post by:
Hi I'm trying to get familiar with Visual Studio .NET. I've built a project in C# as an executable on an XP box. When I copy the executable to another XP box and run it it complains:...
3
by: Leith Bade | last post by:
I have been trying to use the new Visual C++ Toolkit 2003 with the VC6 IDE I set up the executable, inlcude, and library directories to point to the new compilers I had to fix a few errors in the...
2
by: mshngo | last post by:
Hi, I am having the following trouble in compiling DLL on windows. Suppose I want to compile module A into a DLL library, and I want to declare an external interger n in A. My intention is to...
15
by: dspfun | last post by:
Hi, Is it possible to print the function name of the calling function? For example, f1() and f2() both calls f3(), in f3() I would like to print the name of the function calling f3() which...
33
by: Peng Yu | last post by:
Hi, __PRETTY_FUNCTION__ is a macro that gives function name, etc. I'm wondering if there is a macro to get the class name inside a member function. Thanks, Peng
12
by: sinbad | last post by:
hi, is it possible to get function name from function pointer. I am using gcc compiler on linux. thanks
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.