define & prototype

A

adnan.yalin

Could you suggest solutions to a problem currently I have?
Problem:
Let's say I have many different commands and I call a different
function for each (void handle_command_1(*data), void
handle_command_2(*data), etc.) through my DispatchCommand(*data)
function.
For example, only implementing two of those functions is enough for an
executable, but for another executable other functions are necessary.
There can be many executables like this. The functions not implemented
would do nothing.
So basically if handle_command_1 function is implemented in the module
it will be called, but if not i should provide the function with
nothing in its body.
Thanks
 
D

Dave Hansen

Could you suggest solutions to a problem currently I have?
Problem:
Let's say I have many different commands and I call a different
function for each (void handle_command_1(*data), void
handle_command_2(*data), etc.) through my DispatchCommand(*data)
function.
For example, only implementing two of those functions is enough for an
executable, but for another executable other functions are necessary.
There can be many executables like this. The functions not implemented
would do nothing.
So basically if handle_command_1 function is implemented in the module
it will be called, but if not i should provide the function with
nothing in its body.

Something like this (WARNING: UNTESTED!)

extern void handle_cmd_1(void *data);
extern void handle_cmd_3(void *data);

#define NUM_CMDS 4

static void (*cmd_func[NUM_CMDS])(void *data) =
{
NULL, // Command 0 not handled
handle_cmd_1,
NULL, // Command 2 not handled
handle_cmd_3
};

void DispatchCommand(int cmd, void *data)
{
if (cmd < NUM_CMDS && cmd_func[cmd] != NULL)
{
cmd_func[cmd](data);
}
}

All that would change for programs implementing different commands are
the definition of cmd_func and perhaps NUM_CMDS.

Or did I misunderstand your question?

Regards,

-=Dave
 
C

Christopher Benson-Manica

Let's say I have many different commands and I call a different
function for each (void handle_command_1(*data), void
handle_command_2(*data), etc.) through my DispatchCommand(*data)
function.
So basically if handle_command_1 function is implemented in the module
it will be called, but if not i should provide the function with
nothing in its body.

It's not clear to me exactly what you're asking, but it sounds like
you could do something like this...


void (*command_1_handler)( void *data ) = NULL;

void handle_command_1( void *data ) {
if( command_1_handler != NULL ) {
command_1_handler( data );
}
}

and modules wishing to provide handlers for commands would simply
assign the handler function pointers appropriately.
 
A

adnan

It's not clear to me exactly what you're asking, but it sounds like
you could do something like this...

void (*command_1_handler)( void *data ) = NULL;

void handle_command_1( void *data ) {
if( command_1_handler != NULL ) {
command_1_handler( data );
}

}

and modules wishing to provide handlers for commands would simply
assign the handler function pointers appropriately.


Thanks for your answers,

Actually I dont want the client side to deal with any initilization
including assigning NULL to function pointers.I want only client side
to implement the routines and declare them in header files.

So let's say one implements handle_command_1 & handle_command_2 in
target1.c and thus target1.h will have "void handle_command_1(*data);"
and "void handle_command_2(*data);".

Similary another developer will implement handle_command_2 &
handle_command_5 in target2.c and so on.

So my module having "DispatchCommand" function includes either
target1.h or target2.h based on a defined symbol.

Therefore how should I write my module so that I would understand
which routines are implemented and consequently my DispatchCommand
routine would simply return for the routines not implemented. I tried
to use some ifdef switches but it didn't work.

Is there a solution to this?

Thanks
 
D

Dave Hansen

Thanks for your answers,

Actually I dont want the client side to deal with any initilization
including assigning NULL to function pointers.I want only client side
to implement the routines and declare them in header files.

So let's say one implements handle_command_1 & handle_command_2 in
target1.c and thus target1.h will have "void handle_command_1(*data);"
and "void handle_command_2(*data);".

Similary another developer will implement handle_command_2 &
handle_command_5 in target2.c and so on.

So my module having "DispatchCommand" function includes either
target1.h or target2.h based on a defined symbol.

Therefore how should I write my module so that I would understand
which routines are implemented and consequently my DispatchCommand
routine would simply return for the routines not implemented. I tried
to use some ifdef switches but it didn't work.

Is there a solution to this?

You could do something like this (again, untested):

--- target1.h
#define CMD_1 handle_command_1
#define CMD_2 handle_command_2

--- target2.h
#define CMD_2 handle_command_2
#deifne CMD_5 handle_command_5


--- dispatch.c
#include TARGET /* target1.h or target2.h or whatever */

#define NUM_CMDS 6

#ifdef CMD_0
extern void CMD_0(void *data);
#else
#define CMD_0 NULL
#endif

#ifdef CMD_1
extern void CMD_1(void *data);
#else
#define CMD_1 NULL
#endif

#ifdef CMD_2
extern void CMD_2(void *data);
#else
#define CMD_2 NULL
#endif

/* and so on for CMD_3, CMD_4, and CMD_5 */

static void (* const cmd_func[NUM_CMDS])(void *data) =
{ CMD_0, CMD_1, CMD_2, CMD_3, CMD_4, CMD_5 };

void dispatch(int cmd, void *data)
{
if (cmd < NUM_CMDS && cmd_func[cmd] != NULL)
{
cmd_func[cmd](data);
}
}


HTH,

-=Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top