How to enforce function signatures in a function declaration set in C?

I want to make sure that the feature set has the same signature in some C code. Ideally, I could define a new type that describes the return value and arguments of the function, and then declare my set of functions using this new type.

Also, is there a way to specify default values โ€‹โ€‹for the arguments to this typedef function?

+4
source share
5 answers
/* define a typedef for function_t - functions that return void */ /* and take an int and char parameter */ typedef void function_t( int param1, char param2); /* declare some functions that use that signature */ function_t foo; function_t bar; 

Now, when you define functions, there will be an error if they do not use the same signature as in typedef.

 void foo( int x, char c) { /* do some stuff */ return; } /* this will result in a compiler error */ int bar( int x, char c) { /* do some stuff */ return 1; } 

As for your new question (added October 20, 08): โ€œAlso, is there a way to specify default values โ€‹โ€‹for the arguments to this typedef function?โ€

No, there is no way to add default parameters to typedef. Of course, not in C, which does not support default options at all. Even in C ++ you cannot do this because the default value for a parameter is not part of this type. In fact, a class that overrides a virtual method from the base class may specify a different value for the default parameter (or even delete the default value altogether) - however, this is something that should not be done at all, as this will just cause confusion ( http: // www.gotw.ca/gotw/005.htm ).

If you use C ++, you can get the desired behavior using one of (or a combination) of the following:

  • abstract base classes
  • overload
  • template functions
  • macros

But it would be difficult to offer something without knowing more details about what exactly you are trying to accomplish. And I think that, probably, the result may well be quite hacked.

+13
source

This is similar to how a function pointer works:

  // Declaration of function with int arg returning int typedef int (*CALLBACK)(int); //Definition int myFunc(int arg) { return 0; } // Function pointer usage CALLBACK pFunc = myFunc; 
0
source

I donโ€™t think you can do it directly, but you can declare a pointer type of a function and then collect all your functions into an array. The compiler will tell you which of them do not match.

 typedef void (*MethodSig)(int, int); static MethodSig x[] = { fnA, fnB, ... }; 

Or use a macro to declare a function signature

 #define MyFunc(X) void X(int a, int b) 

Thus, they will all be the same.

0
source

You cannot prevent anyone from creating a function that has a signature. But you can control what you will call. Therefore, I assume that this is what you want.

A function that calls arbitrary functions will have a pointer to the function as a parameter. Since you mentioned typedef, you can define typedef earlier in the program, for example:

Suppose you need the signature functions "unsigned short id_for_allowed_functions (int x, char * y)":

 typedef unsigned short (*id_for_allowed_functions)(int, char*); 

Then your calling function:

 void calling_function (id_for_allowed_function x) { (*x)(3, "bla"); } 

And to feed the foo function to it:

 unsigned short foo(int x, char* y) { /* ... */ } calling_function(&foo); 
0
source

Here is an example of creating a list of functions mapped to a simple string.

First enter typedefs:

 typedef GenList *(*DBLISTLOADER)(Database *pDB, char *quellCD, char *profilName); typedef ObjDescription *(*DBCOLUMNLOADER)(); typedef struct dbinfo { char *dbName; DBLISTLOADER dbListLoader; DBCOLUMNLOADER dbColumnLoader; char *options; } DBINFO; 

Then the mapping table:

 DBINFO dbInfoList[] = { { "SRCDOC", loadSRCDOC, colSRCDOC, "q" }, { "PRF_CD", loadPRF_CD, colPRF_CD, "" }, { "MEDIA", loadMEDIA, colMEDIA, "" }, { NULL, NULL, NULL } }; 

Now, to find the function from this table and call it:

 while (dbInfoList[i].dbName != NULL) { if (strcmp(dbInfoList[i].dbName, szDatabase) == 0) { return (dbInfoList[i].dbListLoader)(pDB, quellCD, profilName); } i++; } 

Sorry if it's a little raw. Glued directly from our code; -)

0
source

Source: https://habr.com/ru/post/1277623/


All Articles