Hi all, I have a bit of a question about the best / Qt way of handling the following:
I'm building a Qt library that performs queries against a web service, interprets the XML results and creates instances of classes representing the results.
for simplicity, lets say the XML looks like this:
<node A>
<attribute node A1 />
<attribute node A2 />
<attribute node A3 />
</node A>
<node B>
<attribute node B1 />
<attribute node B2 />
</node B>
... etc...
<node A>
<attribute node A1 />
<attribute node A2 />
<attribute node A3 />
</node A>
<node B>
<attribute node B1 />
<attribute node B2 />
</node B>
... etc...
To copy to clipboard, switch view to plain text mode
The idea is to turn a query phrased as an http request that returns xml containing one or more instances of a single node type into instances of their matching class.
So on the Qt/C++ side we have:
//the classes matching the possible response types (lets call em 'the node classes')
Class virtual base NodeClass //the common properties
Class ANodeClass //NodeClass subclass mapping <node A>
Class BNodeClass //NodeClass subclass mapping <node B>
Class ... etc..
Class WebService //holds some basic info about the web service
Class QueryManager //contains a list of queries, start executing them, monitors their progress
Class Query //forms an http request and receives XML which is stored in a buffer
Class XmlResultHandler //takes an XML buffer and creates an instance of one of the node classes
So, basically I'm trying to create a situation where a query is executed and behaves as a factory to produce instances of one or other of the node classes.
There are two main issues I'm struggling with...
1. My first thought was to make the Query class a template so it could be instantiated with one of the node classes as the template type, which it could then return an instance of. Because Query is a Q_OBJECT this can't be done. Is there another approach that could allow a single class to return a dynamically selected type? Can you use multiple inheritance with a Q_OBJECT class and a pure C++ template class?
2. The XmlResultHandler class also produces an instance of a node class. There are a few common attributes in the base class but the majority of attributes are unique to each node subclass. With the basic pattern of 'parse the xml, call the member function' is there a way to implement this generically so that the matching of node types and their matching member functions can be determined dynamically? The idea here is that in the future if new nodes are introduced into the XML schema the code could load NodeClasses as plugins and dynamically determine how to process the XML and instantiate instances of the new NodeClass
thanks for any help, ideas or advice anyone can offer
Bookmarks