How to handle different functions with function pointers?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to handle different functions with function pointers?

1,999 Views
anunalge
Contributor I
In my program i need to handle different type of functions.I created some structure for accessing different functions.

typedefstruct{
UCHAR (*const GetFunc)(void
);
}
UCHAR_CPMTS;

typedef struct{
UINT (*const GetFunc)(void
);
}UINT_CPMTS;

typedefstruct{
void
(*constSetFunc)(UCHAR);
}
UCHAR_CPMTS_INIT;

typedef
struct{
void
(*constSetFunc)(UINT);
}UINT_CPMTS_INIT;

 

static

const UCHAR_CPMTS UCHAR_GET_FUNC_LIST[] =
{
PROD_SPEC_UCHAR_GET_FUNC_LIST
};
static const UINT_CPMTS UINT_GET_FUNC_LIST[] =
{
PROD_SPEC_UINT_GET_FUNC_LIST
};
static const UCHAR_CPMTS_INIT UCHAR_SET_FUNC_LIST[] =
{
PROD_SPEC_UCHAR_SET_FUNC_LIST
};
static const UINT_CPMTS_INIT UINT_SET_FUNC_LIST[] =
{
PROD_SPEC_UINT_SET_FUNC_LIST
};

#definePROD_SPEC_UCHAR_GET_FUNC_LIST  \

           GetButtonState   \

           NULL


 

using this pointers i can handle only  functions having uchar,uint return types and as arguements.But i need to handle SCHAR, as well as functions with more no of arguements.How to handle them in generic way,Instead of creating one structure for every fuction type.Please help me.I need the solution very urgent.

Thanks,

Anuradha.

Labels (1)
0 Kudos
2 Replies

332 Views
Lundin
Senior Contributor IV
typedef void(FuncType)(void);
typedef int(AnotherFuncType)(void);
typedef void(YetAnotherFuncType)(int);

typedef union
{
  struct
  {
    FuncType func1;
    AnotherFuncType func2;
    YetAnotherFuncType func3;
  };

  FuncType func_array[3];

} FuncTypeContainer;


void func_a (void);
int  func_b (void);
void func_c (int x);

FuncTypeContainer ftc;
ftc.func_array =
{
  (void*)func_a,
  (void*)func_b,
  (void*)func_c
};



The above is flexible and will work flawlessly on Codewarrior (as long as you don't place functions in banked memory etc, then you will have to adapt the program to that with far pointers or similar). However, there are some important things you need to be aware of:

You can't call the functions through the func_array. You must call them through their individual function name or you are invoking undefined behavior. Function pointer typecasts will work perfectly on CW, but they are undefined behavior in ISO C so the code will not be portable if you do that.

Also, the code won't work on compilers using struct padding, which might also make it non-portable.
0 Kudos

332 Views
CompilerGuru
NXP Employee
NXP Employee
Not sure what the setup will be used for.
In general, any special reason why using structs containing function pointers, and not just function pointers?

typedef
UINT(*constUINT_CPMTS_INIT)(UINT);

Also is it necessary to support all different kinds of prototypes (signed char, unsigned char, int,...), can not a combined one, taking a larger type as UINT serve for all the smaller cases too?

Also what are the operations on those arrays, maybe using a dedicated function which does the dispatching in code (instead of a data driven way) can handle the cases too.

Daniel

0 Kudos