define & prototype

Discussion in 'C Programming' started by adnan.yalin@gmail.com, Jun 22, 2007.

  1. Guest

    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
    , Jun 22, 2007
    #1
    1. Advertising

  2. Dave Hansen Guest

    On Jun 22, 9:05 am, wrote:
    > 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
    Dave Hansen, Jun 22, 2007
    #2
    1. Advertising

  3. wrote:

    > 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.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Jun 22, 2007
    #3
  4. adnan Guest

    On Jun 22, 5:40 pm, Christopher Benson-Manica
    <> wrote:
    > wrote:
    > > 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.
    >
    > --
    > C. Benson Manica | I *should* know what I'm talking about - if I
    > cbmanica(at)gmail.com | don't, I need to know. Flames welcome.



    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
    adnan, Jun 25, 2007
    #4
  5. Dave Hansen Guest

    On Jun 25, 1:54 am, adnan <> wrote:
    > On Jun 22, 5:40 pm, Christopher Benson-Manica
    >
    >
    >
    > <> wrote:
    > > wrote:
    > > > 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.

    >
    > > --
    > > C. Benson Manica | I *should* know what I'm talking about - if I
    > > cbmanica(at)gmail.com | don't, I need to know. Flames welcome.

    >
    > 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
    Dave Hansen, Jun 25, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. theotyflos
    Replies:
    3
    Views:
    465
    Thomas Matthews
    Feb 19, 2004
  2. June Lee
    Replies:
    2
    Views:
    800
    Jim Cobban
    Apr 13, 2008
  3. Replies:
    9
    Views:
    186
    Thomas 'PointedEars' Lahn
    May 26, 2006
  4. Replies:
    3
    Views:
    265
  5. javascript fish
    Replies:
    0
    Views:
    170
    javascript fish
    Oct 11, 2008
Loading...

Share This Page