Is it possible to create a key->type map at compile time, with each key-value being added when an instance of a variadic function is called?
template <typename T, typename ... Args>
void writeToQueue(Args... args) {
//Do something with args.
// Insert to map. something akin to:
// CODEMAP[T] = Args...
// T -> Args... mapped when foo<T,Args...> is called.
}
Or
template <int code, typename ... Args>
void writeToQueue(Args... args) {
//Do something with args.
// Insert to map. something akin to:
// CODEMAP[code] = Args...
// code -> Args... mapped when foo<code,Args...> is called.
}
In the above, the requirement is have a CODEMAP that maps either type->type or int->type (whichever is feasible) but the map is populated when the function foo is called, so that it is not a requirement beforehand to know code and args.
Is this at all possible? Either through boost/preprocessor/template programming?
Edit: CODEMAP is as stated, a map to store code -> type information. During runtime, a reader block (say R) would read the messages stored/processed by foo() and parse based on the code at the beginning of a message. the code is always fixed size (4 chars or 1 int).
It's the same translation unit.
Edit: So here's the deal:
Producer: writes data to FIFO queue (critical code hot path) -> Consumer thread reads and process the info from the queue.
A pseudo code is below:
Producer:
void Producer::run() {
// This guy shouldn't worry about the type of data being written.
// So, encapsulating data into structs and passing it to queue is
// out of question.
writeToQueue<Code1>(1,2,"123123",'a', 3.1416);
writeToQueue<Code2>(4,1,'b');
template <int Code, typename ...Args>
void writeToQueue(Args... args) {
queue.insert(args...);
// Need code to args... mapping. So, decided to create static
// instantiation of a formatspecifier class.
static formatspecifier<Code, args...> f{};
}
// To encode the type information to be used in run time.
template <int Code, typename ... Args>
class formatspecifier{
formatspecifier() {
global::codemap[Code] = encodeTypeInfo<Args...>();
}
};
}
Consumer:
void Consumer::readfromQueue() {
while(true) {
if (queue.dataAvailable()) {
const auto code = queue.getCode();
// get encoded type info format from global::codemap map.
const auto fmt = global::codemap[code];
for (int i=0; i < fmt.len; i++) {
// I am unsure how this part should look.
process<fmt[0]::type>(queue.getData<fmt[0]::type>());
}
}
}
}