我正在尝试找出一种为AVR体系结构构建复合编译时数组的智能方法。阵列应如下结构:
- 它应该完全存在于程序内存;
- 它由连续的一系列(未签名)字节组成,又称
uint8_t
; - 应该使用任意长度的字节段构建;
- 按顺序构成一个长度字节和一系列数据字节的段,长度字节是数据字节的数量。
这是这样一个数组的示例:
static const uint8_t data[] PROGMEM = {
1, 0x01,
3, 0xBE, 0x02, 0x00,
3, 0x3D, 0x33, 0x33,
15, 0xE1, 0xD0, 0x00, 0x05, 0x0D, 0x0C, 0x06, 0x2D, 0x44, 0x40, 0x0E, 0x1C, 0x18, 0x16, 0x19,
0 /* end of the sequence */
};
我想避免每次我添加或从序列中删除字节时调整长度字节的负担,例如,以某种形式的伪代码:
BEGINNING_OF_THE_SEQUENCE(identifier)
SEGMENT(0x01),
SEGMENT(0xBE, 0x02, 0x00),
...
END_OF_THE_SEQUENCE()
在上面的示例中,我选择了一个显式字节数组声明,但可以以任何方式构建,例如使用结构。唯一的先决条件是必须保证外观的顺序。
简而
我已经考虑过使用variadic宏,但我也想研究其他手段,例如类和功能模板,元编程,what,what what,what what the not,刻住了最小的代码。我也希望不诉诸C 11特异性,因为我正在使用的当前avr-gcc
编译器的支持是有限的。
我有一个预感,可以使用模板,但我被卡住了。有什么想法吗?
这是C 11及以后的简单示例,可能会有所帮助:
template <typename ...Args>
constexpr std::size_t n_args(Args...) { return sizeof...(Args); }
#define ELEM(...) n_args(__VA_ARGS__), __VA_ARGS__
#include <iostream>
int main()
{
unsigned int a[] = { ELEM(4, 9, 16),
ELEM(100),
ELEM(10, 20, 30, 40, 50),
};
for (auto n : a ) std::cout << n << " ";
std::cout << 'n';
}
另外,您可以使用sizeof
代替n_args
的复合char-array文字,那就是您想要C99解决方案而不是C 11一个:
#define ELEM(...) sizeof((char[]){__VA_ARGS__}), __VA_ARGS__
我不知道在C 03中使用的类似简单的方法。
这是一种方法:
#include <stdint.h>
#include <stdio.h>
#define PROGMEM
#define _ARGSIZE(...) sizeof((uint8_t[]){__VA_ARGS__})/sizeof(uint8_t)
#define _SEGMENT(...) _ARGSIZE( __VA_ARGS__ ), __VA_ARGS__
#define BEGINNING_OF_THE_SEQUENCE(__id) uint8_t static const __id[] PROGMEM = {
#define END_OF_THE_SEQUENCE() }
BEGINNING_OF_THE_SEQUENCE(data)
_SEGMENT(0x01),
_SEGMENT(0xBE, 0x02, 0x00),
_SEGMENT(0xDE, 0xAD, 0xBE, 0xEF)
END_OF_THE_SEQUENCE();
int main() {
int k, counter = data[0];
for (k = 0; k < sizeof(data); k++) {
fprintf(stderr, "%02x ", data[k]);
if(counter-- == 0) {
counter = data[1+k];
fprintf(stderr, "n");
}
}
}
这种方法是C99兼容。
上面的宏可以在某些方面进行修改,以照顾通过的任何类型的数据结构而不是UINT8_T(包括:结构内的P结构)
无宏C 11方法(v2):
#include <array>
#include <iostream>
#include <cstddef>
#include <cstdint>
// This template will be instantiated repeatedly with VItems list
// populated with new items.
template<typename TItem, TItem... VItems> class
t_PackImpl
{
// This template will be selected for second and all other blocks.
public: template<TItem... VInnerItems> using
t_Pack = t_PackImpl
<
TItem
// add all previous items
, VItems...
// add item holding amount of items in new block
, TItem{static_cast<TItem>(sizeof...(VInnerItems))}
// add new block items
, VInnerItems...
>;
// This method will be called on the last instantiated
// template with VItems containing all the items.
// Returns array containing all the items with extra 0 item at the end.
public: static constexpr auto
to_array(void) -> ::std::array<TItem, sizeof...(VItems) + ::std::size_t{1}>
{
return {VItems..., TItem{}};
}
};
// This template will be instantiated just once.
// Starts t_PackImpl instantiation chain.
template<typename TItem> class
t_BeginPack
{
// This template will be selected for first block.
public: template<TItem... VInnerItems> using
t_Pack = t_PackImpl
<
TItem
// add item holding amount of items in new block
, TItem{static_cast<TItem>(sizeof...(VInnerItems))}
// add new block items
, VInnerItems...
>;
};
int main()
{
{
constexpr auto items
{
t_BeginPack<::std::uint8_t>::t_Pack<42>::to_array()
};
for(auto const & item: items)
{
::std::cout << static_cast<::std::uint32_t>(item) << ::std::endl;
}
}
::std::cout << "----------------" << ::std::endl;
{
constexpr auto items
{
t_BeginPack<::std::uint8_t>::t_Pack<0, 1, 2>::to_array()
};
for(auto const & item: items)
{
::std::cout << static_cast<::std::uint32_t>(item) << ::std::endl;
}
}
::std::cout << "----------------" << ::std::endl;
{
constexpr auto items
{
t_BeginPack<::std::uint8_t>::
t_Pack<0, 1, 2>::
t_Pack<0, 1>::
t_Pack<0, 1, 2, 3, 4, 5>::to_array()
};
for(auto const & item: items)
{
::std::cout << static_cast<::std::uint32_t>(item) << ::std::endl;
}
}
return(0);
}
在线运行