Sign in to follow this  
Rasmadrak

Defining function pointers from a file?

Recommended Posts

Hi! Is there a way to dynamically load function pointers from a text-file ? For instance (somewhat pseudo-code):

typedef void (*pFunction)(int);

inline static void call( void (*function)(int),int Value)
{
    if (function)
        function(Value);
};

void printStuff(int i)
{
   std::cout << i;
}

someLoop()
{
     pFunction onCompletion;

     onCompletion = printStuff;  // --- works.
     call(onCompletion,1);

     std::string text = "printStuff";
     onCompletion = text;    // --- does not work.
     call(onCompletion,2);
};


Hopefully the code above isn't full of bugs since it's written from memory. Anyway, Is there a way to do version 2? My plan is to define a set of functions and then link these to various objects during loadtime. Is it possible, or will I have to resort to scripting in LUA or something similar? Thanks for your time! /Robert

Share this post


Link to post
Share on other sites
Look at the problem from other side.

Calling a function involves resolving the function, and passing it some parameters.

Language itself provides you with transparent way of doing that - functions are resolved by compiler during compilation process, and parameters are passed using stack or registers.

std::string text = "printStuff";


But string is not a function - what you're missing is resolution.

The only thing you need to make this work is to map
typedef void (*pFunction)(int);
to a string.

Obviously, each time function signatures change, you need to recompile. So you need to abstract parameter passing. Rather than passing ints, you pass abstract base containers for all possible variables.

In languages which support reflection you get all this for free, and in a very optimal manner. In some extreme Java frameworks, no implementations are hard-coded. Also, since Java is coded in C (or C++), it's obviously possible.

Lua and other script language bindings offer you the same in C++. You can also look up into implementing C++ reflection, which would allow you to construct classes and functions on-the-fly.

Share this post


Link to post
Share on other sites
A binary program doesn't know the name of the functions, just their addresses, so the program doesn't know what "printStuff" is.

The best way to do this is by using tables of function pointers. You set a list of functions in a table, then instead of loading the pointer from a file, you load the index of the function in the table to call.

Here's a simple example. Not 100% sure on the syntax because I don't use this often, but it should give you the idea.
#include <iostream>

/* The two functions to call. */
void SayHello()
{
std::cout << "Hello" << std::endl;
}

void SayGoodbye()
{
std::cout << "Goodbye!" << std::endl;
}


/* The table of function pointers. */
void ( *g_pfnFuncTable[] )() =
{
&SayHello, /* This is the function at index 0 */
&SayGoodbye /* This is the function at index 1 */
};


/* The main function. */
int main()
{
int nFunctionToCall;

/* Open a binary file containing the index of the function to call. */
FILE * f = fopen( "test.dat", "rb" );

/* Read the index of a function to call. */
fread( f, &nFunctionToCall, sizeof( int ));

/* Call the function loaded. Normally, I would test the index
before using it to ensure that it is not out of range... */

( g_pfnFuncTable[ nFunctionToCall ])();

/* Close the file. */
fclose( f );

return 0;
}




Of course, you can also use a switch/case to call the right function.

Share this post


Link to post
Share on other sites
Hi!

It just hit me when you mentioned std::map´ing, that I've already done this in my GUI-system! A little modification here and there and now I'm a happy camper! :)

Scripting via LUA (or any other systeam) would be a bit overkill for this project, since I only need to map certain objects to a specific function at this time.

Thanks for your help!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this