My original question was raised after reviewing my own implementation of webservice requests and handling responses.
Following is a pseudo-code for my current implementation
class classRoot
: public QObject {};
class classA :public classRoot{};
class classB :public classRoot{};
class classC :public classRoot{};
class requestTransport
: public QObject{
signals:
void onRequestComplete (classA* request);
void onRequestComplete (classB* request);
void onRequestComplete (classC* request);
}
class responseProcessor
: public QObject{
public slots:
void requestCompleted (classA* request);
void requestCompleted (classB* request);
void requestCompleted (classC* request);
};
void main()
{
requestTransport st;
responseProcessor rp;
connect(&st, SIGNAL(onRequestComplete(classA*)), &rp, SLOT(requestCompleted(classA*)));
...
connect(&st, SIGNAL(onRequestComplete(classC*)), &rp, SLOT(requestCompleted(classC*)));
}
class classRoot : public QObject {};
class classA :public classRoot{};
class classB :public classRoot{};
class classC :public classRoot{};
class requestTransport : public QObject
{
signals:
void onRequestComplete (classA* request);
void onRequestComplete (classB* request);
void onRequestComplete (classC* request);
}
class responseProcessor : public QObject
{
public slots:
void requestCompleted (classA* request);
void requestCompleted (classB* request);
void requestCompleted (classC* request);
};
void main()
{
requestTransport st;
responseProcessor rp;
connect(&st, SIGNAL(onRequestComplete(classA*)), &rp, SLOT(requestCompleted(classA*)));
...
connect(&st, SIGNAL(onRequestComplete(classC*)), &rp, SLOT(requestCompleted(classC*)));
}
To copy to clipboard, switch view to plain text mode
The advantage of this design is that there is explicit contract between requestTransport and responseProcessor for each type of request.
The major problem with the design is that it does not scale. Each new request type requires change in requestTransport, responseProcessor and main
in respect to the signal/slot connectivity
I am trying to find out if I can have something like the pseudo-code below. QSignalMapper, as far as I can see, cannot be used for this purpose
class requestTransport
{
public:
template<typename T> registerRequestType();
template<typename T> addRequest(T* request);
private signals:
void onRequestComplete(classRoot* request);
}
void main()
{
requestTransport st;
responseProcessor rp;
// following three lines register request types with signal Transport. Each call performs QObject::connect or some other
// metaobject call to wire onRequestComplete signal to the appropriate requestCompleted slot in responseProcessor instance
st.registerRequestType<classA> (&rp);
st.registerRequestType<classB> (&rp);
st.registerRequestType<classC> (&rp);
// this line adds new instance of the request to be sent via Transport. Upon completion the appropriate slot in the responseProcessor
// should be called.
st.addRequest<classB>(new classB());
}
class requestTransport
{
public:
template<typename T> registerRequestType();
template<typename T> addRequest(T* request);
private signals:
void onRequestComplete(classRoot* request);
}
void main()
{
requestTransport st;
responseProcessor rp;
// following three lines register request types with signal Transport. Each call performs QObject::connect or some other
// metaobject call to wire onRequestComplete signal to the appropriate requestCompleted slot in responseProcessor instance
st.registerRequestType<classA> (&rp);
st.registerRequestType<classB> (&rp);
st.registerRequestType<classC> (&rp);
// this line adds new instance of the request to be sent via Transport. Upon completion the appropriate slot in the responseProcessor
// should be called.
st.addRequest<classB>(new classB());
}
To copy to clipboard, switch view to plain text mode
The main advantage for me is that for every new type of request I have to only add new slot handler and register request type with the transport
It also adds clarity to the code, making visible the types of requests the transport is processing.
I don't care if it is templates or other technique, I want to know if it is possible to implement it Qt-way.
Any advice/discussion/solution will be greatly appreciated.
Bookmarks