检查函数是否与 C 中的 typedef 函数类型相同?
Check if function is the same type as my typedef function in C?
我有以下代码:
typedef int state_requester(void* request_paramaters);
int doSomething(state_requester* requester) {
// This if statement is what I'm trying to
// figure out how to do.
if (requester isoftype state_requester) {
requester(NULL);
return 1;
}
return 0;
}
我正在尝试找到一种方法来验证
传递的参数实际上是有效的 state_requester
尝试执行之前的函数格式。
上面的代码只是一个例子,显然不是真正的工作方式。我只需要知道如何 if ( x isoftype y )
关于函数类型定义。
你不能在 C 中这样做。
在编译时检查类型,而不是运行时。
注意:requester
是类型 state_requester*
,而不是 state_requester
。
虽然没有真正的方法可以直接验证数据是否属于特定类型,但我整理了一种方法 'presume' 类型匹配。
这并不能 100% 保证它将是指定的类型。但是,它会假定它是基于类型代码。
我还添加了一些测试。我在几分钟内写了这篇文章,所以我确信它还有改进的余地,而且我绝不是原生开发人员。我刚好有需要就写了。如果对某人有帮助,太棒了!
#include <stdio.h>
#include <stdlib.h>
// Type Definitions
// These are the type definitions that can be used to check for type
// They need to be unique so that only types starting with these
// prefixes will be presumed to be of the type specified.
typedef enum type {
TYPE_StateRequester = 0x10f3ccd3,
TYPE_OtherThing = 0x09331c8c
} type;
// State Requester Definitions & Functions
typedef int state_requester_f(void* request_parameters);
// Since we want to check the "type" of a function reference,
// we need to wrap it in a struct along with it's type code.
struct state_requester_t {
int type;
state_requester_f* call;
} typedef state_requester_t;
state_requester_t new_state_requester(state_requester_f* call) {
state_requester_t sr;
sr.type = TYPE_StateRequester;
sr.call = call;
return sr;
}
// This function will check if data is of type StateRequester
//
// If the data sent to this function is not of the length
// nessecary for the state_requester type, - OR -
// it's type property is not equal to the StateRequester type
// then we will presume that this data is NOT of type StateRequester
uint8_t is_state_requester(void* data, size_t len) {
return len == sizeof(state_requester_t) &&
((state_requester_t*)data)->type == TYPE_StateRequester;
}
// Testing functions
int test_caller(void* data) {
printf("Got to the test_caller function.\n");
return 0;
}
int main(int agc, char *argv[]) {
// Create an actual state_requester_t definitiion
state_requester_t sr = new_state_requester(&test_caller);
// Test to see if it's validated as a state_requester_t
if (is_state_requester(&sr, sizeof(state_requester_t))) {
printf("Yes, variable 'sr' is presumadely of type state_requester_t\n");
sr.call(NULL);
} else {
printf("No, variable 'sr' is not of type state_requester_t\n");
}
// Create a phony state_requester_t definition,
// but keep it the same length as a regular
// state_requester_t
void* a = malloc(sizeof(state_requester_t));
// Test to see if it's validated as a state_requester_t
if (is_state_requester(a, sizeof(state_requester_t))) {
printf("Yes, variable 'a' is presumadely of type state_requester_t\n");
} else {
printf("No, variable 'a' is not of type state_requester_t\n");
}
free(a);
// Create a phony state_requester_t with an improper length
void* b = malloc(5);
// Test to see if it's validated as a state_requester_t
if (is_state_requester(b, sizeof(state_requester_t))) {
printf("Yes, variable 'b' is presumadely of type state_requester_t\n");
} else {
printf("No, variable 'b' is not of type state_requester_t\n");
}
free(b);
return 0;
}
我有以下代码:
typedef int state_requester(void* request_paramaters);
int doSomething(state_requester* requester) {
// This if statement is what I'm trying to
// figure out how to do.
if (requester isoftype state_requester) {
requester(NULL);
return 1;
}
return 0;
}
我正在尝试找到一种方法来验证
传递的参数实际上是有效的 state_requester
尝试执行之前的函数格式。
上面的代码只是一个例子,显然不是真正的工作方式。我只需要知道如何 if ( x isoftype y )
关于函数类型定义。
你不能在 C 中这样做。
在编译时检查类型,而不是运行时。
注意:requester
是类型 state_requester*
,而不是 state_requester
。
虽然没有真正的方法可以直接验证数据是否属于特定类型,但我整理了一种方法 'presume' 类型匹配。
这并不能 100% 保证它将是指定的类型。但是,它会假定它是基于类型代码。
我还添加了一些测试。我在几分钟内写了这篇文章,所以我确信它还有改进的余地,而且我绝不是原生开发人员。我刚好有需要就写了。如果对某人有帮助,太棒了!
#include <stdio.h>
#include <stdlib.h>
// Type Definitions
// These are the type definitions that can be used to check for type
// They need to be unique so that only types starting with these
// prefixes will be presumed to be of the type specified.
typedef enum type {
TYPE_StateRequester = 0x10f3ccd3,
TYPE_OtherThing = 0x09331c8c
} type;
// State Requester Definitions & Functions
typedef int state_requester_f(void* request_parameters);
// Since we want to check the "type" of a function reference,
// we need to wrap it in a struct along with it's type code.
struct state_requester_t {
int type;
state_requester_f* call;
} typedef state_requester_t;
state_requester_t new_state_requester(state_requester_f* call) {
state_requester_t sr;
sr.type = TYPE_StateRequester;
sr.call = call;
return sr;
}
// This function will check if data is of type StateRequester
//
// If the data sent to this function is not of the length
// nessecary for the state_requester type, - OR -
// it's type property is not equal to the StateRequester type
// then we will presume that this data is NOT of type StateRequester
uint8_t is_state_requester(void* data, size_t len) {
return len == sizeof(state_requester_t) &&
((state_requester_t*)data)->type == TYPE_StateRequester;
}
// Testing functions
int test_caller(void* data) {
printf("Got to the test_caller function.\n");
return 0;
}
int main(int agc, char *argv[]) {
// Create an actual state_requester_t definitiion
state_requester_t sr = new_state_requester(&test_caller);
// Test to see if it's validated as a state_requester_t
if (is_state_requester(&sr, sizeof(state_requester_t))) {
printf("Yes, variable 'sr' is presumadely of type state_requester_t\n");
sr.call(NULL);
} else {
printf("No, variable 'sr' is not of type state_requester_t\n");
}
// Create a phony state_requester_t definition,
// but keep it the same length as a regular
// state_requester_t
void* a = malloc(sizeof(state_requester_t));
// Test to see if it's validated as a state_requester_t
if (is_state_requester(a, sizeof(state_requester_t))) {
printf("Yes, variable 'a' is presumadely of type state_requester_t\n");
} else {
printf("No, variable 'a' is not of type state_requester_t\n");
}
free(a);
// Create a phony state_requester_t with an improper length
void* b = malloc(5);
// Test to see if it's validated as a state_requester_t
if (is_state_requester(b, sizeof(state_requester_t))) {
printf("Yes, variable 'b' is presumadely of type state_requester_t\n");
} else {
printf("No, variable 'b' is not of type state_requester_t\n");
}
free(b);
return 0;
}