Creating template parameters at compile time

I created a class that looks like an array, but instead of storing data in the program itself, it transfers a byte from the file (to reduce the impact of RAM). Now it all works for me, but the programmer must define the class using the following:

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

Example:

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

Note that this is for the embedded processor, and the asm directive goes through an assembler tool to create a read-only file.

So here is my question: how can I eliminate the variables "location" and "size"? I hate that the programmer has to enter them manually, and he prefers some way of generating data at compile time. Therefore, instead of the programmer who needs to enter:

//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); 

They could simply enter:

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

. . ++ 11 - , (- constexpr ?). , C-Preprocessor , , . ?

EDIT :

ReadOnlyBlock :

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

ROM, , . , , , ++, .

EDIT:

1 k , . , !

+5
4

( #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__)> \
{};
+1

, , va_args? -

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

, , "", T1. "", sizeof (TV) + 1. , , , , -, ...

0

This can help. I wrote some things to count blocks can add DEF_BLOCK(size)to declare a block. You can try rewriting my example to distribute data inside my blocks.

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;
}
0
source

If the following is true:

  • You are not using __COUNTER__elswhere
  • All arrays have a maximum length of 5
  • All arrays are defined in the same file.

Then you can do this:

#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';
}

In theory, this prints:

@0: 4
@4: 4
@8: 1
@9: 5
0
source

All Articles