vendredi 8 avril 2022

how to push data into different queues based on the data type in cpp?

I have problems to push data into different queues based on the data type.

To be specific, I have multiple request classes. For example:

class RequestA {
};
class RequestB {
};
class RequestC {
};
...

These requests might be inherited by certain class (e.g., class Request).

Each object of these data types must be put into different queues. For example:

std::vector<RequestA> queueA;
std::vector<RequestB> queueB;
std::vector<RequestC> queueC;
...

The reason why I need different queues for each class is that each request at the top (front) of the queue requires different (post-)processing steps later.

For instance,

class RequestHandler {
public:
  void tick() {
     if (!queueA.empty()) {
        processA(queueA.front())
        queueA.pop_front(); // assume std::vector has pop_front()
     }
     if (!queueB.empty()) {
       ...
     }
     ...
   }
private:
 std::vector<RequestA> queueA;
 std::vector<RequestB> queueB;
 std::vector<RequestC> queueC;
...
};

However, the problem is that ,to push data into the queue, I might use followings:

if (typeid()) {
   queueA.push_back();
} else if (typeid()) {
   queueB.push_back();
} ...

or if RequestA, RequestB, ... are inherited by class Request:

std::map<std::type_index, std::vector<Request*>> queue;

queue[typeid()].push_back(...);

might be used.

However, it might be better to avoid using typeid or dynamic_cast.

How to efficiently handle this scenario?

Aucun commentaire:

Enregistrer un commentaire