这里我解释一下这个问题。 假设我在共享内存中加载了一个结构体 A。 结构体 A 可能是这样的:
typedef enum{
GPIO_1,
GPIO_2,
GPIO_3,
GPIO_4,
GPIO_5,
N_GPIO
} gpio;
struct B {
bool active;
bool status;
bool direction;
int timestamp;
int t1;
int t2;
};
struct A {
struct B gpio[N_GPIO];
};
还假设我有两个函数可以对 A 中的 B 结构之一进行操作:
bool set_status(gpio g, bool status, int t1, int t2);
bool activate(gpio g, bool active);
由于A是在共享内存中加载的,所以我需要在上面的两个函数中调用shmget和shmdt;第一个函数的伪代码应该是这样的:
bool set_status(gpio g, bool status, int t1, int t2) {
struct A shm = shmget();
struct B target = shm.gpio[g];
if(target.active) {
bool res = foo1(target, status, t1, t2); //do something on struct B indexed by g
shmdt();
return res;
else
return false;
}
第二个函数的伪代码应该是这样的:
bool activate(gpio g, bool active) {
struct A shm = shmget();
struct B target = shm.gpio[g];
if(target.active) {
bool res = foo2(target, active); //do something on struct B indexed by g
shmdt();
return res;
else
return false;
}
现在,有没有一种方法可以防止使用管理 shm 并检查 B.active 是否设置的通用代码?对我来说,这看起来像装饰器,即有一个管理 shm 的函数,检查 B.active 并调用其中的第二个函数,但问题是第二个函数可能没有唯一的签名(可能有不同的数字)参数)。
我想要这样的东西:
bool set_status(gpio g, bool status, int t1, int t2) {
return decorator(foo1, g, status, t1, t2); //do something on struct B indexed by g
}
bool activate(gpio g, bool active) {
return decorator(foo2, g, active); //do something on struct B indexed by g
}
装饰器以这种方式管理 shm 并检查目标 B.active 。
谢谢!
编辑:这是您可以重构的最小工作示例 https://github.com/oliviera9/c_decorator
最佳答案
您可以创建一个可变参数宏:
#define DECORATOR(fn, g, ...) \
struct A shm = shmget(); \
struct B target = shm.gpio[(g)]; \
if(target.active) { \
bool res = (fn)(__VA_ARGS__)\
shmdt(); \
return res; \
} else {\
return false; \
}
像这样使用它
bool set_status(gpio g, bool status, int t1, int t2) {
DECORATOR(foo1, g, status, t1, t2)
}
bool activate(gpio g, bool active) {
DECORATOR(foo2, g, active)
}
关于c - 如何在c中的另一个函数之前和之后执行公共(public)代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52513363/