如何在C ++中生成中断处理程序的编译时数组? [英] How can I generate a compile-time array of interrupt handlers in C++?
问题描述
我想在一个地方写我的ISR:
some_collection TimerHandlers;
//在链接描述文件中添加到ISR表
void rawTimerIRQHandler(){
call_each_handler_in(handlers);
}
这样我可以在其他文件中注册处理程序
// file1.cpp
void ledTimerHandler1(){
}
寄存器(ledTimerHandler1); //或在init函数中(如果不可能)
// file2.cpp
void ledTimerHandler2(){
}
register(ledTimerHandler2); //或在init函数中(如果不可能)
当硬件跳转到 rawTimerIRQHandler
,它以某种任意顺序执行 ledTimerHandler1
和 ledTimerHandler2
>
显然,我可以使用类似于向量< void(*)()>
,但由于这些处理程序的数量在编译时是已知的,有没有任何方式我可以在编译时生成数组(或模板链表)?我想避免向量
附带的动态内存分配。
我可以使用 template<>
, #define
,甚至GCC特定的属性。
脚手架有点乏味,但一旦完成使用不容易:
// example.h:
#includeRegistered.h
struct example:Registered< example> {};
// main。 cc:
#include< iostream>
#includeexample.h
int main()
{
for(auto p = example :: registry; p; p = p-&链)
std :: cout<< p < '\\\
';
}
// Registered.h:
template< class registered>
struct Registered {
static registered * registry;
registered * chain;
Registered():chain(registry){registry = static_cast< registered *>(this);}
};
cc:
#includeexample.h
template<> example * registered< example> :: registry = 0;
static struct示例first,second,third; //这些可以定义在任何地方w /静态持续时间
第一,第二,第三声明/定义以满足我的内部
I'd like to be able to write my ISR in one place:
some_collection TimerHandlers;
// added to ISR table in linker script
void rawTimerIRQHandler() {
call_each_handler_in(handlers);
}
Such that I can then register handlers in other files
// file1.cpp
void ledTimerHandler1() {
}
register(ledTimerHandler1); //or in an init function if not possible here
// file2.cpp
void ledTimerHandler2() {
}
register(ledTimerHandler2); //or in an init function if not possible here
And when the hardware jumps to rawTimerIRQHandler
, it executes ledTimerHandler1
and ledTimerHandler2
in some arbitrary order.
Obviously, I can implement this using something similar to a vector<void(*)()>
, but since the number of these handlers is known at compile-time, is there any way I can generate an array (or template linked list) at compile-time? I'd like to avoid the dynamic memory allocation that comes with vector
.
I'm open to using template<>
, #define
, or even GCC-specific attributes to acheive this goal.
The scaffolding's a bit tedious but once it's done the usage couldn't be simpler:
// example.h:
#include "Registered.h"
struct example : Registered<example> {};
// main.cc:
#include <iostream>
#include "example.h"
int main ()
{
for ( auto p = example::registry; p; p=p->chain )
std::cout << p << '\n';
}
// Registered.h :
template<class registered>
struct Registered {
static registered *registry;
registered *chain;
Registered() : chain(registry) {registry=static_cast<registered*>(this);}
};
// example.cc:
#include "example.h"
template<> example *Registered<example>::registry = 0;
static struct example first, second, third; // these can be defined anywhere w/ static duration
edit: moved the first,second,third
declaration/definitions to satisfy my inner pedant
这篇关于如何在C ++中生成中断处理程序的编译时数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!