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

懵懂的女人 提交于 2021-02-08 11:20:44

问题


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

回答1:


Instead of having a map, you can template a struct over code like the following:

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

First step checked, we have the type information somewhere for our different message codes. Now, how do we process them using that information? It's time for some template magic:

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

Then you can add another templates to find the relevant processing steps to apply on your queue depending on the message's code... To do so we must "cheat" a little bit: since we can only have the desired code at runtime, we can't branch the processing immediately, we need to use the "template switch" trick. This is illustrated below:

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

You can find a running example on Coliru with a dummy YourQueue and process() implementation.

This solves the consumer part. You can solve the producer part similarly by adding the relevant methods inside pack_processor specializations and a generic writeToQueue method which will use the same kind of Template Switch trick we just saw.




回答2:


So, I tried using polymorphism. It seems to work by storing the derived formatted message to the queue. While de-queuing, the vptr should point to correct implementation of 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.
   }
}

Producer:

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

Consumer:

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


来源:https://stackoverflow.com/questions/38564365/create-a-compile-time-key-to-type-map-which-is-filled-by-calls-to-a-variadic-fun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!