C++ 将模板映射到其他模板,然后映射到元组
C++ map templates to other template, then to tuple
我正在研究一些低级驱动程序,我想我会使用 C++ 模板来增加它的趣味性。
在 Verilog 中,您可以使用 [h:l]
表示法定义位掩码,其中 h
是高位,l
是低位。我已经为此定义了一个数据结构:
uint16_t mask(uint8_t h, uint8_t l) {
return h << 8 + l;
}
我想记住这些位掩码。假设我想修改寄存器 R 的 [7:4] 位,如果我之前已经读入 R,我可以在写入之前修改 R 的旧值,而不是向我的设备发送新的读取请求。
要找到每次记忆所需的字节数,我已经有一个函数
constexpr uint8_t requiredBytes(uint16_t mask)
其中 returns 保存掩码所需的字节数。
有没有一种静态时间声明内存缓冲区以便于分析的方法? (这将 运行 在嵌入式平台上)
我假设我需要如下管道:
template <uint16_t... masks>
(1) V
Apply requiredBytes() to each mask in the template
(2) V
Convert each value from requirdBytes to a type (requiredBytes() = 1 => uint8_t, ...)
(3) V
Convert this list of types into a std::tuple<>
我知道我可以使用一系列模板特化来执行转换 2,这引出了我的问题,我们如何实现转换 1 和 3?
我认为您所说的已经完成的第 2 步与
typename bytesType<n>::type
其中模板参数 n
是字节数,这会通过适当的特化为您提供类型。如果是这样,那么这似乎是一个简单的包扩展:
template <uint16_t ...masks>
using tuple_mask=std::tuple<typename bytesType<requiredBytes(masks)>::type...>;
requiredBytes()
是一个 constexpr,它为 bytesType
提供参数,后者为您提供类型。然后,对其进行打包扩展,并将其提供给 std::tuple
声明。
我正在研究一些低级驱动程序,我想我会使用 C++ 模板来增加它的趣味性。
在 Verilog 中,您可以使用 [h:l]
表示法定义位掩码,其中 h
是高位,l
是低位。我已经为此定义了一个数据结构:
uint16_t mask(uint8_t h, uint8_t l) {
return h << 8 + l;
}
我想记住这些位掩码。假设我想修改寄存器 R 的 [7:4] 位,如果我之前已经读入 R,我可以在写入之前修改 R 的旧值,而不是向我的设备发送新的读取请求。
要找到每次记忆所需的字节数,我已经有一个函数
constexpr uint8_t requiredBytes(uint16_t mask)
其中 returns 保存掩码所需的字节数。
有没有一种静态时间声明内存缓冲区以便于分析的方法? (这将 运行 在嵌入式平台上)
我假设我需要如下管道:
template <uint16_t... masks>
(1) V
Apply requiredBytes() to each mask in the template
(2) V
Convert each value from requirdBytes to a type (requiredBytes() = 1 => uint8_t, ...)
(3) V
Convert this list of types into a std::tuple<>
我知道我可以使用一系列模板特化来执行转换 2,这引出了我的问题,我们如何实现转换 1 和 3?
我认为您所说的已经完成的第 2 步与
typename bytesType<n>::type
其中模板参数 n
是字节数,这会通过适当的特化为您提供类型。如果是这样,那么这似乎是一个简单的包扩展:
template <uint16_t ...masks>
using tuple_mask=std::tuple<typename bytesType<requiredBytes(masks)>::type...>;
requiredBytes()
是一个 constexpr,它为 bytesType
提供参数,后者为您提供类型。然后,对其进行打包扩展,并将其提供给 std::tuple
声明。