如何向函数指针数组中的每个函数添加标准例程? [英] How to add standard routine to every function in array of function pointers?

查看:98
本文介绍了如何向函数指针数组中的每个函数添加标准例程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C ++为嵌入式系统创建一个应用程序.因此,我与某些C库绑定在一起,并且没有使用大量std功能的自由.

I'm creating an application in C++ for an embedded system. Therefore I'm tied to some C libraries, and do not have the freedom to use a lot of std functionality.

我有一个回调签名:

typedef int (*callback_fn)(int a, int b);

包含指向已注册回调的指针的列表:

A list with pointers to the registered callbacks:

callback_fn my_callback_functions[10];

还有一个将回调添加到此列表的函数:

And a function which adds callbacks to this list:

void add_callback(int function_iterator, callback_fn fn)
{
    my_callback_functions[function_iterator] = fn;
}

然后将回调传递给该库,并在发生某些事件时调用回调.

The callbacks then get passed to this library, and it calls the callbacks upon some events.

现在,我想为每个回调添加一个小例程,这样我可以在每次调用时重置计时器:

Now I'd like to add a little routine to every callback, so I can reset a timer every time a call has been made:

myTimer.reset();

如何在不更改签名或不离开函数指针(我需要与库进行交互)的情况下将其添加到每个调用中?

How can I add this to every call without changing the signature, or moving away from function pointers (which I need to interact with the library)?

我尝试使用模板做一些事情,但是找不到正确的方法来完成这项工作.

I tried some things with templates but couldn't find the right way to make this work.

推荐答案

让我们从幼稚起步,然后再添加复杂性.

Let's start naive, then add sophistication.

添加例程的最简单方法是编辑回调函数.也就是说,如果您的回调之一是fun1(),则可以将myTimer.reset();行添加到函数主体的开头.

The simplest way to add your routine would be to edit the callback function. That is, if one of your callbacks is fun1(), you could add the line myTimer.reset(); to the beginning of the function body.

除了...也许并非每次对此函数的调用都应该重置计时器(不要忘记允许将来进行更改).我们可能不太天真,可以创建一个包装函数来重置计时器,然后调用fun1().回调可以调用此包装,而其他调用使用原始函数.

Except... maybe not every call to this function is supposed to reset the timer (don't forget to allow for future changes). We can be less naive and create a wrapper function that resets the timer then calls fun1(). The callbacks can call this wrapper, while other invocations use the original function.

int wrap1(int a, int b)
{
    myTimer.reset();
    return fun1(a, b);
}

除了...我很懒.我不想为每个回调写一个包装器.我将把任务交给其他人,一个不会抱怨我的懒惰的人,一个像gcc这样的人(或者您选择的任何编译器).我宁愿编写一个模板来显示如何编写此包装器,然后让编译器根据需要生成包装器.变化的一件事是回调函数,因此它成为模板参数.

Except... I'm lazy. I don't want to write a wrapper for each and every callback. I'll just hand that task off to someone else, someone who won't complain about my laziness, someone like gcc (or whatever your compiler of choice is). I'd rather write a template showing how to write this wrapper, then let the compiler generate wrappers as needed. The one thing that varies is the callback function, so that becomes the template argument.

template <callback_fn F>
int wrap(int a, int b)
{
    myTimer.reset();
    return F(a, b);
}

现在,您可以使用如下代码行添加回调.

Now you can add your callbacks with lines like the following.

    add_callback(0, wrap<fun1>);
    add_callback(1, wrap<fun2>);

这可以概括为处理除callback_fn之外的其他指针类型,但是我将推迟过度设计解决方案.如果某人需要更高级的技巧,则该人总是可以提出一个新问题来引用该问题.

This could be generalized to handle pointer types other than callback_fn, but I'll hold off on over-engineering a solution. If someone needs even more sophistication, that person could always ask a new question that references this one.

这篇关于如何向函数指针数组中的每个函数添加标准例程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆