Skip to main content
gamedev.net
🔒 Locked

Function Pointers (is there a way to...)

Started by Xorcist Mar 15, 2003 at 4:43 PM 4 replies 300+ views
Original Post
Xorcist
Xorcist
Is there a way to define a function pointer without a calling convetion, so that it may be pointed to two functions with different calling conventions? If not, is there any way I can setup a series of functions, so they''re calling convention is the same (say a pointer to an array) but that array itself can contain a series of variant data, which I''d have to parse through myself as a pseudo parameter set?
Qw3r7yU10p!
Qw3r7yU10p!
It sounds like you''re going to possibly get a result with switching based on type. That''s often an indicator of a bad solution.

What are you trying to solve?
Xorcist
Xorcist
I''m working on an input asbraction system. Each input is configured by the user, which is then pointed to an action function. So I basically have an array of function pointers. But instead of having each index be a simple pointer that can point to any given function, they have to be a specific pointers that point only to functions with the same calling convention.

So I''m assuming my choices are to make all the functions uniform in their calling conventions or find another way to declare the pointers so that I can point to functions which different parameter sets.

Maybe I just haven''t thought this through enough.

Please share any advice you might have.
Zipster
Zipster
My suggestion is to make an abstract base class which defines a virtual member function. You can then derive from this class for each input signal you have, and use base class pointers to store them and call the function.

Yes, you are limited in the calling convention of that virtual function, however, each derived class allows you to add its own member functions and data.

If you want, have your virtual function take a void pointer parameter, and each derived class function can interpret this pointer in a different way, whether it be not used, or maybe it points to a structure of some sorts, etc. Depends on the implementation of each function as to how it is used.
SiCrane
SiCrane
In C++ one way to achieve what I think you''re attempting is to use the Loki library''s Functor class.

Example code mixing _cdecl and _stdcall function pointers with Functor objects:

  
#include <iostream>

#include <loki/Functor.h>

void _cdecl function1(int i) {
std::cout << "_cdecl " << i << std::endl;
}

void _stdcall function2(int i) {
std::cout << "_stdcall " << i << std::endl;
}

int main(void) {
Loki::Functor<void, TYPELIST_1(int) > functor_array[2] = { function1, function2 };
functor_array[0](5);
functor_array[1](6);
}


You can download loki from:
http://sourceforge.net/projects/loki-lib/
LilBudyWizer
LilBudyWizer
I think you are misusing the term calling convention. A calling convention is a convention for passing parameters, i.e. what order are things pushed onto and popped off of the stack. The data type of parameters and number of parameters is not part of the calling convention. Rather that is simply the function definition.

Your if not is what a variable arguement list is. printf is a prime example. va_list and the related functions just give you access to the stack frame. You first tell it the name of the first paramter which says where on the stack you are starting. You then give it the type of each parameter which tells it how much to step by through the stack. It doesn't really work as an array, but rather a structure. No error checking is done on the variable part of the list. You can pass anything, right or wrong, as people often discover with printf. Your format string can say it is a string while the parameter you pass is an integer.

Generally it is a bad idea. With printf it is inherent in what you are doing. You are providing a format string which says what the number and types of the parameters are but are mainly specifying the formatting you want performed. Streams provide an alternative that isn't cumbersome. That also suggests an alternative. You could define a group of overloaded operators so that you are actually calling individual functions taking differant parameters but it looks like one operation. Templates, specifically template specialization, provides another alternative.

Overall if you have to decide which parameters you need to pass then this approach is a bad approach. The reason being that you could just as easily decide which function to call. So rather than just a list of functions to be called you would need a list of functions to call and the parameters to be passed. Then it might actually save you something.

Ok, so I'm slow. It looks like a case where it is an approriate approach. I think SiCrane's suggestion is about calling conventions which your question isn't really about. I'm not familar with Functors, but they may do what you need. There may be other things in Loki or Boost that handle what you need in a more controlled and structured manner than variable arguement lists do. Templates are compile time though so there are limits.

[edited by - LilBudyWizer on March 15, 2003 8:20:44 PM]
Keys to success: Ability, ambition and opportunity.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.