简体   繁体   English

C++ map 模板到其他模板,然后到元组

[英]C++ map templates to other template, then to tuple

I'm working on some low level drivers and I thought I'd use C++ templates to spice it up.我正在研究一些低级驱动程序,我想我会使用 C++ 模板来增加它的趣味性。

In Verilog, you can define bitmasks using [h:l] notation where h is the high bit, l is the low bit.在 Verilog 中,您可以使用[h:l]表示法定义位掩码,其中h是高位, l是低位。 I have a data structure already defined for this:我已经为此定义了一个数据结构:

uint16_t mask(uint8_t h, uint8_t l) {
  return h << 8 + l;
}

I'd like to memoize these bitmasks.我想记住这些位掩码。 Say I want to modify bits [7:4] of register R, if I've already read in R before hand I can just modify the old value of R before writing it, rather than sending out a new read request to my device.假设我想修改寄存器 R 的位 [7:4],如果我之前已经读过 R,我可以在写入之前修改 ZE1E1D3D40573127E9EE0480CAF1283D6 的旧值,而不是向我的设备发送新的读取请求。

To find the number of bytes needed for each memoization, I already have a function为了找到每个记忆所需的字节数,我已经有一个 function

constexpr uint8_t requiredBytes(uint16_t mask)

which returns the number of bytes needed to hold the mask.它返回保存掩码所需的字节数。

Is there a way to static-time declare a memoization buffer for ease of analysis?有没有办法静态时间声明一个记忆缓冲区以便于分析? (this will run on embedded platforms) (这将在嵌入式平台上运行)

I assume I need a pipeline like below:我假设我需要一个像下面这样的管道:

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<>

I know I can do transformation 2 using a series of template specializations, which leads to my question, how can we implement transformations 1 and 3?我知道我可以使用一系列模板特化来进行转换 2,这导致了我的问题,我们如何实现转换 1 和 3?

I presume that your step 2, which you've described as already a done deal, is something along the lines of我认为您的第 2 步(您已将其描述为已完成的交易)类似于以下内容

typename bytesType<n>::type

Where the template parameter n is the number of bytes, and this gives you the type via an appropriate specialization.其中模板参数n是字节数,这通过适当的特化为您提供类型。 If so, then this seems to be something along the lines of a straightforward pack expansion:如果是这样,那么这似乎是一个简单的包扩展:

template <uint16_t ...masks>
using tuple_mask=std::tuple<typename bytesType<requiredBytes(masks)>::type...>;

requiredBytes() is a constexpr, this provides the parameter to bytesType , which gives you the type. requiredBytes()是一个 constexpr,它为bytesType提供了参数,它为您提供了类型。 Then, pack-expand it, and feed it into a std::tuple declaration.然后,对其进行打包扩展,并将其输入到std::tuple声明中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM