5

我创建了一个看起来像数组的类,但它不是在程序本身中保存数据,而是从文件中流式传输字节(以减少对 RAM 的影响)。现在我已经完成了所有这些工作,但是程序员必须使用以下内容定义类:

#define CreateReadOnlyBlock(name, location, size, ...)          \
template<>                                                      \
const unsigned int ReadOnlyBlock<location, size>::Data[]        \
    __asm__( ".readonly__" #location "__" #name)                \
    = { __VA_ARGS__ };                                          \
ReadOnlyBlock<location, size> name;

例子:

//A read only array of {0, 1, 2, 3}
CreateReadOnlyBlock(readOnlyArray, 0, 4, 0, 1, 2, 3); 

请注意,这是针对嵌入式处理器的,asm 指令通过汇编器中的工具来创建只读文件。

所以这是我的问题:如何消除“位置”和“大小”变量?我讨厌程序员必须手动输入它们,并且更喜欢在编译时生成它们的某种方式。因此,程序员不需要键入:

//A read only array at location 0 of {0, 1, 2, 3}
CreateReadOnlyBlock(readOnlyArray1, 0, 4, 0, 1, 2, 3); 
//A read only array at location 4 of {4, 5, 6, 7}
CreateReadOnlyBlock(readOnlyArray2, 4, 4, 4, 5, 6, 7); 

他们可以输入:

CreateReadOnlyBlock(readOnlyArray1, 0, 1, 2, 3); 
CreateReadOnlyBlock(readOnlyArray2, 4, 5, 6, 7); 

并且会生成适当的常量。基本上,我正在寻找某种方法来在编译时根据先前的定义生成和放置这些常量。C ++ 11 是公平的游戏,我只是不太熟悉它(带有 constexpr 的东西似乎是合理的?)。此外,如果 C-Preprocessor 不会使它变得比现在更丑,它也可以。这可能吗?

为清楚起见编辑:

在 ReadOnlyBlock 类中有这个方法:

    template<const int _location, const int _size> class ReadOnlyBlock
    {
        ...
        unsigned int operator[] (size_t index)
        {
            return LoadFromROM(index + _location);
        }
    }

位置变量和 ROM 文件之间存在内在的相互依赖关系,我想不出如何打破。但是,我可以完全控制工具链,但是我需要一种方法来传递汇编器工具如何构造文件以及向 C++ 代码指示块在文件中的位置。

另一个编辑:

该文件及其块可能非常大,多达 1k 个字,因此很多预处理器魔法可能会崩溃。另外,感谢大家迄今为止的帮助!

4

4 回答 4

1

我仍然没有看到生成名称(那个#location片段)的完整解决方案,但对于其余的,我想你可以使用这样的东西:

template< std::size_t Line >
struct current_location : current_location< Line - 1 > {};

template<>
struct current_location< 0 > : std::integral_constant< std::size_t, 0 > {};

#define NUMARGS(...) (sizeof((int[]){__VA_ARGS__})/sizeof(int))

#define CreateReadOnlyBlock(name, ...)                          \
template<>                                                      \
const unsigned int ReadOnlyBlock<                               \
    current_location<__LINE__-1>::value, NUMARGS(__VA_ARGS__)   \
>::Data[]                                                       \
    __asm__( ".readonly__" #name)                               \
    = { __VA_ARGS__ };                                          \
ReadOnlyBlock<current_location<__LINE__-1>::value,              \
              NUMARGS(__VA_ARGS__)> name;                       \
template<>                                                      \
struct current_location<__LINE__>                               \
    : std::integral_constant<std::size_t,                       \
        current_location<__LINE__-1>::value+NUMARGS(__VA_ARGS__)> \
{};
于 2013-03-21T18:15:12.897 回答
0

也许有点掌握,但是可以使用可变参数模板而不是 va_args 吗?就像是

template <typename T1, ... TV>
class ReadOnlyBlock
{
    static unsigned int Data[sizeof(TV) + 1];
};

基本上,任何你需要“位置”的地方,都可以使用 T1。在任何需要“大小”的地方,使用 sizeof(TV) + 1。没有合适的编译器来测试它,但也许需要考虑......

于 2013-03-21T18:14:24.343 回答
0

它可能会有所帮助。我写了一些东西来计算块可以在DEF_BLOCK(size)块声明之前添加。您可以尝试重写我的示例以在我的块内分配数据

template<size_t ID>
struct block_t
{
   enum{location = 0};
};

#define LAST_BLOCK struct last_block_t{enum{id=__COUNTER__-1};};

#define SPEC2(ID, SIZE) template<> struct block_t<ID>{enum{location = block_t<ID-1>::location + SIZE, prev_block = ID-1, size = SIZE};}
#define SPEC(ID, SIZE) SPEC2(ID, SIZE)

#define DEF_BLOCK(SIZE) SPEC(__COUNTER__, SIZE)

DEF_BLOCK(10);
DEF_BLOCK(11);
LAST_BLOCK;

int _tmain(int argc, _TCHAR* argv[])
{
   std::cout << block_t<last_block_t::id>::location << std::endl;
    return 0;
}
于 2013-03-21T18:14:45.983 回答
0

如果以下情况成立:

  • 你不在__COUNTER__其他地方使用
  • 所有数组的最大长度为 5
  • 所有数组都在同一个文件中定义

然后你可以这样做:

#include <iostream>

#define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
#define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)

template <int... Args>
struct arg_counter {
  enum { count = sizeof...(Args) };
};

#define INC_COUNTER1 arg_counter<-1>::count
#define INC_COUNTER2 arg_counter<-1, __COUNTER__>::count
#define INC_COUNTER3 arg_counter<-1, __COUNTER__, __COUNTER__>::count
#define INC_COUNTER4 arg_counter<-1, __COUNTER__, __COUNTER__, __COUNTER__>::count
#define INC_COUNTER5 arg_counter<-1, __COUNTER__, __COUNTER__, __COUNTER__, __COUNTER__>::count

#define INC_COUNTER_IMPL2(count, ...) INC_COUNTER ## count
#define INC_COUNTER_IMPL(count, ...) INC_COUNTER_IMPL2(count, __VA_ARGS__) 
#define INC_COUNTER(...) INC_COUNTER_IMPL(VA_NARGS(__VA_ARGS__), __VA_ARGS__)

// removed: __asm__( ".readonly__" #location "__" #name)
#define CreateReadOnlyBlockImpl(name, location, size, ...)      \
  template<>                                                    \
  const unsigned int ReadOnlyBlock<location, size>::Data[]      \
    = { __VA_ARGS__ };                                          \
  ReadOnlyBlock<location, size> name;


#define CreateReadOnlyBlock(name, ...)                                  \
  CreateReadOnlyBlockImpl(name, __COUNTER__, INC_COUNTER(__VA_ARGS__), __VA_ARGS__);

template<int Location, int Size> struct ReadOnlyBlock
{
  static const unsigned int Data[Size];
  int loc () const { return Location; }
  int size() const { return Size; }
};

CreateReadOnlyBlock(readOnlyArray1, 0, 1, 2, 3);
CreateReadOnlyBlock(readOnlyArray2, 4, 5, 6, 7);
CreateReadOnlyBlock(readOnlyArray3, 9);
CreateReadOnlyBlock(readOnlyArray4, 1, 2, 3, 4, 5);

int main()
{
  std::cout << "@" << readOnlyArray1.loc() << ": " << readOnlyArray1.size() << '\n';
  std::cout << "@" << readOnlyArray2.loc() << ": " << readOnlyArray2.size() << '\n';
  std::cout << "@" << readOnlyArray3.loc() << ": " << readOnlyArray3.size() << '\n';
  std::cout << "@" << readOnlyArray4.loc() << ": " << readOnlyArray4.size() << '\n';
}

在 ideone 上打印:

@0: 4
@4: 4
@8: 1
@9: 5
于 2013-03-21T18:22:36.693 回答