Upon a existing source code framework, I'm trying to make source code to communicate between two objects (e.g., A and B).
AFAIK, it is impossible to deliver the message by using queue in each object due to incomplete type inference.
For example (assuming each class is defined in different header file, A.h B.h),
In class A:
B* b; // assume we have this declaration in class A
b->receiveMessageFromA(data); // object 'b' wants to receive a message from object 'a'
In class B:
A* a; // assume we have this declaration in class B
a->receiveMessageFromB(data); // object 'a' wants to receive a message from object 'b'
Above implementation cannot be done even if I use forward class declaration since incomplete type compile error occurs.
To compensate this, I thought that defining class called MessageQueue
can help this.
Now, I want to give simple example to define class A, B, and Message Queue.
class Message { // Assume Request is defined somewhere
public:
std::vector<Request> messageFromA; // message q
std::vector<Request> messageFromB; // message q
}
class A {
public:
Message* msg;
void sendMsgToB(Request req) {
msg->messageFromA.push_back(req); // send message to B
}
void tick() {
if (msg->messageFromB().size() != 0) {
// if 'messageFromB' queue has elements, A can receive message from B
}
}
}
class B {
public:
Message* msg;
void sendMsgToA(Request req) {
msg->messageFromB.push_back(req);
}
void tick() {
if (msg->messageFromA.size() != 0) {
// if 'messageFromA' queue has elements, B can receive message from A
}
}
}
Is designing class Message
is a bad choice to communicate between two classes?
I heard that there exists listener/observer pattern, but it might require modifying a lot of source code in the existing framework by breaking original design patterns.
Aucun commentaire:
Enregistrer un commentaire