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() {
// Multiple requests might be accumulated in the queue before calling tick()
if (!queueA.empty()) {
processA(queueA.front())
queueA.pop_front(); // assume std::vector has pop_front()
}
// queueB can be empty although other queues are full of elements
if (!queueB.empty()) {
...
}
...
// Polymorphism might be used.
// if RequestA, RequestB, ... are derived by Request:
if (!poly_queue.empty()) {
Request* req = poly_queue.top();
req->process(/* status of handler */);
poly_queue.pop_front(); // assume std::vector has pop_front()
// However, the result is not same as one using separated queue,
// because the order of requests is not preserved.
// For example, in the separated queue example, the processing order might be,
// RequestA -> Request C or only RequestA.
// However, if I use the single queue, we cannot achieve same result.
}
private:
std::vector<RequestA> queueA;
std::vector<RequestB> queueB;
std::vector<RequestC> queueC;
...
std::vector<Request*> poly_queue;
};
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?