创建一个编译时键到类型映射,该映射由对可变函数的调用填充

Create a compile time key-to-type map which is filled by calls to a variadic function

本文关键字:映射 填充 调用 函数 类型 一个 编译 创建      更新时间:2023-10-16

是否可以在编译时创建一个键->类型映射,在调用可变函数的实例时添加每个键值?

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

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

在上面的例子中,要求有一个映射type->type或int->type(以可行的为准)的CODEMAP,但在调用函数foo时会填充映射,因此事先不需要知道代码和args。

这有可能吗?通过boost/预处理器/模板编程?

编辑:CODEMAP如前所述,是一个存储代码->类型信息的映射。在运行时,读取器块(比如R)将读取foo()存储/处理的消息,并根据消息开头的代码进行解析。代码总是固定大小(4个字符或1个整数)。

这是同一个翻译单元。

编辑:所以交易是这样的:

生产者:将数据写入FIFO队列(关键代码热路径)->消费者线程读取并处理队列中的信息。

下面是一个伪代码:

生产商:

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...>();
       }
    };
}

消费者:

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>());
        } 
      }  
    }
 }

您可以在code上模板化结构,而不是使用映射,如下所示:

enum Codes {
    BEGIN_CODE = 0,
    Code1,
    Code2,
    NB_CODES
};
template <typename ... Args>
struct param_pack {
    // Alternatively you could also use std::tuple?!
};
template <Code code>
struct code_info;
// You still have to manually define this at some point...
// For all your different messages...
template <>
struct code_info<Code1> {
    typedef param_pack<int, double, double> args_t;
};
template <>
struct code_info<Code2> {
    typedef param_pack<int, float, float, long> args_t;
}

检查第一步,我们有不同消息代码的类型信息。现在,我们如何使用这些信息来处理它们?是时候施展一些模板魔法了:

namespace details {
template <typename ArgPack>
struct pack_processor;
template <typename T, typename ... Args>
struct pack_processor<param_pack<T, Args...>> {
    static void process_pack(YourQueue& queue) {
        process<T>(queue.getData<T>());
        pack_processor<param_pack<Args...>>::process_pack(queue);
    }
};
template <typename T>
struct pack_processor<param_pack<T>> {
    static void process_pack(YourQueue& queue) {
        process<T>(queue.getData<T>());
    }
};
} // namespace details
template <Code code>
process_message(YourQueue& queue) {
    details::pack_processor<typename code_info<code>::args_t>::process_pack(queue);
}

然后,您可以添加另一个模板,根据消息的代码查找要应用于队列的相关处理步骤。。。要做到这一点,我们必须"欺骗"一点:由于我们只能在运行时获得所需的代码,我们不能立即分支处理,因此我们需要使用"模板切换"技巧。如下所示:

namespace details {
// This is not static:
// you can't have static polymorphism when you get information from runtime...
template <Code ref_code>
void process_data_with_code(Code code, YourQueue& queue) {
    // We found the good code:
    if (code == ref_code) {
        // We retrieve the static information
        // and run the appropriate process_pack specialization -> this is static
        process_message<ref_code>(queue);
    } else {
        process_data_with_code<static_cast<Code>(ref_code-1)>(code, queue);
    }
}
template <>
void process_data_for_code<BEGIN_CODE>(Code code, YourQueue& queue) {
    std::cout << "Code not found..." << std::endl;
}
} // namespace details
void process_data(Code code, YourQueue& queue) {
    process_data_for_code<static_cast<Code>(NB_CODE-1)>(code, queue);
}

您可以在Coliru上找到一个使用伪YourQueueprocess()实现的运行示例。

这解决了消费者的问题。您可以通过在pack_processor专业化中添加相关方法和通用writeToQueue方法来类似地解决生产者部分,该方法将使用我们刚才看到的模板切换技巧。

所以,我尝试使用多态性。它似乎是通过将派生的格式化消息存储到队列中来工作的。在取消排队时,vptr应该指向process()的正确实现。

class Message {
   virtual void process() = 0;
}
template <typename... Args>
class FormattedMessage : public Message {
   std::tuple<Args...> data;
   //Specialize process function for each formatted message.
   void process() {
      //As now I have the tuple, I can easily iterate/process it.
   }
}

生产商:

template <typename ...Args>
void Producer::writeToQueue(Args... args) {
    using fmttype = FormattedMessage<Args...>;
    this->queue.push<fmttype>(args...);
}

消费者:

void Consumer::readfromQueue() {
    while(true) {
      if (queue.dataAvailable()) {
          this->queue.template front<Message>().process();
          this->queue.pop(this->queue.template front<Message>().size());
      }  
    }
 }