Archived

This topic is now archived and is closed to further replies.

Floating

flexible multiuse function...

Recommended Posts

Hi, I have an API (DLL) with many hundreds of commands but only about 40 functions. It looks like this:
enum { command1=0, command2,command3,command4, etc.};

bool function1(int commandID,int arg1,int arg2,float* arg3);
bool function2(int commandID,int arg1,float arg2,float* arg3);
bool function3(int commandID,int arg1,float arg2,char* arg3);
etc.
A same function can be called with different commandIDs, but the argument list has to match the function's argument list. Like this: function1(command1,intArg1,intArg2,floatPointer); function1(command3,intArg1,intArg2,floatPointer); This is working fine but I want to do it more easy and flexible. I want to declare my functions like this: bool function1(int commandID,void* arg1,void* arg2,void* arg3); so that I can pass just any argument type. The only problem is when an argument passed is not a variable, like that: function1(command1,100.0f,15,3); This won't compile. I tried to append a '&' to get the address of the constants but it doesn't work of course! What are my options? I could do: float a=100.0f; int b=15; int c=3; function1(command1,&a,&b,&c); but that's not nice at all. How can I call the function with constant values in one line? It this possible? I hope I was not too confusing (^_^; Thanks [edited by - Floating on September 3, 2003 8:45:17 PM]

Share this post


Link to post
Share on other sites
switch(commandID) {
case command1:
// insert code that should be in its own function
break;
case command2:
// insert more code that should be in its own function
break;
}
Why can't you just have separate functions if you want them to do different things?

[edited by - Raloth on September 3, 2003 10:20:21 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Raloth
switch(commandID) {
case command1:
// insert code that should be in its own function
break;
case command2:
// insert more code that should be in its own function
break;
}
Why can't you just have separate functions if you want them to do different things?

[edited by - Raloth on September 3, 2003 10:20:21 PM]


Because I have more than 200 commands! I cannot have a function for each. And new functions are added everyday!

It is much nicer to have to add just an entry in the enum field of the header file than to have to rewrite a function each time.

[edited by - Floating on September 3, 2003 12:10:23 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Lord Hen
So instead of 200 functions, you have 200 case blocks? Doesnt seem like its that much trouble to just make them functions...




Lord Hen


I don''t care about the source file, I care about the header file (that''s what people using my API will see!). I want my header file to be small, clean and easily readable like:


// Commands

enum{
loadScene=0, // arg1 is the file name

addPrimitive, // arg1 is the type, arg2 is the color

etc.

and not:

extern "C" __declspec(dllexport) bool loadScene(char* name);
extern "C" __declspec(dllexport) bool addPrimitive(int type,float* color);



If I add a new command which uses an already existing function, I don''t need to distribute everytime a new lib file. I can just distribute the dll and the header file

Share this post


Link to post
Share on other sites
quote:
Original post by Floating

enum { command1=0, command2,command3,command4, etc.};

bool function1(int commandID,int arg1,int arg2,float* arg3);
bool function2(int commandID,int arg1,float arg2,float* arg3);
bool function3(int commandID,int arg1,float arg2,char* arg3);
etc.


A same function can be called with different commandIDs, but the argument list has to match the function''s argument list. Like this:

function1(command1,intArg1,intArg2,floatPointer);
function1(command3,intArg1,intArg2,floatPointer);

This is working fine but I want to do it more easy and flexible. I want to declare my functions like this:

bool function1(int commandID,void* arg1,void* arg2,void* arg3);

so that I can pass just any argument type. The only problem is when an argument passed is not a variable, like that:

function1(command1,100.0f,15,3);

This won''t compile. I tried to append a ''&'' to get the address of the constants but it doesn''t work of course! What are my options?
I could do:

[edited by - Floating on September 3, 2003 8:45:17 PM]



Why would you pass a constant when your argument requires a pointer???

Perhaps:


// With a float pointer as the last arg

template<typename A, typename B>
function1(int command, A a, B b, float *c);

// Using a constant as the last arg

template<typename A, typename B, typename C>
function2(int command, A a, B b, C c);

Share this post


Link to post
Share on other sites
The obvious solution (and I don''t know why nobody has suggested it yet) is to have a single function using varargs (just like printf). In the source you have a big switch statement and the usual varargs code:
#include < cstdargs >
bool function( int commandID, ... )
{
va_list args;
va_start( commandID );

switch ( commandID )
{
and so on
The drawback is no type-checking.

Share this post


Link to post
Share on other sites
you should try with templates but if you want to do it your way just do



bool function1(int commandID,void* arg1,void* arg2,void* arg3)
{
int a=(int)arg1;
int b=(int)arg2;
etc...

}


and call the function like that

function1(command1,(void*)10,(void*)b,(void*)c);

Share this post


Link to post
Share on other sites
quote:
Original post by JohnBolton
The obvious solution (and I don''t know why nobody has suggested it yet) is to have a single function using varargs (just like printf). In the source you have a big switch statement and the usual varargs code:
#include < cstdargs >
bool function( int commandID, ... )
{
va_list args;
va_start( commandID );

switch ( commandID )
{
and so on
The drawback is no type-checking.


JohnBolton thanks a lot! That''s exactly what I was looking for

Share this post


Link to post
Share on other sites