Looking at some OSCORE code here: https://github.com/Fraunhofer-AISEC/uoscore-uedhoc/blob/main/modules/oscore/src/option.c and at lines 31 and 63 there is some unusual casting and pointer function work I could use help with.
bool (*class_to_condition(enum option_class class))(uint16_t code)
{
switch (class) {
case CLASS_I:
return is_class_i; //is_class_x() returns bool
case CLASS_E:
return is_class_e;
default:
break;
}
return false;
}
and later
bool (*condition)(uint16_t) = class_to_condition(class);
then used as
if (!condition(code)) {
I get:
- class_to_condition(u16) will return a bool
- "condition" variable is a pointer to a the function class_to_condition, and this function will accept one argument
- uint16_t is the argument type the function accepts
I don't get
- the enum
- why it appears to take two arguments (sort of)
- Why is the function definition a pointer it self
- uint16_t "code" is never used in the class_to_condition() ?
Is this so much more efficient as a pointer over the obvious non-pointer alternative that it's worth the increased complexity (clever vs cute)?